习惯了Delphi、c#调用系统MessageBox本地化显示,待用PySide调用时,Qt原生提示对话框,默认以英文显示。
如何本地化呢?
参考些资料,加以摸索,实现所需效果。并可根据此思路,设计自己所需要的MessageBox封装。
QTextCodec.setCodecForTr(QTextCodec.codecForName("UTF-8")) box = QMessageBox(QMessageBox.Question, self.tr("提示"), self.tr("您确定要退出吗?"), QMessageBox.NoButton, self) yr_btn = box.addButton(self.tr("是"), QMessageBox.YesRole) box.addButton(self.tr("否"), QMessageBox.NoRole) box.exec_() if box.clickedButton() == yr_btn: print 'Bye bye...' return else: print '继续...'
效果如下图示:
直接以.exec_()判断,[是]按钮返回0,尚不知如何与QMesageBox.YesRole对应。但若使用QMessageBox.AcceptRole与QMessageBox.RejectRole则可以。
下面代码,摘自PySide自带例子:
MESSAGE = "<p>Message boxes have a caption, a text, and up to three " \ "buttons, each with standard or custom texts.</p>" \ "<p>Click a button to close the message box. Pressing the Esc " \ "button will activate the detected escape button (if any).</p>" msgBox = QMessageBox(QMessageBox.Question, "QMessageBox.warning()", MESSAGE, QMessageBox.NoButton, self) msgBox.addButton("Save &Again", QMessageBox.AcceptRole) msgBox.addButton("&Continue", QMessageBox.RejectRole) if msgBox.exec_() == QMessageBox.AcceptRole: print "Save Again" else: print "Continue"
显示如图:
另一种分步方案:
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: print 'YES pressed' elif box.clickedButton() == buttonN: print 'NO pressed'
谁可知之?大抵其官方文档可见些解释吧。一些是点击触发事件,一些仅是样式类似。
参考资料