Python报错——AttributeError: module 'PyQt5.QtGui' has no attribute 'QApplication'

Python报错——AttributeError: module ‘PyQt5.QtGui’ has no attribute ‘QApplication’

初次用Python制作图形界面,遇到了这个问题
AttributeError: module ‘PyQt5.QtGui’ has no attribute ‘QApplication’

代码如下:

    from PyQt5 import QtCore, QtGui, QtWidgets
    class Ui_MainWindow(object):
        def setupUi(self, MainWindow):
            MainWindow.setObjectName("MainWindow")
            MainWindow.resize(520, 401)
            self.centralwidget = QtWidgets.QWidget(MainWindow)
            self.centralwidget.setObjectName("centralwidget")
            self.quitButton = QtWidgets.QPushButton(self.centralwidget)
            self.quitButton.setGeometry(QtCore.QRect(350, 260, 91, 51))
            self.quitButton.setAutoDefault(False)
            self.quitButton.setDefault(False)
            self.quitButton.setFlat(False)
            self.quitButton.setObjectName("quitButton")
            MainWindow.setCentralWidget(self.centralwidget)
            self.menubar = QtWidgets.QMenuBar(MainWindow)
            self.menubar.setGeometry(QtCore.QRect(0, 0, 520, 23))
            self.menubar.setObjectName("menubar")
            MainWindow.setMenuBar(self.menubar)
            self.statusbar = QtWidgets.QStatusBar(MainWindow)
            self.statusbar.setObjectName("statusbar")
            MainWindow.setStatusBar(self.statusbar)

            self.retranslateUi(MainWindow)
            QtCore.QMetaObject.connectSlotsByName(MainWindow)

        def retranslateUi(self, MainWindow):
            _translate = QtCore.QCoreApplication.translate
            MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
            self.quitButton.setText(_translate("MainWindow", "quit"))


    if __name__ == "__main__":
        import sys
        app=QtWidgets.QApplication(sys.argv)
        widget=QtWidgets.QWidget()
        ui=Ui_MainWindow()
        ui.setupUi(widget)
        widget.show()
        sys.exit(app.exec_())

查了一些资料后发现:我用qt designer创建的是一个mainwindow,而不是wideget或者dialog,所以需要把

widget=QtWidgets.QWidget()

改成

widget=QtWidgets.QMainWindow()

然后界面就出来啦~~~

你可能感兴趣的:(Python报错)