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

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 sysfrom 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 sysfrom 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.lryc.cn/news/254879.html

相关文章:

  • SpringMvc集成开源流量监控、限流、熔断降级、负载保护组件Sentinel | 京东云技术团队
  • [LeetCode] 12. 整数转罗马数字
  • 深入了解Java Period类,对时间段的精确控制
  • 企业软件的分类有哪些|app小程序定制开发
  • 选择更好的Notes索引附件方式
  • Vue混淆与还原
  • R语言单因素方差分析+差异显著字母法标注+逐行详细解释
  • linux 消息队列apache-activemq服务的安装
  • 前端数据加密相关问题
  • Vue3中reactive和ref对比
  • 【尘缘送书第五期】Java程序员:学习与使用多线程
  • Linux C语言 34-库封装操作
  • JavaWeb-Tomcat
  • k8s之Pod常用命令详解、镜像拉取策略(imagePullPolicy)
  • Spark低版本适配Celeborn
  • idea报错:Error:java: 不允许在使用 -release 时从系统模块 java.xml 导出程序包?
  • Vector Quantized Diffusion Model for Text-to-Image Synthesis
  • solidity实现ERC1155多代币标准
  • 10、外观模式(Facade Pattern,不常用)
  • <软考>软件设计师-3程序设计语言基础(总结)
  • C/C++---------------LeetCode第278. 第一个错误的版本
  • C语言三种循环输出9*9乘法表
  • IntelliJ IDEA 之初体验
  • java中synchronized和Lock的区别是什么?
  • ESP32-Web-Server编程-通过 Base64 编码在网页中插入图片
  • 聊一聊大模型 | 京东云技术团队
  • pandas空格及网页空格符NBSP替换处理
  • 智能优化算法应用:基于战争策略算法无线传感器网络(WSN)覆盖优化 - 附代码
  • 数据结构和算法-栈
  • C#基础与进阶扩展合集-进阶篇(持续更新)