【PyQt5】{15} —— 汉化对话框的按钮

汉化对话框的按钮

# -*- coding: utf-8 -*-
"""
Created on Sat May  9 14:46:56 2020

@author: Giyn
"""

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QMessageBox


class Simple_Window(QWidget):
    def __init__(self):
        super(Simple_Window, self).__init__() # 使用super函数可以实现子类使用父类的方法
        
        self.button = QPushButton("Question", self)
        self.button.clicked.connect(self.show_msg_box)


    def show_msg_box(self, button):
        msg_box = QMessageBox(self) # 实例化一个QMessageBox对象
        msg_box.setWindowTitle("Question") # 设置对话框的标题
        msg_box.setText("Do you want to save it?") # 设置对话框的内容
        msg_box.setIcon(QMessageBox.Question)
        
        # 调用addButton方法传入字符串,确定按钮扮演的角色
        msg_box.addButton("是", QMessageBox.YesRole)
        msg_box.addButton("否", QMessageBox.NoRole)
        msg_box.exec()
        
        if msg_box.clickedButton().text() == "是": # clickedButton方法返回用户按下的按钮
            self.close()
            print("保存成功!")
        else:
            self.close()
            print("退出程序!")


if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = Simple_Window()
    window.show()
    sys.exit(app.exec())

Output:

After clicking “是”:
After clicking “否”:

QMessageBox对话框的图标参数:

QMessageBox.NoIcon
QMessageBox.Question
QMessageBox.Information
QMessageBox.Warning
QMessageBox.Critical


QMessageBox中常见的按钮角色:

QMessageBox::AcceptRole − > -> > O K OK OK
QMessageBox::RejectRole − > -> > C a n c e l Cancel Cancel
QMessageBox::YesRole − > -> > Y e s Yes Yes
QMessageBox::NoRole − > -> > N o No No


clickedButton 方法返回用户按下的按钮


Reference:https://study.163.com/course/courseLearn.htm?courseId=1208995818

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值