Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

qt5

pyqt

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.

like image 365
Portu Avatar asked Jan 23 '16 03:01

Portu


1 Answers

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.

like image 67
Abhyam Gupta Avatar answered Nov 15 '22 23:11

Abhyam Gupta