Python Qt5: AttributeError: 'module' 对象没有属性 'QApplication'

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34959409/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 15:46:18  来源:igfitidea点击:

Qt5: AttributeError: 'module' object has no attribute 'QApplication'

pythonpyqtqt5

提问by Portu

System: 15.10 (Wily Werewolf) x64

系统:15.10(Wily Werewolf)x64

Code:

代码:

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'test.ui'
#
# Created by: PyQt5 UI code generator 5.5.1
#
# WARNING! All changes made in this file will be lost!

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(400, 300)
        self.pushButton = QtWidgets.QPushButton(Form)
        self.pushButton.setGeometry(QtCore.QRect(120, 120, 85, 26))
        self.pushButton.setObjectName("pushButton")

        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle(_translate("Form", "Form"))
        self.pushButton.setText(_translate("Form", "PushButton"))

if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    ex = Ui_Form()
    ex.show()
    sys.exit(app.exec_())

I get the following error:

我收到以下错误:

    app = QtGui.QApplication(sys.argv)
AttributeError: 'module' object has no attribute 'QApplication'

Basically, I designed the GUI with the Qt5 and then used pyuic5. I already installed PyQt5, not sure if the installation went through as expected.

基本上,我用 Qt5 设计了 ​​GUI,然后使用了 pyuic5。我已经安装了 PyQt5,不确定安装是否按预期进行。

UI Design:

界面设计:

enter image description here

在此处输入图片说明

Any help will be highly appreciated.

任何帮助将不胜感激。

采纳答案by Peter

I am not sure how your main function was generated. I tried to replicate it with what seems to be the same version of pyuic5. I am calling it with the commandline pyuic5 -x untitled.ui(where the ui as in your case just contains a PushButton in a Widget). The -xoption has the effect: 'The generated Python code includes a small amount of additional code that creates and displays the GUI when it is executes as a standalone application.' (http://pyqt.sourceforge.net/Docs/PyQt5/designer.html) The result I get is

我不确定您的主要功能是如何生成的。我试图用似乎是相同版本的 pyuic5 来复制它。我用命令行调用它pyuic5 -x untitled.ui(在你的情况下,ui 只包含一个 Widget 中的 PushButton)。该-x选项具有以下效果:“生成的 Python 代码包括少量附加代码,这些代码在作为独立应用程序执行时创建和显示 GUI。” (http://pyqt.sourceforge.net/Docs/PyQt5/designer.html)我得到的结果是

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'untitled.ui'
#
# Created by: PyQt5 UI code generator 5.5.1
#
# WARNING! All changes made in this file will be lost!

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(400, 300)
        self.pushButton = QtWidgets.QPushButton(Form)
        self.pushButton.setGeometry(QtCore.QRect(70, 50, 75, 23))
        self.pushButton.setObjectName("pushButton")

        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle(_translate("Form", "Form"))
        self.pushButton.setText(_translate("Form", "PushButton"))


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    Form = QtWidgets.QWidget()
    ui = Ui_Form()
    ui.setupUi(Form)
    Form.show()
    sys.exit(app.exec_())

which has a different main function. The rest of the code is equivalent.

它具有不同的主要功能。其余代码是等效的。