pyqt5 QMessageBox打不开

pyqt5 QMessageBox not open

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(9576, 698)
        font = QtGui.QFont()
        font.setFamily("Agency FB")
        font.setPointSize(9)
        MainWindow.setFont(font)
        MainWindow.setFocusPolicy(QtCore.Qt.StrongFocus)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.verticalLayoutWidget = QtWidgets.QWidget(self.centralwidget)
        self.verticalLayoutWidget.setGeometry(QtCore.QRect(20, 30, 221, 421))
        self.verticalLayoutWidget.setObjectName("verticalLayoutWidget")
        self.verticalLayout = QtWidgets.QVBoxLayout(self.verticalLayoutWidget)
        self.verticalLayout.setContentsMargins(0, 0, 0, 0)
        self.verticalLayout.setObjectName("verticalLayout")
        self.RButton = QtWidgets.QRadioButton(self.verticalLayoutWidget)
        font = QtGui.QFont()
        font.setPointSize(20)
        self.RButton.setFont(font)
        self.RButton.setObjectName("RButton")
        self.verticalLayout.addWidget(self.RButton)
        self.RButton2 = QtWidgets.QRadioButton(self.verticalLayoutWidget)
        font = QtGui.QFont()
        font.setPointSize(20)
        self.RButton2.setFont(font)
        self.RButton2.setObjectName("RButton2")
        self.verticalLayout.addWidget(self.RButton2)
        self.RButton3 = QtWidgets.QRadioButton(self.verticalLayoutWidget)
        font = QtGui.QFont()
        font.setPointSize(20)
        self.RButton3.setFont(font)
        self.RButton3.setObjectName("RButton3")
        self.verticalLayout.addWidget(self.RButton3)
        self.Button = QtWidgets.QPushButton(self.centralwidget)
        self.Button.setGeometry(QtCore.QRect(390, 150, 351, 221))
        font = QtGui.QFont()
        font.setFamily("Agency FB")
        font.setPointSize(24)
        self.Button.setFont(font)
        self.Button.setFocusPolicy(QtCore.Qt.NoFocus)
        self.Button.setAutoDefault(False)
        self.Button.setObjectName("Button")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 957, 21))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.Button.clicked.connect(self.btn_clicked)
        self.RButton.clicked.connect(self.btn_clicked)

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




    def btn_clicked(self):
        QMessageBox.about(self,"창","안녕")

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.RButton.setText(_translate("MainWindow", "버튼1"))
        self.RButton2.setText(_translate("MainWindow", "버튼2"))
        self.RButton3.setText(_translate("MainWindow", "버튼3"))
        self.Button.setText(_translate("MainWindow", "확인"))

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_())

当按钮点击程序关闭时... 我需要帮助

问题是因为 QMessageBox.about() 需要一个小部件作为第一个参数,但自身,即 Ui_MainWindow,不是一个小部件。 Qt Designer 不生成小部件,而是创建一个 class 来填充小部件。也不建议修改Qt Designer生成的class,创建一个继承自适当widget的class是合适的,使用设计class

class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
    def __init__(self, *args, **kwargs):
        QtWidgets.QMainWindow.__init__(self, *args, **kwargs)
        self.setupUi(self)
        self.Button.clicked.connect(self.btn_clicked)
        self.RButton.clicked.connect(self.btn_clicked)

    def btn_clicked(self):
        QMessageBox.about(self,"창","안녕")


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    w = MainWindow()
    w.show()
    sys.exit(app.exec_())