PyQT5之QMessageBox

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


class QMessageBoxDemo(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        self.setWindowTitle('QMessageBox实例')
        self.resize(300, 300)

        button1 = QPushButton()
        button1.setText('显示关于对话框')

        button2 = QPushButton()
        button2.setText('显示消息对话框')

        button3 = QPushButton()
        button3.setText('显示警告对话框')

        button4 = QPushButton()
        button4.setText('显示错误对话框')

        button5 = QPushButton()
        button5.setText('显示提问对话框')

        layout = QVBoxLayout()
        layout.addWidget(button1)
        layout.addWidget(button2)
        layout.addWidget(button3)
        layout.addWidget(button4)
        layout.addWidget(button5)
        self.setLayout(layout)

        button1.clicked.connect(self.showDialog)
        button2.clicked.connect(self.showDialog)
        button3.clicked.connect(self.showDialog)
        button4.clicked.connect(self.showDialog)
        button5.clicked.connect(self.showDialog)

    def showDialog(self):
        text = self.sender().text()
        if text == "显示关于对话框":
            QMessageBox.about(self, '关于', '这是一个关于对话框')
        elif text == "显示消息对话框":
            reply = QMessageBox.information(self, '消息', '这是一个消息对话框',
                                            QMessageBox.Yes | QMessageBox.No,
                                            QMessageBox.Yes)
            print(reply == QMessageBox.Yes)
        elif text == "显示警告对话框":
            QMessageBox.warning(self, '警告', '这是一个警告对话框',
                                QMessageBox.Yes | QMessageBox.No,
                                QMessageBox.Yes)
        elif text == "显示错误对话框":
            QMessageBox.critical(self, '错误', '这是一个错误对话框',
                                 QMessageBox.Yes | QMessageBox.No,
                                 QMessageBox.Yes)
        elif text == "显示提问对话框":
            QMessageBox.question(self, '提问', '这是一个提问对话框',
                                 QMessageBox.Yes | QMessageBox.No,
                                 QMessageBox.Yes)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    main = QMessageBoxDemo()
    main.show()
    sys.exit(app.exec_())


posted @ 2024-06-13 15:49  星空28  阅读(24)  评论(0编辑  收藏  举报