简体   繁体   English

PyQt4 Python2 Unicode

[英]PyQt4 Python2 Unicode

hey guys i have been working on a calculator with python and PyQt4 on Ubuntu. 大家好,我一直在Ubuntu上使用python和PyQt4制作计算器。

I have some buttons with Unicode characters (back arrow, root) , I used the cxfreeze command in the terminal to compile my project, it uses python 2 by default everything went fine , however when i run the program the text on the buttons isn't shown properly. 我有一些带有Unicode字符的按钮(后退箭头,根),我在终端中使用了cxfreeze命令来编译我的项目,默认情况下它使用python 2一切都很好,但是当我运行该程序时,按钮上的文本为' t正确显示。

I tried adding # -*- coding: utf-8 -*- to my script but it didn't help. 我尝试在脚本中添加# -*- coding: utf-8 -*- ,但没有帮助。

Here's how it looks under python2 : 这是在python2下的样子:

Python2

Under Python3 : 在Python3下:

Python3

I would like to know if there's a way to make cx_freeze use python3 , OR show the special characters properly under python2. 我想知道是否有一种方法可以使cx_freeze使用python3,或者在python2下正确显示特殊字符。

Any help would be appreciated, Thank you. 任何帮助将不胜感激,谢谢。

long ago I made a program (PyQt4/PySide and python 2.7.x) with unicode characters and build with cx_Freeze and works. 很久以前,我制作了一个具有unicode字符的程序(PyQt4 / PySide和python 2.7.x),并使用cx_Freeze构建并正常工作。

I use for example: 我使用例如:

u"\u25B2"

QString only accepts ASCII characters. QString仅接受ASCII字符。 I don't think it will work in Python v2 or v3. 我认为它不适用于Python v2或v3。 You may get a little more from QByteArray as it accepts Latin-1. 由于QByteArray接受Latin-1,您可能会从中得到更多。

From the docs: 从文档:

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

For Python v3 the following conversions are done by default. 对于Python v3,默认情况下会进行以下转换。

  • 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. 如果Qt期望使用char *(或const版本),则PyQt4将接受仅包含ASCII字符,字节,QByteArray或实现缓冲协议的Python对象的str或QString。
  • 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. 如果Qt需要一个char(或const版本),则PyQt4将接受与char *相同的类型,并且还要求提供一个字符。
  • If Qt expects a signed char * or an unsigned char * (or a const version) then PyQt4 will accept a bytes. 如果Qt期望带符号的char *或无符号的char *(或const版本),则PyQt4将接受一个字节。
  • If Qt expects a signed char or an unsigned char (or a const version) then PyQt4 will accept a bytes of length 1. 如果Qt需要一个有符号的char或一个无符号的char(或const版本),则PyQt4将接受长度为1的字节。
  • If Qt expects a QString then PyQt4 will accept a str, a bytes that contains only ASCII characters, a QChar or a QByteArray. 如果Qt期望使用QString,则PyQt4将接受str(仅包含ASCII字符,QChar或QByteArray的字节)。
  • If Qt expects a QByteArray then PyQt4 will also accept a str that contains only Latin-1 characters, or a bytes. 如果Qt需要一个QByteArray,那么PyQt4也将接受仅包含Latin-1字符或字节的str。

For Python v2 the following conversions are done by default. 对于Python v2,默认情况下会进行以下转换。

  • 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. 如果Qt期望使用char *,signed char *或unsigned char *(或const版本),则PyQt4将接受仅包含ASCII字符,str,QByteArray或实现缓冲区协议的Python对象的unicode或QString。 。
  • 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. 如果Qt期望使用char,signed char或unsigned char(或const版本),则PyQt4将接受与char *,signed char *和unsigned char *相同的类型,并且还要求提供单个字符。
  • If Qt expects a QString then PyQt4 will accept a unicode, a str that contains only ASCII characters, a QChar or a QByteArray. 如果Qt需要QString,则PyQt4将接受unicode,即仅包含ASCII字符,QChar或QByteArray的str。
  • If Qt expects a QByteArray then PyQt4 will accept a unicode that contains only Latin-1 characters, or a str. 如果Qt需要一个QByteArray,则PyQt4将接受仅包含Latin-1字符或str的unicode。

Adding the encoding at the top of the file only helps the OS when reading the file not interpreting the code. 在文件顶部添加编码仅在读取文件而不解释代码时才有助于操作系统。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM