【QTdesigner】课时44.显示不同类型的消息对话框(QMessageBox)【pyqt5+QTdesigner模式】

>>> 点击进入:pyqt5专栏<<<

老师原课件下载地址:
有积分的朋友,支持下我,打赏也OK。
不下载也可以,我每节课会吧代码贴出来


视频链接

P43课时44.显示不同类型的消息对话框


课程重点

在这里插入图片描述


代码位置

在这里插入图片描述


完整代码

'''

消息对话框:QMessageBox


1. 关于对话框
2. 错误对话框
3. 警告对话框
4. 提问对话框
5. 消息对话框


有2点差异
1. 显示的对话框图标可能不同
2. 显示的按钮是不一样的

'''
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
class QMessageBoxDemo(QWidget):
    def __init__(self):
        super(QMessageBoxDemo,self).__init__()
        self.initUI()
    def initUI(self):
        self.setWindowTitle('QMessageBox案例')
        self.resize(300,400)

        layout = QVBoxLayout()
        self.button1 = QPushButton()
        self.button1.setText('显示关于对话框')
        self.button1.clicked.connect(self.showDialog)

        self.button2 = QPushButton()
        self.button2.setText('显示消息对话框')
        self.button2.clicked.connect(self.showDialog)

        self.button3 = QPushButton()
        self.button3.setText('显示警告对话框')
        self.button3.clicked.connect(self.showDialog)


        self.button4 = QPushButton()
        self.button4.setText('显示错误对话框')
        self.button4.clicked.connect(self.showDialog)

        self.button5 = QPushButton()
        self.button5.setText('显示提问对话框')
        self.button5.clicked.connect(self.showDialog)


        layout.addWidget(self.button1)
        layout.addWidget(self.button2)
        layout.addWidget(self.button3)
        layout.addWidget(self.button4)
        layout.addWidget(self.button5)
        self.setLayout(layout)

    def showDialog(self):
        text = self.sender().text()
        if text == '显示关于对话框':
            QMessageBox.about(self,'关于','这是一个关于对话框')
        elif text == '显示消息对话框':
            reply = QMessageBox.information(self,'消息','这是一个消息对话框', QMessageBox.Yes | QMessageBox.No,QMessageBox.Yes)
            print(reply == QMessageBox.Yes)
        elif text == '显示警告对话框':
            QMessageBox.warning(self,'警告','这是一个警告对话框',QMessageBox.Yes | QMessageBox.No,QMessageBox.Yes)
        elif text == '显示错误对话框':
            QMessageBox.critical(self,'警告','这是一个警告对话框',QMessageBox.Yes | QMessageBox.No,QMessageBox.Yes)
        elif text == '显示提问对话框':
            QMessageBox.question(self,'警告','这是一个警告对话框',QMessageBox.Yes | QMessageBox.No,QMessageBox.Yes)

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

遇到的问题

在这里插入图片描述
这里连续调用5次链接,我可是我不知道如何遍历所有按钮类。看看后面会不会学到,尴尬…


改写效果展示

在这里插入图片描述


改写注意点

毫无难度。。


改写代码(QTdesigner模式)

# -*- coding:utf-8 -*-
'''
@Author: knocky
@Blog: https://blog.youkuaiyun.com/zzx188891020
@E-mail: 188891020@qq.com
@File: class_X.py
@CreateTime: 2020/5/11 23:36
'''

import sys
from PyQt5.QtWidgets import QApplication,QMessageBox
from PyQt5 import uic


class my_form():
    def __init__(self):
        super().__init__()
        self.ui = uic.loadUi("../ui_package/class44.ui")
        self.ui.button1.clicked.connect(self.showDialog)
        self.ui.button2.clicked.connect(self.showDialog)
        self.ui.button3.clicked.connect(self.showDialog)
        self.ui.button4.clicked.connect(self.showDialog)
        self.ui.button5.clicked.connect(self.showDialog)


    def showDialog(self):
        text = self.ui.sender().text()
        if text == '显示关于对话框':
            QMessageBox.about(self.ui,'关于','这是一个关于对话框')
        elif text == '显示消息对话框':
            reply = QMessageBox.information(self.ui,'消息','这是一个消息对话框', QMessageBox.Yes | QMessageBox.No,QMessageBox.Yes)
            print(reply == QMessageBox.Yes)
        elif text == '显示警告对话框':
            QMessageBox.warning(self.ui,'警告','这是一个警告对话框',QMessageBox.Yes | QMessageBox.No,QMessageBox.Yes)
        elif text == '显示错误对话框':
            QMessageBox.critical(self.ui,'警告','这是一个警告对话框',QMessageBox.Yes | QMessageBox.No,QMessageBox.Yes)
        elif text == '显示提问对话框':
            QMessageBox.question(self.ui,'警告','这是一个警告对话框',QMessageBox.Yes | QMessageBox.No,QMessageBox.Yes)



if __name__ == '__main__':
    app = QApplication(sys.argv)
    main = my_form()
    main.ui.show()
    sys.exit(app.exec_())

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值