• 【python】PySide中QMessageBox设置中文按钮及使用


    PyQt、PySide使用QMessageBox的时候会发现按钮都是英文的,对于中文的应用软件来说会降低使用体验。本文将以问答对话框为例,介绍如何设置中文按钮,以及如何使用。

    实验环境

    本文实验环境为:Windows 10,Python 3.8,PySide==6.5.0。其他版本的PySide或PyQt也可以使用类似的方法进行设置和使用。

    问答(Question)

    如下代码定义了一个汉化后问答对话框的类,默认Yes按钮显示“是”,No按钮显示“否”。可以通过yes_button_set_textno_button_set_text方法来覆盖默认的Yes、No按钮显示。

    from PySide6.QtCore import Qt
    from PySide6.QtWidgets import QMessageBox
    
    
    class QuestionMsgBox(QMessageBox):
        def __init__(self, title, text):
            super().__init__()
            self.setWindowModality(Qt.WindowModality.ApplicationModal)
            self.setWindowTitle(title)
            self.setIcon(QMessageBox.Icon.Question)
            self.setText(text)
            self.setStandardButtons(QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.No)
            self.yes_button = self.button(QMessageBox.StandardButton.Yes)
            self.no_button = self.button(QMessageBox.StandardButton.No)
            self.yes_button.setText('是')  # 默认Yes按钮为“是”
            self.no_button.setText('否')  # 默认No按钮为“否”
    
    	def yes_button_set_text(self, text):
    		self.yes_button.setText(text)
    
        def no_button_set_text(self, text):
           self.no_button.setText(text)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    单独测试

    接下来将单独测试问答对话框,代码如下所示。

    from PySide6.QtCore import Qt
    from PySide6.QtWidgets import QMessageBox
    
    
    class QuestionMsgBox(QMessageBox):
        def __init__(self, title, text):
            super().__init__()
            self.setWindowModality(Qt.WindowModality.ApplicationModal)
            self.setIcon(QMessageBox.Icon.Question)
            self.setWindowTitle(title)
            self.setText(text)
            self.setStandardButtons(QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.No)
            self.yes_button = self.button(QMessageBox.StandardButton.Yes)
            self.no_button = self.button(QMessageBox.StandardButton.No)
            self.yes_button.setText('是')
            self.no_button.setText('否')
    
        def yes_button_set_text(self, text):
            self.yes_button.setText(text)
    
        def no_button_set_text(self, text):
            self.no_button.setText(text)
    
    
    def yes_func():
        print('Pressed yes button')
    
    
    def no_func():
        print('Pressed no button')
    
    
    def main():
        app = QApplication([])
        msg_box = QuestionMsgBox('标题', '确认内容?')
        msg_box.yes_button.clicked.connect(yes_func)
        msg_box.no_button.clicked.connect(no_func)
        msg_box.show()
        app.exec()
    
    
    if __name__ == '__main__':
        main()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43

    程序运行后,显示如图1所示问答对话框。单击“是”,将运行yes_func()函数;单击“否”,将运行no_func()函数。

    需要注意的是:在单独测试对话框的时候,main()函数中需使用msg_box.show()方法来显示对话框,而不能使用msg_box.exec()方法。否则,程序将无法结束,需强行关闭程序。

    在这里插入图片描述

    图1

    应用测试

    最后是在应用中测试问答对话框,代码如下所示。

    from PySide6.QtCore import Qt
    from PySide6.QtGui import QScreen, QFont
    from PySide6.QtWidgets import QApplication, QMessageBox, QWidget, QVBoxLayout, QHBoxLayout, QPushButton, QLabel, QLineEdit
    
    
    class QuestionMsgBox(QMessageBox):
        def __init__(self, title, text):
            super().__init__()
            self.setWindowModality(Qt.WindowModality.ApplicationModal)
            self.setIcon(QMessageBox.Icon.Question)
            self.setWindowTitle(title)
            self.setText(text)
            self.setStandardButtons(QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.No)
            self.yes_button = self.button(QMessageBox.StandardButton.Yes)
            self.no_button = self.button(QMessageBox.StandardButton.No)
            self.yes_button.setText('是')
            self.no_button.setText('否')
    
        def yes_button_set_text(self, text):
            self.yes_button.setText(text)
    
        def no_button_set_text(self, text):
            self.no_button.setText(text)
    
    
    class AppGUI(QWidget):
        def __init__(self):
            super().__init__()
            # set window
            self.setWindowTitle('应用窗口')
            win_width = 290
            win_height = 80
            self.setFixedWidth(win_width)
            self.setFixedHeight(win_height)
            screen_size = QScreen.availableGeometry(QApplication.primaryScreen())
            win_x = (screen_size.width() - win_width) / 2
            win_y = (screen_size.height() - win_height) / 2
            self.move(win_x, win_y)
            self.setFont(QFont('Microsoft YaHei UI', 12))
            # set layout
            self.win_layout = QVBoxLayout()
            self.main_layout = QHBoxLayout()
            self.setLayout(self.win_layout)
            # widget
            self.open_button = QPushButton('打开问答对话框')
            self.open_button.clicked.connect(self.open_question_msg_box)
            self.win_layout.addWidget(self.open_button)
            self.win_layout.addLayout(self.main_layout)
            self.label = QLabel('单击的按钮:')
            self.main_layout.addWidget(self.label)
            self.entry = QLineEdit()
            self.main_layout.addWidget(self.entry)
    
        def open_question_msg_box(self):
            msg_box = QuestionMsgBox('标题', f'确认内容?')
            msg_box.yes_button.clicked.connect(self.yes_func)
            msg_box.no_button.clicked.connect(self.no_func)
            msg_box.exec()
    
        def yes_func(self):
            self.entry.setText('是')
    
        def no_func(self):
            self.entry.setText('否')
    
    
    def main():
        app = QApplication([])
        app_gui = AppGUI()
        app_gui.show()
        app.exec()
    
    
    if __name__ == '__main__':
        main()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75

    程序运行后,打开如图2所示界面。单击“打开问答对话框”,打开如图1所示问答对话框,单击问答对话框的按钮后,将会将所单击的按钮文本打印在输入框中。若单击“是”,则如图3所示;若单击“否”,则如图4所示。

    需要注意的是:在应用测试中使用对话框的时候,AppGUI类中的open_question_msg_box方法需使用msg_box.exec()方法,而不能使用msg_box.show()方法。否则,对话框将会一闪而过。

    在这里插入图片描述

    图2

    在这里插入图片描述

    图3

    在这里插入图片描述

    图4

    联系我

    如有疑问,邮件是最快联系我的方式:wm_chen@yeah.net

  • 相关阅读:
    Spring 自定义注解 面向切面编程
    竞赛 题目:基于卷积神经网络的手写字符识别 - 深度学习
    算法提升 (三)基础数据结构
    PE结构学习(6)_重定位表
    基于.Net+SWEBUI开发的开源WMS仓库管理系统
    算法之斐波那契数列
    【Java】字节流、字符流、IO异常、属性集
    redis持久化和Redis事务
    智慧校园信息化管理系统的方案设计与实施
    室温离子液体1-丁基-3-甲基咪唑六氟磷酸盐([EMIM] PF6)科研试剂
  • 原文地址:https://blog.csdn.net/weixin_37577134/article/details/132742790