代码
'''弹出不同种类的消息提示框
'''
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QMessageBoxclass Ui_MainWindow(object):def setupUi(self, MainWindow):MainWindow.setObjectName("MainWindow")MainWindow.resize(443, 88)self.centralwidget = QtWidgets.QWidget(MainWindow)self.centralwidget.setObjectName("centralwidget")self.pushButton = QtWidgets.QPushButton(self.centralwidget)self.pushButton.setGeometry(QtCore.QRect(30, 30, 61, 31))font = QtGui.QFont()font.setPointSize(12)font.setBold(True)font.setWeight(75)self.pushButton.setFont(font)self.pushButton.setObjectName("pushButton")self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)self.pushButton_2.setGeometry(QtCore.QRect(110, 30, 61, 31))font = QtGui.QFont()font.setPointSize(12)font.setBold(True)font.setWeight(75)self.pushButton_2.setFont(font)self.pushButton_2.setObjectName("pushButton_2")self.pushButton_3 = QtWidgets.QPushButton(self.centralwidget)self.pushButton_3.setGeometry(QtCore.QRect(190, 30, 61, 31))font = QtGui.QFont()font.setPointSize(12)font.setBold(True)font.setWeight(75)self.pushButton_3.setFont(font)self.pushButton_3.setObjectName("pushButton_3")self.pushButton_4 = QtWidgets.QPushButton(self.centralwidget)self.pushButton_4.setGeometry(QtCore.QRect(270, 30, 61, 31))font = QtGui.QFont()font.setPointSize(12)font.setBold(True)font.setWeight(75)self.pushButton_4.setFont(font)self.pushButton_4.setObjectName("pushButton_4")self.pushButton_5 = QtWidgets.QPushButton(self.centralwidget)self.pushButton_5.setGeometry(QtCore.QRect(350, 30, 61, 31))font = QtGui.QFont()font.setPointSize(12)font.setBold(True)font.setWeight(75)self.pushButton_5.setFont(font)self.pushButton_5.setObjectName("pushButton_5")MainWindow.setCentralWidget(self.centralwidget)self.retranslateUi(MainWindow)QtCore.QMetaObject.connectSlotsByName(MainWindow)def retranslateUi(self, MainWindow):_translate = QtCore.QCoreApplication.translateMainWindow.setWindowTitle(_translate("MainWindow", "弹出不同种类的消息提示框"))self.pushButton.setText(_translate("MainWindow", "消息框"))self.pushButton_2.setText(_translate("MainWindow", "警告框"))self.pushButton_3.setText(_translate("MainWindow", "问题框"))self.pushButton_4.setText(_translate("MainWindow", "错误框"))self.pushButton_5.setText(_translate("MainWindow", "关于框"))self.pushButton.clicked.connect(self.info)self.pushButton_2.clicked.connect(self.warn)self.pushButton_3.clicked.connect(self.question)self.pushButton_4.clicked.connect(self.critical)self.pushButton_5.clicked.connect(self.about)def info(self): QMessageBox.information(None, '消息', '这是一个消息提示框', QMessageBox.Ok)def warn(self): QMessageBox.warning(None, '警告', '这是一个警告提示框', QMessageBox.Ok)def question(self): QMessageBox.question(None, '问题', '这是一个问题提示框', QMessageBox.Ok)def critical(self): QMessageBox.critical(None, '错误', '这是一个错误提示框', QMessageBox.Ok)def about(self): QMessageBox.about(None, '关于', '这是一个关于提示框')
if __name__ == '__main__':import sysapp = QtWidgets.QApplication(sys.argv)MainWindow = QtWidgets.QMainWindow() ui = Ui_MainWindow() ui.setupUi(MainWindow) MainWindow.show() sys.exit(app.exec_())
效果