Python: PySide(PyQt)QMessageBox按钮显示中文
习惯了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'
谁可知之?大抵其官方文档可见些解释吧。一些是点击触发事件,一些仅是样式类似。

参考资料
QMessageBox - PySide v1.0.7 documentation
QDialogButtonBox Class | Qt 4.8
Python: PySide(PyQt)QMessageBox按钮显示中文的更多相关文章
- extjs之messagebox按钮显示中文
在extjs中我们用到了很多的提示框,但是在对话框按钮上面显示的都是英文的ok.yes.no.cancel,这里给出一个方法,能够使得提示框的按钮文本显示问中文. extjs在加载完成之后会调用页面的 ...
- UIWebView弹出键盘按钮显示中文
UIWebView是一个很常用的视图,一般用来加载网页,比如百度: 点击文本框输入框后,会弹出一个带有toolbar的键盘,toolbar中有3个辅助按钮 有了这3个按钮,是方便很多,但默认是英文的, ...
- 让UIWebView弹出键盘上的按钮显示中文
UIWebView是一个很常用的视图,一般用来加载网页,比如百度: 点击文本框输入框后,会弹出一个带有toolbar的键盘,toolbar中有3个辅助按钮 有了这3个按钮,是方便很多,但默认是英文的, ...
- dev XtraMessageBox按钮显示中文
dev的XtraMessageBox控件使用起来很美观,但默认显示确定的是英文,如下图: 通过下面代码可使“OK”显示为中文:首先创建一个继承自Localizer的类: using DevExpres ...
- IOS 调用系统相册或照相机tab按钮显示中文
- 让XtraMessageBox按钮显示中文
需要定义一个继承子Localizer的类,然后重写GetLocalizedString public class MessboxClass : Localizer { public override ...
- python matplotlib plot 数据中的中文无法正常显示的解决办法
转发自:http://blog.csdn.net/laoyaotask/article/details/22117745?utm_source=tuicool python matplotlib pl ...
- uploadify不能正确显示中文的按钮文本的解决办法
uploadify 目前不能正确显示中文的按钮文本. 我发现bug的原因是uploadify错误的使用了 js 的 escape 和 flash 的 unescape配对,而这2个是不兼容的.正确的转 ...
- 关于python无法显示中文的问题:SyntaxError: Non-ASCII character '\xe4' in file test.py on line 3, but no encoding declared。
[已解决]关于python无法显示中文的问题:SyntaxError: Non-ASCII character '\xe4' in file test.py on line 3, but no enc ...
随机推荐
- Linux zookeeper 单机安装
Zookeeper(端口2181) 下载地址 http://mirror.bit.edu.cn/apache/zookeeper/ 解压到/usr/local目录 >tar -zxvf zook ...
- redis如何清除所有的key
redis比memcache好的地方之一,如果memcache,恐怕就得关掉重启了. 1 使用cli FLUSHDB 清除一个数据库,FLUSHALL清除整个redis数据. 2 使用shell re ...
- linux 常用命令记录&& xsheel 使用记录
cp -r x1 x2 复制文件x1到x2 ls 当前目录下的文件列表 ll ...
- js 滑动门的实现
原理:滑动门,这里以图片进行实例,首先设定主盒子div的宽度和高度设定,并进行图片初始化位置的设定,然后将图片绑定事件,并设定要达到的效果 html代码: <!DOCTYPE html> ...
- Servlet基本_初期化パラメータ、外部ファイル
1.サーブレットの初期化パラメータサーブレットの初期化パラメータを利用するには.必ずweb.xmlにおいてサーブレットマッピングを指定する必要がある.(Tomactのinvokerサーブレットは利用で ...
- postmessage/cors跨域postMessage、xhr2和xmldomain
一.h5 postMessage node http-server配置服务器 有关配置:请参考我的http://www.cnblogs.com/leee/p/5502727.html 我把文件夹a配置 ...
- List of numerical libraries,Top Numerical Libraries For C#
Top Numerical Libraries For C# AlgLib (http://alglib.net) ALGLIB is a numerical analysis and data pr ...
- 传输层——UDP报文头介绍
16位源端口 16位目的端口 16位总长度 16位校验和 数据 源端口:长度为16位,2个字节. 目的端口:长度为16位,2个字节. 总长度:长度为16位,2个字节,表示 UDP包头长度 和 数据长度 ...
- VIM+ctags+cscope
VIM+ctags+cscope用法: http://blog.csdn.net/hnllei/article/details/7407124 http://blog.csdn.net/luckywa ...
- jquery val() text() html()的区别
value()主要用在表单元素上,如果其他的元素获取value是通过attract()的方法,text()是获取元素的纯文本,如果text(“content”)就会更改元素的文本内容:html()获取 ...