PDA

View Full Version : How to get foreign text in title bars, buttons, tool tips, etc.



tom.hedden
8th May 2014, 02:35
I'm working on the tutorial at:
http://zetcode.com/gui/pyqt4/firstprograms/
It has been very helpful. I have been experimenting to expand my knowledge.
In the first example program I see:
# -*- coding: utf-8 -*-
I assume that this means that the encoding is UTF-8. (No?)
If I understand correctly, this means that I should be able to use other
natural languages such as Russian, at least in strings, UI, etc. (No?)
The title bar text is set by the line:
w.setWindowTitle('Simple')
I changed the string 'Simple" to Russian 'Простой', which is Russian for 'Simple'.
When I execute the file the text is not properly displayed in the title bar.
Similarly, when I put Russian text in the Tooltip example it is also not
displayed properly.
Similarly, when I change the label of the "Quit" button in that example the
text is also not properly displayed.
Is there something I have to do differently to get these things to work?
Or are these labels dependent upon the system language (which in my
case is English)? Or does this represent a limitation of the PyQt?
Thanks in advance for any pointers.
Tom

ChrisW67
8th May 2014, 04:22
In the first example program I see:
# -*- coding: utf-8 -*-
I assume that this means that the encoding is UTF-8. (No?)

No, it means that the Python interpreter should assume the file is UTF-8 encoded and not strip out non-ASCII. You must ensure that the file is saved that way. If you are editing the file with emacs that same magic line will set the editor to the relevant mode. If you edit it with something else then you must ensure that is loads/saves text UTF-8 encoded and not, for example, using Latin 1 or a Windows-1252 (which will mangle the content).

Then you must tell Python to create Unicode strings from the string literals:


# These options all work for me
w.setWindowTitle(u'Простой' ) # note the preceding 'u'
w.setWindowTitle('Простой'. decode('utf-8'))
w.setWindowTitle(unicode('ПростР¾Ð¹', 'utf-8'))
and PyQt will do the correct thing with them.

Generally these fixed strings would be handled using Qt's translation mechanisms.

tom.hedden
8th May 2014, 16:57
Hello ChrisW67,
Thank you for your explanation.
The Russian was/is definitely UTF-8 encoded: I'm very aware of that pitfall.
I tried all three options that you suggested, and they all work fine.
Thanks again,
Tom