简体   繁体   中英

PyQt4 Python2 Unicode

hey guys i have been working on a calculator with python and PyQt4 on Ubuntu.

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.

I tried adding # -*- coding: utf-8 -*- to my script but it didn't help.

Here's how it looks under python2 :

Python2

Under 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.

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.

I use for example:

u"\u25B2"

QString only accepts ASCII characters. I don't think it will work in Python v2 or v3. You may get a little more from QByteArray as it accepts Latin-1.

From the docs:

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.

Adding the encoding at the top of the file only helps the OS when reading the file not interpreting the code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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