【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)

单独测试

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

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所示问答对话框。单击“是”,将运行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()

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

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

在这里插入图片描述

图2

在这里插入图片描述

图3

在这里插入图片描述

图4

联系我

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值