QMessageBox 改变标准按钮的文本

QMessageBox change text of standard button

我想使用 QMessageBox.Question 作为图标。但我想更改标准按钮的文本。我不希望按钮的文本是 "Yes" 和 "No"。我希望它们是 "Evet" 和 "Iptal"。这是我的代码。

choice = QtGui.QMessageBox.question(self, 'Kaydet!',
                                    'Kaydetmek İstediğinize Emin Misiniz?',
                                    QtGui.QMessageBox.Yes | QtGui.QMessageBox.No)

为此,您需要创建一个 QMessageBox 的实例并手动更改按钮的文本:

box = QtGui.QMessageBox()
box.setIcon(QtGui.QMessageBox.Question)
box.setWindowTitle('Kaydet!')
box.setText('Kaydetmek İstediğinize Emin Misiniz?')
box.setStandardButtons(QtGui.QMessageBox.Yes|QtGui.QMessageBox.No)
buttonY = box.button(QtGui.QMessageBox.Yes)
buttonY.setText('Evet')
buttonN = box.button(QtGui.QMessageBox.No)
buttonN.setText('Iptal')
box.exec_()

if box.clickedButton() == buttonY:
    # YES pressed
elif box.clickedButton() == buttonN:
    # NO pressed

Qt 为其库中使用的所有内置字符串提供翻译。您只需要为当前语言环境安装一个翻译器:

app = QtGui.QApplication(sys.argv)

translator = QtCore.QTranslator(app)
locale = QtCore.QLocale.system().name()
path = QtCore.QLibraryInfo.location(QtCore.QLibraryInfo.TranslationsPath)
translator.load('qt_%s' % locale, path)
app.installTranslator(translator)