gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 10:36:11 24 4
gpt4 key购买 nike

系统:15.10(狡猾的狼人)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,不确定安装是否按预期进行。

用户界面设计:

enter image description here

任何帮助将不胜感激。

最佳答案

我不确定你的 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_())

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

关于python - Qt5: AttributeError: 'module' 对象没有属性 'QApplication',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34959409/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com