【问题标题】:QMessageBox change text of standard buttonQMessageBox 更改标准按钮的文本
【发布时间】:2016-06-23 14:06:15
【问题描述】:

我想使用QMessageBox.Question 作为图标。但我想更改标准按钮的文本。我不希望按钮的文本是“是”和“否”。我希望他们成为“Evet”和“Iptal”。这是我的代码。

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

【问题讨论】:

  • 实际上,现在我克服了这个问题,就像这样选择 = QtGui.QMessageBox.question(self, "Kaydet!", "Kaydetmek İstediğinize Emin Misiniz?", "Evet", button1Text='İptal', defaultButtonNumber=0, escapeButtonNumber=-1) ifchoice == 0: self.frame_11.show() else: pass

标签: python pyqt qmessagebox


【解决方案1】:

为此,您需要创建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

【讨论】:

  • 谢谢丹尼尔,不胜感激
  • 'no' in QtGui.QMessageBox.no 应该大写 -> QtGui.QMessageBox.No;抱歉,我不能只编辑一封信。
  • 不错的收获!谢谢!
  • 只是为了完成您的定制:box.setDefaultButton(QtGui.QMessageBox.No)(或QtGui.QMessageBox.Yes,您的电话)
  • 无角色条件不起作用。如果用户单击 no 选项,我必须执行 deleteFeature 功能.. 怎么办?
【解决方案2】:

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)

【讨论】:

  • 您可能还想检查除 'qt_%s' 之外的其他文件。例如,我的 Qt 安装有空的 qt_pl.qm,无法加载,但 qtbase_pl.qm 有我想要的所有翻译。 TranslationsPath目录下还有其他类型的翻译文件。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-02-07
  • 2012-08-12
  • 2017-12-28
相关资源
最近更新 更多