当前位置: 首页 > news >正文

PyQt5之消息对话框

PyQt5之消息对话框

  • 严重性级别和图标
  • 常用的消息弹窗
  • 常用标准按钮
  • QMessageBox两套接口
    • static functions
      • about类型方法
      • 其它类型方法
    • 基于属性的API
  • 举例

消息对话框主要涉及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_())

相关文章:

  • java计算机毕业设计校友闲置书籍管理平台源代码+数据库+系统+lw文档
  • Interactron | 体现自适应的目标检测器
  • javaEE---CSS
  • PCIE操作基础原理
  • Windows系统SVG图片预览插件
  • 2022.10.1模拟赛
  • 西瓜书研读——第三章 线性模型: 线性判别分析 LDA
  • 云计算概论 --云安全机制
  • java计算机毕业设计企业公开招聘系统源程序+mysql+系统+lw文档+远程调试
  • 谷粒学院16万字笔记+1600张配图(十五)——微信扫码登录
  • 详述进程概念【Linux】
  • VVC系列(一)VTM下载编译
  • LeetCode50天刷题计划第二季(Day 7 — 验证二叉搜索树(15.00-16.00
  • 在 IDEA 中用 Nacos2.1.0 源码启动集群模式并调试
  • 前端毕业设计:Nodejs+Vue菜鸟驿站仓库管理系统的设计与实现
  • 2018以太坊智能合约编程语言solidity的最佳IDEs
  • Android Studio:GIT提交项目到远程仓库
  • Android开发 - 掌握ConstraintLayout(四)创建基本约束
  • Essential Studio for ASP.NET Web Forms 2017 v2,新增自定义树形网格工具栏
  • js中的正则表达式入门
  • LeetCode18.四数之和 JavaScript
  • miniui datagrid 的客户端分页解决方案 - CS结合
  • mysql外键的使用
  • PHP的Ev教程三(Periodic watcher)
  • Promise面试题2实现异步串行执行
  • Redis学习笔记 - pipline(流水线、管道)
  • swift基础之_对象 实例方法 对象方法。
  • Webpack 4 学习01(基础配置)
  • Zsh 开发指南(第十四篇 文件读写)
  • 成为一名优秀的Developer的书单
  • 大数据与云计算学习:数据分析(二)
  • - 概述 - 《设计模式(极简c++版)》
  • 紧急通知:《观止-微软》请在经管柜购买!
  • 说说动画卡顿的解决方案
  • 在GitHub多个账号上使用不同的SSH的配置方法
  • Spring Batch JSON 支持
  • 完善智慧办公建设,小熊U租获京东数千万元A+轮融资 ...
  • ​香农与信息论三大定律
  • (六)vue-router+UI组件库
  • (七)Knockout 创建自定义绑定
  • (原創) X61用戶,小心你的上蓋!! (NB) (ThinkPad) (X61)
  • .axf 转化 .bin文件 的方法
  • .Net - 类的介绍
  • .NET C# 使用 SetWindowsHookEx 监听鼠标或键盘消息以及此方法的坑
  • .NET delegate 委托 、 Event 事件,接口回调
  • .NET 编写一个可以异步等待循环中任何一个部分的 Awaiter
  • .NET 解决重复提交问题
  • .net专家(张羿专栏)
  • .py文件应该怎样打开?
  • ?php echo $logosrc[0];?,如何在一行中显示logo和标题?
  • @EnableAsync和@Async开始异步任务支持
  • @RequestBody与@ResponseBody的使用
  • [android] 切换界面的通用处理
  • [AutoSAR 存储] 汽车智能座舱的存储需求
  • [ICCV2017]Neural Person Search Machines