【问题标题】:Qt5: AttributeError: 'module' object has no attribute 'QApplication'Qt5:AttributeError:“模块”对象没有属性“QApplication”
【发布时间】:2016-04-29 19:16:34
【问题描述】:

系统:15.10 (Wily Werewolf) x64

代码:

# -*- 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_())

我收到以下错误:

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

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

界面设计:

我们将不胜感激。

【问题讨论】:

  • 这个:dir(QtGui) 给你什么?
  • 我刚刚更新了帖子以供进一步参考。
  • 这段代码有几个问题:QApplicationQWidgets 中,缺少import sys 语句,Ui_Form 可能应该继承一个Qt 类QWidget?,ex.setupUi(ex)必须在ex.show() 之前调用。
  • 让我再试一次。我在练习中使用过。

标签: python pyqt qt5


【解决方案1】:

我不确定您的 main 函数是如何生成的。我试图用似乎是相同版本的 pyuic5 来复制它。我用命令行pyuic5 -x untitled.ui 调用它(在你的例子中,ui 只在小部件中包含一个 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_())

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

【讨论】:

  • 没错,import sys 语句丢失了。但是,这段代码是由 pyuic5 生成的... pyuic5 test.ui > test.py
【解决方案2】:

在 PyQt5 中,QApplication 方法不支持与QtGui 一起使用,而是可以与QtWidgets 一起使用。尝试重写您的代码,如

app = QtWidgets.QApplication(sys.argv)

如果使用 PyQt5 而不是 PyQt4。它应该可以工作。 我知道QStyleFactory 等其他属性也已更改为与QtWidgets 一起使用。

希望这可以解决您的问题。

【讨论】:

    猜你喜欢
    • 2010-11-18
    • 2018-08-28
    相关资源
    最近更新 更多