PyQt5最全20 对话框之QMessageBox显示不同类型的消息对话框

PyQt5最全20 对话框之QMessageBox显示不同类型的消息对话框

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


class QMessageBoxDemo(QWidget):
    """
    QMessageBox 消息对话框

    1.关于对话框     about
    2.错误对话框     critical    关键的; 批评的,爱挑剔的; 严重的; 极重要的;
    3.警告对话框     warning
    4.提示对话框     question
    5,消息对话框    information
    有两点差异:1.显示的对话框图标不一样 2.显示的按钮是不一样的

    PyQt5中的几种窗口:
    QMainWindow 在最基本的窗口上增加了菜单栏 工具栏 标题栏 状态栏等
    QWidget 最基本的窗口
    Qdialog 对话框窗口
    """
    def __init__(self):
        super(QMessageBoxDemo, self).__init__()
        self.initUI()

    def initUI(self):
        self.setWindowTitle('QMessageBox演示')
        self.resize(600, 400)

        layout = QVBoxLayout()
        # 关于对话框
        # self.button1 = QPushButton('显示关于对话框')
        self.button1 = QPushButton()
        self.button1.setText('显示关于对话框')
        self.button1.clicked.connect(self.showDialog)
        layout.addWidget(self.button1)

        # 消息对话框
        self.button2 = QPushButton()
        self.button2.setText('显示消息对话框')
        self.button2.clicked.connect(self.showDialog)
        layout.addWidget(self.button2)

        # 警告对话框
        self.button3 = QPushButton()
        self.button3.setText('显示警告对话框')
        self.button3.clicked.connect(self.showDialog)
        layout.addWidget(self.button3)

        # 错误对话框
        self.button4 = QPushButton()
        self.button4.setText('显示错误对话框')
        self.button4.clicked.connect(self.showDialog)
        layout.addWidget(self.button4)

        # 提问对话框
        self.button5 = QPushButton()
        self.button5.setText('显示提问对话框')
        self.button5.clicked.connect(self.showDialog)
        layout.addWidget(self.button5)

        self.setLayout(layout)

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


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


结果

在这里插入图片描述
通过点击不同的按钮,弹出来的消息对话框不同,带有不同类型的图标,可以设置弹出对话框的选择项和默认选择
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值