PyQt5之消息对话框


消息对话框主要涉及QMessageBox类。
QMessageBox类提供了一个对话框,用于通知用户或询问用户问题并接收答案。

消息框显示主要文本以提醒用户情况,信息性文本以进一步解释警报或询问用户一个问题,以及可选的详细文本,以便在用户请求时提供更多数据。 消息框还可以显示用于接受用户响应的图标和标准按钮。

提供了两个使用QMessageBox的API,基于属性的API和静态函数。 调用静态函数是更简单的方法,但是比使用基于属性的API更不灵活,结果信息较少。

消息对话框分为五种,分别是提示信息、询问、警告、错误、关于。

严重性级别和图标

在这里插入图片描述

常用的消息弹窗

弹窗类型描述
QMessageBox.NoIcon消息框没有任何图标
QMessageBox.Question该消息正在提问
QMessageBox.Information表示该消息没有任何异常
QMessageBox.Warning表示该消息是警告,但可以处理
QMessageBox.Critical表示该消息代表一个严重问题

常用标准按钮

  • 可以添加内置的自定义按钮,使用 setStandardButtons() 方法;

  • 如果标准按钮对于您的消息框不够灵活,可以使用 addButton() 重载,它接受 文本 和 ButtonRole 来添加自定义按钮。

  • QMessageBox 使用 ButtonRole 来确定屏幕上按钮的顺序(根据平台的不同而不同); 可以在调用 exec()之后测试 clickkedbutton()的值

  • 以下描述了标准按钮的标志QMessageBox.StandardButton;每个按钮都有一个定义的 QMessageBox.ButtonRole

标准按钮描述
QMessageBox.Ok使用AcceptRole
QMessageBox.Open使用AcceptRole
QMessageBox.Save使用AcceptRole
QMessageBox.Cancel使用RejectRole
QMessageBox.Close一个用 定义的“关闭”按钮RejectRole
QMessageBox.Discard“放弃”或“不保存”按钮,取决于平台,定义为DestructiveRole
QMessageBox.Apply使用ApplyRole
QMessageBox.Reset用ResetRole
QMessageBox.RestoreDefaults使用ResetRole
QMessageBox.Help使用HelpRole
QMessageBox.SaveAll使用AcceptRole
QMessageBox.Yes使用YesRole
QMessageBox.YesToAll3使用YesRole
QMessageBox.No使用NoRole
QMessageBox.NoToAll使用NoRole
QMessageBox.Abort一个用RejectRole
QMessageBox.Retry使用AcceptRole
QMessageBox.Ignore一个用AcceptRole
QMessageBox.NoButton无效的按钮

QMessageBox两套接口

QMessageBox提供两套接口来实现,一种是static functions(静态方法调用),另外一种 the property-base API(基于属性的API)。直接调用静态方法是一种比较简单的途径,但是没有基于属性API的方式灵活。

static functions

一般会使用到其提供的几个 static 函数(C++层的函数原型,其参数类型和python中的一样):

about类型方法

about(QWidget *parent, const QString &title, const QString &text)

  • parent:指定父组件
  • title:消息框中的标题
  • text:消息框中显示的内容

其它类型方法

question(QWidget *parent, const QString &title, const QString &text, StandardButtons buttons = StandardButtons( Yes | No ), StandardButton defaultButton = NoButton)

  • parent:用于指定父组件;
  • title:消息框中的标题
  • text:消息框中显示的内容
  • buttons:消息框中显示的button,它的取值是 StandardButtons ,每个选项可以使用 | 运算组合起来。如QMessageBox.Ok|QMessageBox.Cancel。
  • defaultButton ,消息框中默认选中的button。
  • 这个函数有一个返回值,用于确定用户点击的是哪一个按钮。我们可以直接获取其返回值。如果返回值是 Ok,也就是说用户点击了 Ok按钮,
  • QLabel支持HTML形式的文本显示,在resultLabel中是通过HTML的语法形式进行显示的。具体可以参考一下HTML语法。
    information,warning, critical的类似。

基于属性的API

QMessageBox类的 static 函数优点是方便使用,缺点也很明显,非常不灵活。我们只能使用简单的几种形式。为了能够定制QMessageBox细节,我们必须使用QMessageBox的属性设置 API。

举例

from PyQt5.QtWidgets import *
import sys


class Window(QWidget):
    def __init__(self):
        super().__init__()
        self.setup_ui()

    def setup_ui(self):
        self.setWindowTitle("消息对话框的使用")
        self.resize(300, 500)

        self.questButton = QPushButton("questButton")
        self.questButton.clicked.connect(self.show_message)
        self.infoButton = QPushButton("infoButton")
        self.infoButton.clicked.connect(self.show_message)
        self.warningButton = QPushButton("warningButton")
        self.warningButton.clicked.connect(self.show_message)
        self.criticalButton = QPushButton("criticalButton")
        self.criticalButton.clicked.connect(self.show_message)
        self.aboutButton = QPushButton("aboutQtButton")
        self.aboutButton.clicked.connect(self.show_message)
        self.infoButton2 = QPushButton("infoButton2")
        self.infoButton2.clicked.connect(self.show_message)

        msgBox = QMessageBox()
        msgBox.setIcon(QMessageBox.Information)
        msgBox.setWindowTitle("The property-base API")
        msgBox.setText("The Python file  has been modified.")
        msgBox.setInformativeText("Do you want to save your changes?")
        msgBox.setDetailedText("Python is powerful... and fast; \nplays well with others;\n")

        fmbox = QFormLayout()
        fmbox.addRow("Question", self.questButton)
        fmbox.addRow("Infomation", self.infoButton)
        fmbox.addRow("Warning", self.warningButton)
        fmbox.addRow("Critical", self.criticalButton)
        fmbox.addRow("About", self.aboutButton)
        fmbox.addRow("Infomation2", self.infoButton2)

        self.resultLabel = QLabel("detailed message")
        hbox = QHBoxLayout()
        hbox.addWidget(self.resultLabel)
        fmbox.addRow(hbox)
        self.setLayout(fmbox)

    def show_message(self):
        if self.sender() == self.questButton:
            button = QMessageBox.question(self, "Question", "检测到程序有更新,是否安装最新版本?",
                                          QMessageBox.Ok | QMessageBox.Cancel, QMessageBox.Ok)

            if button == QMessageBox.Ok:
                self.resultLabel.setText("<h2>Question:<font color=red>  OK</font></h2>")
            elif button == QMessageBox.Cancel:
                self.resultLabel.setText("<h2>Question:<font color=red>  Cancel</font></h2>")
            else:
                return
        if self.sender() == self.infoButton:
            button = QMessageBox.information(self, "Information", "今天是国庆节,休息一下!",
                                             QMessageBox.Ok | QMessageBox.Cancel, QMessageBox.Ok)

            if button == self.infoButton.Ok:
                self.infoButton.setText("<h2>Question:<font color=red>  OK</font></h2>")
            elif button == QMessageBox.Cancel:
                self.infoButton.setText("<h2>Question:<font color=red>  Cancel</font></h2>")
            else:
                return
        if self.sender() == self.criticalButton:
            QMessageBox.critical(self, "Critical", "服务器宕机!")
            self.resultLabel.setText("<h2><font color=red>Critical</font></h2>")
        if self.sender() == self.aboutButton:
            QMessageBox.about(self, "About", "Copyright 2022 June.\n All Right reserved.")
            self.resultLabel.setText("About")
        if self.sender() == self.infoButton2:
            msgBox = QMessageBox()
            msgBox.setIcon(QMessageBox.Information)
            msgBox.setWindowTitle("The property-base API")
            msgBox.setText("The Python file has been modified.")
            msgBox.setInformativeText("Do you want to save your changes?")
            msgBox.setDetailedText("Python is powerful... and fast; \nplays well with others;\n \
            runs everywhere; \n is friendly & easy to learn; \nis Open.")
            msgBox.setStandardButtons(QMessageBox.Save | QMessageBox.Discard | QMessageBox.Cancel);
            msgBox.setDefaultButton(QMessageBox.Save)
            msgBox.exec()


if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = Window()
    window.show()
    sys.exit(app.exec_())
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值