PyQt基础_011_对话框类控件QMessage

基本功能

python 复制代码
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *

class WinForm( QWidget): 
    def __init__(self): 
        super(WinForm,self).__init__() 
        self.setWindowTitle("QMessageBox") 
        self.resize(300, 100) 
        self.myButton = QPushButton(self) 
        self.myButton.setText("点击弹出消息框") 
        self.myButton.clicked.connect(self.msg) 

    def msg(self): 
        # 使用infomation信息框  
        reply = QMessageBox.information(self, "title", "hello world", QMessageBox.Yes | QMessageBox.No , QMessageBox.Yes ) 
        print( reply )

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

增加图标显示

python 复制代码
import sys

from PyQt5.Qt import *

"""
QMessageBox.Icon
QMessageBox.NoIcon
QMessageBox.Question
QMessageBox.Information
QMessageBox.Warning
QMessageBox.Critical
"""

class Window(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("QMessageBox")
        self.resize(500, 500)
        self.move(400, 250)
        self.setup_ui()

    def setup_ui(self):
        mb = QMessageBox(self)
        # mb = QMessageBox(QMessageBox.Critical, '窗口标题', '主标题', QMessageBox.Ok | QMessageBox.Discard, self)
        # mb.setModal(False)  # 强行设置为非模态
        # mb.setWindowModality(Qt.NonModal)  # 强行设置为非模态
         # mb.show()  # 一定为模态,即使使用show()方法也仍为模态

        mb.setWindowTitle("message")

        # 设置图标
        # mb.setIcon(QMessageBox.Information)  # 设置标准图标
        mb.setIconPixmap(QPixmap("./resource/python_96px.ico").scaled(40, 40)) # 设置自定义图标

        # 设置主标题
        mb.setText("<h3>hello world</h3>") # 设置主标题
        # mb.setTextFormat(Qt.PlainText)  # 设置主标题文本格式
        # mb.setTextFormat(Qt.RichText)
        mb.setTextFormat(Qt.AutoText)

        # 设置提示文本(副标题)
        mb.setInformativeText("tips") # 设置副标题
        # print(mb.informativeText())

        # 设置详细文本
        mb.setDetailedText("this is a message") # 设置详情(不支持富文本)
        # print(mb.detailedText())

        # 设置复选框
        mb.setCheckBox(QCheckBox("下次不再提醒", mb)) # 设置复选框
        mb.checkBox().toggled.connect(lambda: print("clicked"))

        mb.open()

if __name__ == "__main__":
    app = QApplication(sys.argv)

    window = Window()
    window.show()

    sys.exit(app.exec_())

按钮事件

python 复制代码
import sys

from PyQt5.Qt import *

class Window(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("QMessageBox-按钮操作")
        self.resize(500, 500)
        self.move(400, 250)
        self.setup_ui()

    def setup_ui(self):
        mb = QMessageBox(self)
        mb.setWindowTitle("message")

        # 添加移除按钮
        # mb.addButton(QPushButton("Yes!Yes!Yes!", mb), QMessageBox.YesRole)
        yes_btn = mb.addButton("Yes", QMessageBox.YesRole)
        # mb.removeButton(yes_btn)  # 移除按钮

        # 设置标准按钮
        mb.setStandardButtons(QMessageBox.Apply | QMessageBox.No)

        # 默认按钮(默认哪个按钮获取到焦点)
        mb.setDefaultButton(QMessageBox.Apply)

        # 退出按钮(按下键盘Esc键时激活的按钮)
        mb.setEscapeButton(QMessageBox.No)

        # 按钮信号槽
        apply_btn = mb.button(QMessageBox.Apply) # 获取按钮对象

        def test(btn):
            if btn == yes_btn:
                print("yes clicked")
            elif btn == apply_btn:
                print("apply clicked")

            role = mb.buttonRole(btn)
            if role == QMessageBox.YesRole:
                 print("yes clicked")
            elif role == QMessageBox.NoRole:
                print("no clicked")

        mb.buttonClicked.connect(test)

        mb.open()

if __name__ == "__main__":
    app = QApplication(sys.argv)

    window = Window()
    window.show()

    sys.exit(app.exec_())
相关推荐
民乐团扒谱机2 天前
【超详细】PyQt6 实现 AU 风格波形图编辑器:多级缩放与采样点逐级渲染,附全过程代码
开发语言·python·编辑器·pyqt·audition·时域·频谱图
典典分享指南3 天前
Excel SUMIFS 跨表汇总实战:多表条件求和的完整指南
django·flask·numpy·pyqt·fastapi
狮子雨恋6 天前
【无标题】
pyqt
微小冷8 天前
pyqt+matplotlib多进程图像刷新
pyqt·matplotlib·进程通信·多进程·queue·process
千里码aicood9 天前
PyQt基于卷积神经网络的智慧校园的设计与实现
人工智能·cnn·pyqt
千里码aicood9 天前
基于PyQt心理咨询机器人智能系统的设计与实现
机器人·pyqt
典典分享指南9 天前
Windows 自动化踩坑实录
python·intellij-idea·pyqt·emacs·visual studio
深蓝海拓12 天前
基于QtPy (PySide6) 的PLC-HMI工程实战记录(五)为当前动作画面的信号连接变量
网络·笔记·python·学习·pyqt
智购科技自动售卖机厂家15 天前
2026自动售货机OTA升级系统设计:从全量升级到差分升级的带宽优化工程实践~YH
大数据·人工智能·numpy·pyqt·fastapi
【赫兹威客】浩哥15 天前
【无标题】
python·spring·django·pyqt