锋哥原创的PyQt6视频教程:
对话框使用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")