Answer a question

System: 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.

UI Design:

enter image description here

Any help will be highly appreciated.

Answers

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 -x option 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

# -*- 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.

Logo

Python社区为您提供最前沿的新闻资讯和知识内容

更多推荐