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

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.


In PyQt5 the QApplication method is not supported to be used with QtGui instead it can be used with QtWidgets. Try to rewrite your code like

app = QtWidgets.QApplication(sys.argv)

if using PyQt5 instead of PyQt4. It should probably work. Other attributes like which i know QStyleFactory has also changed to be used with QtWidgets.

Hope this may solve your problem.

Tags:

Python

Pyqt

Qt5