PyQt4 Python2 Unicode

PyQt4 Python2 Unicode

大家好,我一直在用 python 和 PyQt4 在 Ubuntu 上开发计算器。

我有一些带有 Unicode 字符的按钮(后退箭头、根),我在终端中使用 cxfreeze 命令来编译我的项目,它默认使用 python 2 一切正常,但是,当我 运行 程序时,按钮上的文本显示不正确。

我尝试将 # -*- coding: utf-8 -*- 添加到我的脚本中,但它没有帮助。

这是 python2 下的样子:

在 Python3 下:

我想知道是否有办法让 cx_freeze 使用 python3 ,或者在 python2.

下正确显示特殊字符

任何帮助将不胜感激,谢谢。

QString 只接受 ASCII 字符。我不认为它会在 Python v2 或 v3 中工作。您可能会从 QByteArray 获得更多,因为它接受 Latin-1。

来自文档:

http://pyqt.sourceforge.net/Docs/PyQt4/gotchas.html

For Python v3 the following conversions are done by default.

  • If Qt expects a char * (or a const version) then PyQt4 will accept a str or QString that contains only ASCII characters, a bytes, a QByteArray, or a Python object that implements the buffer protocol.
  • If Qt expects a char (or a const version) then PyQt4 will accept the same types as for char * and also require that a single character is provided.
  • If Qt expects a signed char * or an unsigned char * (or a const version) then PyQt4 will accept a bytes.
  • If Qt expects a signed char or an unsigned char (or a const version) then PyQt4 will accept a bytes of length 1.
  • If Qt expects a QString then PyQt4 will accept a str, a bytes that contains only ASCII characters, a QChar or a QByteArray.
  • If Qt expects a QByteArray then PyQt4 will also accept a str that contains only Latin-1 characters, or a bytes.

For Python v2 the following conversions are done by default.

  • If Qt expects a char *, signed char * or an unsigned char * (or a const version) then PyQt4 will accept a unicode or QString that contains only ASCII characters, a str, a QByteArray, or a Python object that implements the buffer protocol.
  • If Qt expects a char, signed char or an unsigned char (or a const version) then PyQt4 will accept the same types as for char *, signed char * and unsigned char * and also require that a single character is provided.
  • If Qt expects a QString then PyQt4 will accept a unicode, a str that contains only ASCII characters, a QChar or a QByteArray.
  • If Qt expects a QByteArray then PyQt4 will accept a unicode that contains only Latin-1 characters, or a str.

在文件顶部添加编码仅有助于 OS 读取文件而不解释代码。

很久以前我用 unicode 字符制作了一个程序(PyQt4/PySide 和 python 2.7.x)并使用 cx_Freeze 构建并运行。

我用的例子:

u"\u25B2"