欢迎光临
我们一直在努力

PyQt5如何使用QMessageBox显示不同的对话框

这篇“PyQt5如何使用QMessageBox显示不同的对话框”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“PyQt5如何使用QMessageBox显示不同的对话框”文章吧。

使用QMessageBox显示不同的对话框

QMessageBox是通用的消息对话框,包括如下多种形式,不同的对话框有不同的图标和按钮,还可以根据自己需要微调样式。

# _*_ coding:utf-8 _*_
 
import sys
 
from PyQt6.QtWidgets import QWidget
from PyQt6.QtWidgets import QApplication
from PyQt6.QtWidgets import QMainWindow
from PyQt6.QtWidgets import QVBoxLayout
from PyQt6.QtWidgets import QPushButton
from PyQt6.QtWidgets import QMessageBox
from PyQt6.QtGui import QIcon
from PyQt6.QtCore import Qt
 
 
class DemoQMessageBox(QMainWindow):
    """主窗体界面"""
 
    def __init__(self):
        """构造函数"""
        super().__init__()
 
        self.setWindowTitle("MainWindow")
        self.setWindowIcon(QIcon(r"./res/20 (3).ico"))
        self.resize(300, 200)
        self.setMinimumSize(600, 400)
 
        self.center()
        self.initui()
 
    def initui(self):
        """初始函数"""
 
        self.vboxlayout = QVBoxLayout(self)
        self.vboxlayout.setAlignment(Qt.AlignmentFlag.AlignCenter)
        self.main_widget = QWidget()
        self.main_widget.setLayout(self.vboxlayout)
        self.setCentralWidget(self.main_widget)
 
        btns = ["关于对话框", "错误对话框", "警告对话框", "提问对话框", "消息对话框",]
 
        for btnname in btns:
            """添加button"""
            btn = QPushButton(btnname)
            self.vboxlayout.addWidget(btn)
            btn.clicked.connect(self.show_dialog)
 
    def center(self):
        """居中显示"""
        win_rect = self.frameGeometry()  # 获取窗口矩形
        screen_center = self.screen().availableGeometry().center()  # 屏幕中心
        win_rect.moveCenter(screen_center)      # 移动窗口矩形到屏幕中心
        self.move(win_rect.center())         # 移动窗口与窗口矩形重合
 
    def show_dialog(self):
        if type(self.sender()) is QPushButton:
            btn_text = self.sender().text()
            if btn_text == "关于对话框":
                QMessageBox.about(self, "关于", "这是关与对话框")
            elif btn_text == "错误对话框":
                QMessageBox.critical(self,"错误","程序发生了错误!",QMessageBox.StandardButton.Ignore | QMessageBox.StandardButton.Abort | QMessageBox.StandardButton.Retry,QMessageBox.StandardButton.Retry)
            elif btn_text == "警告对话框":
                QMessageBox.warning(
                    self, "警告", "余量不足。", QMessageBox.StandardButton.Ok, QMessageBox.StandardButton.Ok)
            elif btn_text == "提问对话框":
                QMessageBox.question(self, "提问", "是否取消" ,QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.No,QMessageBox.StandardButton.Yes)
            elif btn_text == "消息对话框":
                QMessageBox.information(
                    self, "消息", "这是通知消息", QMessageBox.StandardButton.Ok, QMessageBox.StandardButton.Ok)
 
 
if __name__ == "__main__":
    app = QApplication(sys.argv)
    view = DemoQMessageBox()
    view.show()
    sys.exit(app.exec())

结果:

点击不同按钮,显示不同的消息窗口:

关于对话框:

错误对话框:

警告对话框:

提问对话框:

消息对话框:

以上就是关于“PyQt5如何使用QMessageBox显示不同的对话框”这篇文章的内容,相信大家都有了一定的了解,希望小编分享的内容对大家有帮助,若想了解更多相关的知识内容,请关注云搜网行业资讯频道。

赞(0)
【声明】:本博客不参与任何交易,也非中介,仅记录个人感兴趣的主机测评结果和优惠活动,内容均不作直接、间接、法定、约定的保证。访问本博客请务必遵守有关互联网的相关法律、规定与规则。一旦您访问本博客,即表示您已经知晓并接受了此声明通告。