pyqt5学习笔记(5) QDialog,QMessageBox

QDialog 学习笔记

QDialog继承自QWidget, 子类有 QColorDialog, QErrorMessage, QFileDialog, QFontDialog, QInputDialog, QMessageBox, QProgressDialog,QWizard

1 QDialog 类中常见方法

方法 简介
setWindowModality 设置模态

2 QMessageBox 类中常见方法

方法名 描述
about 弹出关于对话框
critical 弹出严重错误对话框
information 弹出消息对话框
question 弹出问答对话框
warning 弹出警告对话框

QMessageBox 标准按钮类型

类型 描述
QMessageBox.Yes 同意操作
QMessageBox.No 取消操作
QMessageBox.Ok 同意操作
QMessageBox.Cancel 取消操作
QMessageBox.Abort 终止操作
QMessageBox.Retry 重试操作
QMessageBox.Ignore 忽略操作

简单demo如下:

#!/usr/bin/env python3
# -*- coding:utf-8 -*-
# Author: Lancer  2020-05-08 08:45:33

from PyQt5.QtWidgets import QDialog,QMessageBox,QPushButton,QHBoxLayout,QVBoxLayout, QGridLayout,QWidget,QApplication
from PyQt5.QtCore import *

class  QDialogDemo(QWidget):
    def __init__(self):
        super(QDialogDemo, self).__init__()
        self.resize(800,600)
        self.setWindowTitle("QDialog学习笔记")
        mainlayout = QGridLayout()
        self.setLayout(mainlayout)
        self.btn01 = QPushButton("QDialog例子")
        self.meg_btn01 = QPushButton("关于对话框")
        self.meg_btn02 = QPushButton("严重错误对话框")
        self.meg_btn03 = QPushButton("消息对话框")
        self.meg_btn04 = QPushButton("问答对话框对话框")
        self.meg_btn05 = QPushButton("警告对话框")

        mainlayout.addWidget(self.btn01,0,0,1,1)# 第0行0列 占一行一列
        mainlayout.addWidget(self.meg_btn01, 1, 0, 1, 1)
        mainlayout.addWidget(self.meg_btn02, 1, 1, 1, 1)
        mainlayout.addWidget(self.meg_btn03, 1, 2, 1, 1)
        mainlayout.addWidget(self.meg_btn04, 1, 3, 1, 1)
        mainlayout.addWidget(self.meg_btn05, 1, 4, 1, 1)

        self.btn01.clicked.connect(self.showdialog)
        self.meg_btn01.clicked.connect(self.show_about)
        self.meg_btn02.clicked.connect(self.show_critical)
        self.meg_btn03.clicked.connect(self.show_information)
        self.meg_btn04.clicked.connect(self.show_question)
        self.meg_btn05.clicked.connect(self.show_warning)

    def  showdialog(self):
        dialog = QDialog()
        dialog.setWindowTitle("Dialog")
        dialog.resize(400,300)
        dialog.setWindowModality(Qt.ApplicationModal) #设置窗口的属性为ApplicationModal模态,用户只有关闭弹窗后,才能关闭主界面
        dialog.exec_()

    def  show_about(self):
        QMessageBox.about(self, "标题", "关于对话框消息正文")

    def  show_critical(self):
        QMessageBox.critical(self, "标题", "严重错误对话框消息正文", QMessageBox.Yes | QMessageBox.No|QMessageBox.Ignore, QMessageBox.Yes)

    def show_information(self):
        QMessageBox.information(self, '标题', '消息对话框正文', QMessageBox.Yes | QMessageBox.No, QMessageBox.Yes)

    def show_question(self):
        QMessageBox.question(self, "标题", "提问框消息正文", QMessageBox.Yes | QMessageBox.No, QMessageBox.Yes)

    def show_warning(self):
        QMessageBox.warning(self, "标题", "警告框消息正文", QMessageBox.Yes | QMessageBox.No, QMessageBox.Yes)

if __name__ == '__main__':
    import sys
    app = QApplication(sys.argv)
    test = QDialogDemo()
    test.show()
    sys.exit(app.exec_())

效果如下:
pyqt5学习笔记(5) QDialog,QMessageBox_第1张图片

你可能感兴趣的:(pyqt5)