使用 QAction 获取 TypeError

问题描述 投票:0回答:1

在执行以下代码片段时,我收到如下 TypeError:

类型错误:参数与任何重载调用都不匹配:
QAction(QObject):参数 1 具有意外类型“str”QAction(str, QObject): 参数 2 具有意外类型 'Ui_MainWindow'
QAction(QIcon, str, QObject):参数 1 具有意外类型“str”

有人可以帮我重新评级吗?我对这件事很陌生,提前谢谢!!

代码片段:

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        ##
        #......some more codes
        ##
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

        #app.aboutToQuit.connect(self.closeEvent)
        quit = QAction("Quit", self)
        quit.triggered.connect(self.closeEvent)

        menubar = self.menuBar()
        fmenu = menubar.addMenu("File")
        fmenu.addAction(quit)


    def retranslateUi(self, MainWindow):
        ## codes


    def closeEvent(self, event):
        choice = QtGui.QMessageBox.question(self.centralwidget,"Quit message","Are you sure you want to leave?",QtGui.QMessageBox.Yes | QtGui.QMessageBox.No)
        if choice == QtGui.QMessageBox.Yes:
            event.accept()
        else:
            event.ignore()



if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()

    sys.exit(app.exec_())
python pyqt5
1个回答
0
投票

只需传递 str 不要通过自我 退出 = QAction("退出")

© www.soinside.com 2019 - 2024. All rights reserved.