Solve Chinese scrambling when PYQT is used in python

  • 2021-06-28 13:20:45
  • OfStack

For example, to solve the problem of Chinese scrambling when PyQt is used in Python:

u precedes the Chinese string, such as u'Hello World'. Other online methods are not explored much. The version of Python will also affect the solution, so this is only recommended here.

(Some people say that using the toLocal8bit function is OK, but I tried it and it doesn't seem to work). See an example:


#coding=utf-8

from PyQt4 import QtGui, QtCore

s = QtCore.QString(u' Hello (hello) world (world)')
t = s.toLocal8Bit()
u = unicode(t,'gbk','ignore')
print t
print u

The output of this program is:


���(hello)����(world)
 Hello (hello) world (world)

[Note] When using controls, you need to use Chinese, add u before the string you want to display, for example (the following two lines of code are in the class and cannot be executed directly):


btn_quit = QtGui.QPushButton(u" Close ",self)

reply = QtGui.QMessageBox.question(self, u' MsgBox ',
      "Are you sure to quit?", QtGui.QMessageBox.Yes, QtGui.QMessageBox.No)

Related articles: