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

PyQt基础_011_对话框类控件QMessage

基本功能

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_())

增加图标显示

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_())

按钮事件

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_())


http://www.kler.cn/a/152219.html

相关文章:

  • kubernetes(k8s)容器内无法连接同所绑定的Service ClusterIP问题记录
  • 指针(2)
  • windows 查看mysql的错误日志
  • Android 11.0 修改Android系统的通知自动成组的数量
  • Spring底层篇
  • uniapp如何与原生应用进行混合开发?
  • Docker stats 命令
  • Javaweb之Vue组件库Element案例的详细解析
  • pthread 使用入门
  • 【猜数字游戏】用wxPython实现:基本的游戏框架 + 简单的图形用户界面
  • Constraintlayout
  • numpy知识库:深入理解numpy.resize函数和数组的resize方法
  • List系列集合
  • 道路病害检测数据集RDD2022的标签映射关系【参考自官网给出的label_map.pbtxt文件,附查看代码】
  • 华为云cce容器管理中的调度策略作用
  • 在Pycharm中创建项目新环境,安装Pytorch
  • Elasticsearch:对时间序列数据流进行降采样(downsampling)
  • 李宏毅2020机器学习课程笔记(二)- 深度学习
  • 建堆的时间复杂度和堆排序
  • (C++20) constinit常量初始化