首页 PyQt5视频教程 PyQt QMessageBox消息盒子展示对话框API大全
pay pay

PyQt QMessageBox消息盒子展示对话框API大全

日期: 七月 7, 2023, 1:14 p.m.
栏目: PyQt5视频教程
阅读: 70
作者: Python自学网-村长

摘要: QMessageBox是PyQt5中用于展示消息盒子的控件,常用于展示简单的提示、警告和错误信息等。其API包括以下内容:

QMessageBox是PyQt5中用于展示消息盒子的控件,常用于展示简单的提示、警告和错误信息等。其API包括以下内容:

  • about(parent: QWidget, title: str, text: str):展示一个关于信息对话框,包含标题和文本内容。
  • aboutQt(parent: QWidget, title: str = ''):展示一个关于Qt信息对话框,包含标题和Qt版本信息。
  • critical(parent: QWidget, title: str, text: str, buttons: Union[QMessageBox.StandardButtons, QMessageBox.StandardButton] = QMessageBox.Ok, defaultButton: QMessageBox.StandardButton = QMessageBox.NoButton) -> QMessageBox.StandardButton:展示一个严重错误信息对话框,包含标题、文本内容和一个或多个标准按钮。
  • information(parent: QWidget, title: str, text: str, buttons: Union[QMessageBox.StandardButtons, QMessageBox.StandardButton] = QMessageBox.Ok, defaultButton: QMessageBox.StandardButton = QMessageBox.NoButton) -> QMessageBox.StandardButton:展示一个普通提示信息对话框,包含标题、文本内容和一个或多个标准按钮。
  • question(parent: QWidget, title: str, text: str, buttons: Union[QMessageBox.StandardButtons, QMessageBox.StandardButton] = QMessageBox.StandardButtons(QMessageBox.Yes | QMessageBox.No), defaultButton: QMessageBox.StandardButton = QMessageBox.NoButton) -> QMessageBox.StandardButton:展示一个询问信息对话框,包含标题、文本内容和一个或多个标准按钮。
  • warning(parent: QWidget, title: str, text: str, buttons: Union[QMessageBox.StandardButtons, QMessageBox.StandardButton] = QMessageBox.Ok, defaultButton: QMessageBox.StandardButton = QMessageBox.NoButton) -> QMessageBox.StandardButton:展示一个警告信息对话框,包含标题、文本内容和一个或多个标准按钮。
  • setDetailedText(text: str):设置消息盒子的详细文本内容。

以下是一个演示程序,其中包含了上述API的使用:

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QMessageBox


class Example(QWidget):

    def __init__(self):
        super().__init__()

        # 设置窗口标题和大小
        self.setWindowTitle('QMessageBox Example')
        self.resize(400, 300)

        # 创建按钮并设置点击事件
        btn_about = QPushButton('About', self)
        btn_about.move(50, 100)
        btn_about.clicked.connect(self.show_about)

        btn_about_qt = QPushButton('About Qt', self)
        btn_about_qt.move(150, 100)
        btn_about_qt.clicked.connect(self.show_about_qt)

        btn_critical = QPushButton('Critical', self)
        btn_critical.move(250, 100)
        btn_critical.clicked.connect(self.show_critical)

        btn_information = QPushButton('Information', self)
        btn_information.move(50, 150)
        btn_information.clicked.connect(self.show_information)

        btn_question = QPushButton('Question', self)
        btn_question.move(150, 150)
        btn_question.clicked.connect(self.show_question)

        btn_warning = QPushButton('Warning', self)
        btn_warning.move(250, 150)
        btn_warning.clicked.connect(self.show_warning)

    def show_about(self):
        QMessageBox.about(self, 'About', 'This is an about message box')

    def show_about_qt(self):
        QMessageBox.aboutQt(self, 'About Qt')



def show_critical(self):
    reply = QMessageBox.critical(self, 'Critical', 'This is a critical message box',
                                  QMessageBox.Yes | QMessageBox.No, QMessageBox.Yes)
    if reply == QMessageBox.Yes:
        print('Yes clicked')
    else:
        print('No clicked')

    def show_information(self):
        QMessageBox.information(self, 'Information', 'This is an information message box')
    
    def show_question(self):
        reply = QMessageBox.question(self, 'Question', 'Do you like PyQt5?',
                                      QMessageBox.Yes | QMessageBox.No, QMessageBox.Yes)
        if reply == QMessageBox.Yes:
            print('Yes clicked')
        else:
            print('No clicked')
    
    def show_warning(self):
        QMessageBox.warning(self, 'Warning', 'This is a warning message box')
    
    def closeEvent(self, event):
        # 关闭所有打开的对话框
        reply = QMessageBox.question(self, 'Confirm Exit', 'Are you sure to exit?',
                                      QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
        if reply == QMessageBox.Yes:
            event.accept()
        else:
            event.ignore()

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

在演示程序中,我们创建了一个窗口和多个按钮,点击每个按钮会展示一个不同类型的消息盒子。其中关于信息和关于Qt信息对话框使用了about()和aboutQt()方法,警告信息和错误信息对话框使用了warning()和critical()方法,询问信息对话框使用了question()方法,普通提示信息对话框使用了information()方法。我们还重写了closeEvent()方法,确保程序关闭时关闭所有打开的对话框。

运行程序后,点击不同的按钮可以看到不同类型的消息盒子对话框。

 

原创视频,版权所有,未经允许,切勿转载,违者必究!
回顶部