- PyQt 教程
- PyQt - 首页
- PyQt - 简介
- PyQt - 环境配置
- PyQt - Hello World
- PyQt - 主要类
- PyQt - 使用 Qt Designer
- PyQt - 元对象
- PyQt 信号与槽
- PyQt - 信号和槽
- PyQt - 支持和信号
- PyQt - 未绑定和绑定信号
- PyQt - 使用 PyQtSignal 创建新信号
- PyQt - 连接、断开和发射信号
- PyQt - 槽装饰器
- PyQt - 槽连接
- PyQt 布局
- PyQt - 布局管理
- PyQt - QBoxLayout
- PyQt - QGridLayout
- PyQt - QFormLayout
- PyQt - QHBoxLayout
- PyQt - QVBoxLayout
- PyQt - QStackedLayout
- PyQt - QGraphicsGridLayout
- PyQt - QGraphicsAnchorLayout
- PyQt - QGraphicsLayout
- PyQt - QGraphicsLinearLayout
- PyQt 基本控件
- PyQt - 基本控件
- PyQt - QLabel 控件
- PyQt - QLineEdit 控件
- PyQt - QPushButton 控件
- PyQt - QRadioButton 控件
- PyQt - QCheckBox 控件
- PyQt - QComboBox 控件
- PyQt - QSpinBox 控件
- PyQt - QMessageBox
- PyQt - QDialogButtonBox 控件
- PyQt - QFontComboBox 控件
- PyQt - QDoubleSpinBox 控件
- PyQt - QToolBox 控件
- PyQt - QDialog 类
- PyQt - QMessageBox
- PyQt - 多文档界面
- PyQt - 拖放操作
- PyQt 绘图 API
- PyQt - 绘图 API
- PyQt 数据库
- PyQt - 数据库处理
- PyQt 基础知识
- PyQt - BrushStyle 常量
- PyQt - QClipboard
- PyQt - QPixmap 类
- PyQt 有用资源
- PyQt - 快速指南
- PyQt - 有用资源
- PyQt - 讨论
PyQt - QMessageBox
QMessageBox 是一个常用的模态对话框,用于显示一些信息消息,并可以选择性地要求用户通过点击其上的任何一个标准按钮来响应。每个标准按钮都有一个预定义的标题、一个角色,并返回一个预定义的十六进制数字。
与 QMessageBox 类关联的重要方法和枚举在以下表格中给出:
| 序号 | 方法及描述 |
|---|---|
| 1 |
setIcon() 显示与消息严重性相对应的预定义图标
|
| 2 |
setText() 设置要显示的主消息文本 |
| 3 |
setInformativeText() 显示附加信息 |
| 4 |
setDetailText() 对话框显示一个“详细信息”按钮。点击该按钮时显示此文本 |
| 5 |
setTitle() 显示对话框的自定义标题 |
| 6 |
setStandardButtons() 要显示的标准按钮列表。每个按钮都与以下内容相关联: QMessageBox.Ok 0x00000400 QMessageBox.Open 0x00002000 QMessageBox.Save 0x00000800 QMessageBox.Cancel 0x00400000 QMessageBox.Close 0x00200000 QMessageBox.Yes 0x00004000 QMessageBox.No 0x00010000 QMessageBox.Abort 0x00040000 QMessageBox.Retry 0x00080000 QMessageBox.Ignore 0x00100000 |
| 7 |
setDefaultButton() 将按钮设置为默认按钮。如果按下 Enter 键,它将发出 clicked 信号 |
| 8 |
setEscapeButton() 设置按钮,使其在按下 Escape 键时被视为已点击 |
QMessageBox 没有定义预定义图标,而是通过样式提供它们。默认值为无图标。消息框在所有情况下都保持相同。建议使用表格中建议的标准图标或平台的样式指南中的标准图标。
在这种情况下,没有一个预定义图标适合我们的消息框,我们可以通过设置图标像素图属性而不是图标属性来创建自定义图标。
示例 1
在本例中,说明了使用 QMessageBox 类及其方法的预定义图标。
import sys
from PyQt6.QtWidgets import QApplication, QWidget, QPushButton, QMessageBox
def window():
app = QApplication(sys.argv)
w = QWidget()
# Create buttons
b1 = QPushButton(w)
b1.setText("Information")
b1.move(45, 50)
b2 = QPushButton(w)
b2.setText("Warning")
b2.move(150, 50)
b3 = QPushButton(w)
b3.setText("Question")
b3.move(50, 150)
b4 = QPushButton(w)
b4.setText("Critical")
b4.move(150, 150)
# Connect button signals to functions
b1.clicked.connect(show_info_messagebox)
b2.clicked.connect(show_warning_messagebox)
b3.clicked.connect(show_question_messagebox)
b4.clicked.connect(show_critical_messagebox)
# Set window title
w.setWindowTitle("PyQt MessageBox")
# Show all widgets
w.show()
# Start the app
sys.exit(app.exec())
def show_info_messagebox():
msg = QMessageBox()
msg.setIcon(QMessageBox.Icon.Information)
msg.setText("Information")
msg.setWindowTitle("Information MessageBox")
msg.setStandardButtons(QMessageBox.StandardButton.Ok | QMessageBox.StandardButton.Cancel)
retval = msg.exec()
def show_warning_messagebox():
msg = QMessageBox()
msg.setIcon(QMessageBox.Icon.Warning)
msg.setText("Warning")
msg.setWindowTitle("Warning MessageBox")
msg.setStandardButtons(QMessageBox.StandardButton.Ok | QMessageBox.StandardButton.Cancel)
retval = msg.exec()
def show_question_messagebox():
msg = QMessageBox()
msg.setIcon(QMessageBox.Icon.Question)
msg.setText("Question")
msg.setWindowTitle("Question MessageBox")
msg.setStandardButtons(QMessageBox.StandardButton.Ok | QMessageBox.StandardButton.Cancel)
retval = msg.exec()
def show_critical_messagebox():
msg = QMessageBox()
msg.setIcon(QMessageBox.Icon.Critical)
msg.setText("Critical")
msg.setWindowTitle("Critical MessageBox")
msg.setStandardButtons(QMessageBox.StandardButton.Ok | QMessageBox.StandardButton.Cancel)
retval = msg.exec()
if __name__ == "__main__":
window()
输出
执行代码后,我们看到四个点击按钮以获取特定的图标。
示例 2
在以下示例中,点击顶层窗口上的按钮的信号,连接的函数将显示消息框对话框。
msg = QMessageBox()
msg.setIcon(QMessageBox.Information)
msg.setText("This is a message box")
msg.setInformativeText("This is additional information")
msg.setWindowTitle("MessageBox demo")
msg.setDetailedText("The details are as follows:")
setStandardButton() 函数显示所需的按钮。
msg.setStandardButtons(QMessageBox.Ok | QMessageBox.Cancel)
buttonClicked() 信号连接到一个槽函数,该函数识别信号源的标题。
msg.buttonClicked.connect(msgbtn)
该示例的完整代码如下:
import sys
from PyQt6.QtCore import *
from PyQt6.QtGui import *
from PyQt6.QtWidgets import *
def window():
app = QApplication(sys.argv)
w = QWidget()
b = QPushButton(w)
b.setText("Show message!")
b.move(100,50)
b.clicked.connect(showdialog)
w.setWindowTitle("PyQt MessageBox demo")
w.show()
sys.exit(app.exec_())
def showdialog():
msg = QMessageBox()
msg.setIcon(QMessageBox.Information)
msg.setText("This is a message box")
msg.setInformativeText("This is additional information")
msg.setWindowTitle("MessageBox demo")
msg.setDetailedText("The details are as follows:")
msg.setStandardButtons(QMessageBox.Ok | QMessageBox.Cancel)
msg.buttonClicked.connect(msgbtn)
retval = msg.exec_()
def msgbtn(i):
print ("Button pressed is:",i.text())
if __name__ == '__main__':
window()
输出
以上代码产生以下输出。当单击主窗口按钮时,将弹出消息框:
如果单击消息框上的“确定”或“取消”按钮,控制台将输出以下内容:
Button pressed is: OK Button pressed is: Cancel
疑问
信息
警告
严重错误