PyQt6 QMessageBox对话框控件

2023-12-17 20:12:25

锋哥原创的PyQt6视频教程:

2024版 PyQt6 Python桌面开发 视频教程(无废话版) 玩命更新中~_哔哩哔哩_bilibili2024版 PyQt6 Python桌面开发 视频教程(无废话版) 玩命更新中~共计48条视频,包括:2024版 PyQt6 Python桌面开发 视频教程(无废话版) 玩命更新中~、第2讲 PyQt6库和工具库QTDesigner安装与配置、第3讲 PyQt6第一个程序HelloWorld实现等,UP主更多精彩视频,请关注UP账号。icon-default.png?t=N7T8https://www.bilibili.com/video/BV11C4y1P7fj/

对话框使用QMessageBox类表示,PyQt6内置5种不同类型的对话框,分别是消息对话框、问答对话框、警告对话框、错误对话框和关于对话框,它们的主要区别在于,弹出的对话框中的图标不同。

说明对话框类型
QMessageBox.information()消息对话框
OMessageBox.critical()错误对话框
QMessageBox.question()问答对话框
QMessageBox.about()关于对话框
QMessageBox.warning()警告对话框

UI生成参考代码:

# Form implementation generated from reading ui file 'QMessageBox对话框控件.ui'
#
# Created by: PyQt6 UI code generator 6.4.2
#
# WARNING: Any manual changes made to this file will be lost when pyuic6 is
# run again.  Do not edit this file unless you know what you are doing.


from PyQt6 import QtCore, QtGui, QtWidgets


class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(236, 331)
        self.verticalLayoutWidget = QtWidgets.QWidget(parent=Form)
        self.verticalLayoutWidget.setGeometry(QtCore.QRect(20, 10, 191, 311))
        self.verticalLayoutWidget.setObjectName("verticalLayoutWidget")
        self.verticalLayout = QtWidgets.QVBoxLayout(self.verticalLayoutWidget)
        self.verticalLayout.setContentsMargins(0, 0, 0, 0)
        self.verticalLayout.setObjectName("verticalLayout")
        self.pushButton_3 = QtWidgets.QPushButton(parent=self.verticalLayoutWidget)
        self.pushButton_3.setObjectName("pushButton_3")
        self.verticalLayout.addWidget(self.pushButton_3)
        self.pushButton_2 = QtWidgets.QPushButton(parent=self.verticalLayoutWidget)
        self.pushButton_2.setObjectName("pushButton_2")
        self.verticalLayout.addWidget(self.pushButton_2)
        self.pushButton_4 = QtWidgets.QPushButton(parent=self.verticalLayoutWidget)
        self.pushButton_4.setObjectName("pushButton_4")
        self.verticalLayout.addWidget(self.pushButton_4)
        self.pushButton_5 = QtWidgets.QPushButton(parent=self.verticalLayoutWidget)
        self.pushButton_5.setObjectName("pushButton_5")
        self.verticalLayout.addWidget(self.pushButton_5)
        self.pushButton = QtWidgets.QPushButton(parent=self.verticalLayoutWidget)
        self.pushButton.setObjectName("pushButton")
        self.verticalLayout.addWidget(self.pushButton)

        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle(_translate("Form", "Form"))
        self.pushButton_3.setText(_translate("Form", "消息对话框"))
        self.pushButton_2.setText(_translate("Form", "错误对话框"))
        self.pushButton_4.setText(_translate("Form", "问答对话框"))
        self.pushButton_5.setText(_translate("Form", "关于对话框"))
        self.pushButton.setText(_translate("Form", "警告对话框"))

Main测试代码:

"""
    python加载ui文件
    作者 : 小锋老师
    官网 : www.python222.com
"""
import sys

from PyQt6.QtWidgets import QApplication, QWidget, QPushButton, QMessageBox
from PyQt6 import uic, QtGui


def showInfo():
    QMessageBox.information(None, '消息对话框标题', '消息对话框内容', QMessageBox.StandardButton.Ok)


def showCritical():
    QMessageBox.critical(None, '错误对话框标题', '错误对话框内容', QMessageBox.StandardButton.Ok)


def showQuestion():
    QMessageBox.question(None, '问答对话框标题', '问答对话框内容', QMessageBox.StandardButton.Ok)


def showWarm():
    QMessageBox.warning(None, '警告对话框标题', '警告对话框内容', QMessageBox.StandardButton.Ok)


def showAbout():
    # 关于对话框不能加按钮
    QMessageBox.about(None, '关于对话框标题', '关于对话框内容')


if __name__ == '__main__':
    app = QApplication(sys.argv)
    ui = uic.loadUi("./QMessageBox对话框控件.ui")
    widget: QWidget = ui.verticalLayoutWidget
    info_btn: QPushButton = ui.pushButton_3
    critical_btn: QPushButton = ui.pushButton_2
    question_btn: QPushButton = ui.pushButton_4
    about_btn: QPushButton = ui.pushButton_5
    warning_btn: QPushButton = ui.pushButton
    info_btn.clicked.connect(showInfo)
    critical_btn.clicked.connect(showCritical)
    question_btn.clicked.connect(showQuestion)
    about_btn.clicked.connect(showAbout)
    warning_btn.clicked.connect(showWarm)

    ui.show()

    sys.exit(app.exec())

对话框的常用按钮类型有:

按钮类型说明
QMessageBox.StandardButton.Ok同意操作
QMessageBox.StandardButton.Apply应用操作
QMessageBox.StandardButton.Ignore忽略操作
QMessageBox.StandardButton.Yes同意操作
QMessagcBox.StandardButton.Close关闭操作
QMessageBox.StandardButton.No取消操作
OMessageBox.StandardButton.Cancel取消操作
OMessageBox.StandardButton.Abort终止操作
OMessageBox.StandardButton.Open打开操作
QMessageBox.StandardButton.Retry重试操作
OMessageBox.StandardButton.Save保存操作

多个按钮用 '|' 分隔

QMessageBox.information(None, '消息对话框标题', '消息对话框内容',
                            QMessageBox.StandardButton.Ok | QMessageBox.StandardButton.No)

获取对话框点击结果:

result = QMessageBox.information(None, '消息对话框标题', '消息对话框内容',
                                     QMessageBox.StandardButton.Ok | QMessageBox.StandardButton.No)
    if result == QMessageBox.StandardButton.Ok:
        print("点了OK")
    elif result == QMessageBox.StandardButton.No:
        print("点了No")

文章来源:https://blog.csdn.net/caoli201314/article/details/135047808
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。