Reputation: 43
I am working on with PySide 1.21 and Qt 4.85 using PyCharm 3.1 with Python 2.7.6. I would like my application to support unicode so at the beginning of the code I type:
#--coding: utf-8 --
from PySide.QtCore import *
from PySide.QtGui import *
import sys
import math
class Form(QDialog):
def __init__(self,parent=None):
super(Form,self).__init__(parent)
self.resultsList = QTextBrowser()
self.resultsInput = QLineEdit("Enter an expression and press return key")
layout = QVBoxLayout()
layout.addWidget(self.resultsList)
layout.addWidget(self.resultsInput)
self.setLayout(layout)
self.resultsInput.selectAll() # or
self.resultsInput.setFocus()
self.resultsInput.returnPressed.connect(self.compute)
def compute(self):
try:
text = self.resultsInput.text()
self.resultsList.append("{0} =<b>{1}</b>".format(text, eval(text)))
except:
self.resultsList.append("<font color=red><b>Expression Invalid</b></font>")
# self.resultsList.append("<font color=red><b>格式错误</b></font>") ## unicode
app = QApplication(sys.argv)
form = Form()
form.show()
app.exec_()
When I replaced the code at the except block using unicode, the unicode does not show up properly in the program. Where did I get wrong? Is the problem with the PySide, Qt or some setting error? Any help will be appreciated.
Upvotes: 1
Views: 1779
Reputation: 43
Finally I get it sorted. It is simple, in python 2.7, when you want to support unicode, you need to declare:
#--coding: utf-8 --
at the beginning of the program, also when hard coding with in the application, you need to write "u" in front of the code. For example:
self.resultsList.append("<font color=red><b>Expression Invalid</b></font>")
need to write as:
self.resultsList.append(u"<font color=red><b>格式错误</b></font>")
just a little "u" and the problem is solved.
Upvotes: 2