简体   繁体   English

如何正确运行pyqt5应用程序?

[英]How to run a pyqt5 application properly?

I can't get the GUI for my application to run in the manner that I need it to. 我无法以我需要的方式来让我的应用程序运行GUI。 My question is, given the below criteria, how do I go about setting up and running the GUI properly. 我的问题是,考虑到以下条件,我该如何正确设置和运行GUI。 The lack of good documentation that I have found is insanely frustrating (maybe I'm looking in the wrong places?). 我发现缺少好的文档令人发疯(很可能是我在错误的地方找东西?)。

I have a main window in a file called MainCustomerWindow.py containing a class by the same name. 我在一个名为MainCustomerWindow.py的文件中有一个主窗口,其中包含一个具有相同名称的类。 This is where all the code from the qt designer is. 这是qt设计器中所有代码的所在。 I have another class file called GUIController. 我有另一个名为GUIController的类文件。 The GUIController class does just that, controls the multiple GUI windows. GUIController类正是这样做的,它控制着多个GUI窗口。 It is in this GUIController class that I am trying to instantiate and run the MainCustomerWindow. 我试图在此GUIController类中实例化并运行MainCustomerWindow。 Here is the code I have been trying. 这是我一直在尝试的代码。

def setup_window(self):
    APP = QtWidgets.QApplication(sys.argv)
    Window = MainCustomerWindow()
    Window.setupUi(QtWidgets.QMainWindow)
    Window.show()
    sys.exit(APP.exec_())

Just as a side note, I come from JavaFX and Swing, and don't fully understand the workflow for pyqt5. 附带说明一下,我来自JavaFX和Swing,对pyqt5的工作流程并不完全了解。 So if someone could add an explanation for that as well it would be greatly appreciated. 因此,如果有人也可以对此添加解释,将不胜感激。

The class generated by Qt Designer is not a widget, it is a class used to fill an existing widget, so you must create an object in the window, assuming you have used the "Main Window" template, then the widget must be QMainWindow (if it is another maybe you should use QDialog or QWidget ), then you have to create another class that belongs to the design, and using the method setupUi() you must pass the widget to fill it: Qt Designer生成的类不是窗口小部件,它是用于填充现有窗口小部件的类,因此您必须在窗口中创建一个对象(假设您已使用“主窗口”模板),那么该窗口小部件必须为QMainWindow (如果是另一个,则应该使用QDialogQWidget ),然后必须创建属于该设计的另一个类,并使用setupUi()方法必须传递小部件以填充它:

def setup_window(self):
    app = QtWidgets.QApplication(sys.argv)
    # create window
    window = QtWidgets.QMainWindow()

    ui = MainCustomerWindow()
    # fill window
    ui.setupUi(window)

    window.show()

    sys.exit(app.exec_())

Although a better option is to create a new class and have it inherit from both: 尽管更好的选择是创建一个新类并使其从这两个类继承:

class MainWindow(QtWidgets.QMainWindow, MainCustomerWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        self.setupUi(self)

If you want to get detailed information I recommend you read the following: 如果您想获取详细信息,我建议您阅读以下内容:

You can try taking the code you have there and adding it to the main statement at the end of your app's script. 您可以尝试获取那里的代码,并将其添加到应用程序脚本末尾的main语句中。 You can also have this statement instantiate your class where the init method contains the setupui() call. 您还可以让此语句实例化您的类,其中init方法包含setupui()调用。 For example: 例如:

if __name__ == '__main__':
    app = QWidgets.QApplication(sys.argv)
    window = QMainWindow()
    main_window = MainCustomerWindow()

    window.show()
    sys.exit(app.exec())

This code first sets up the PyQt app as an instance of QApplication. 此代码首先将PyQt应用设置为QApplication的实例。 Next it instantiates an instance of QMainWindow so that PyQt knows what to display as the main app starts. 接下来,它实例化QMainWindow的实例,以便PyQt知道在主应用程序启动时显示的内容。 In my experience, I've put setupui() in the init method of the app class. 根据我的经验,我已经将setupui()放在了应用程序类的init方法中。 In your case, in the init method of MainCustomerWindow Finally, window.show() tells PyQt to begin rendering the main window. 在您的情况下,最后使用MainCustomerWindow的init方法, window.show()告诉PyQt开始渲染主窗口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM