PyQt - QMessageBox



QMessageBox 是一个常用的模态对话框,用于显示一些信息消息,并可以选择性地要求用户通过点击其上的任何一个标准按钮来响应。每个标准按钮都有一个预定义的标题、一个角色,并返回一个预定义的十六进制数字。

与 QMessageBox 类关联的重要方法和枚举在以下表格中给出:

序号 方法及描述
1

setIcon()

显示与消息严重性相对应的预定义图标

Question 疑问

Information 信息

Warning 警告

Critical 严重错误

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

输出

执行代码后,我们看到四个点击按钮以获取特定的图标。

QMessageBox Output example one

示例 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()

输出

以上代码产生以下输出。当单击主窗口按钮时,将弹出消息框:

QMessageBox Output

如果单击消息框上的“确定”或“取消”按钮,控制台将输出以下内容:

Button pressed is: OK
Button pressed is: Cancel
广告

© . All rights reserved.