pyqt5 QMessageBox无法打开

时间:2017-11-03 04:56:16

标签: python pyqt pyqt5 qt-designer qmessagebox

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

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

1 个答案:

答案 0 :(得分:0)

问题是由于QMessageBox.about()需要作为小部件的第一个参数而自我,即Ui_MainWindow,不是小部件。 Qt Designer不会生成窗口小部件,但会创建一个填充窗口小部件的类。也不建议修改Qt Designer生成的类,适合创建一个继承自相应小部件并使用设计类的类

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