[PyQt] Using Unicode

projetmbc projetmbc at club-internet.fr
Wed Jun 17 11:43:47 BST 2009


Gabriel Hahmann a écrit :
> On Wed, Jun 17, 2009 at 11:33 AM, projetmbc<projetmbc at club-internet.fr> wrote:
>   
>> Gabriel Hahmann a écrit :
>>     
>>> Hi,
>>>
>>> I'm new to the list and new to programming in python QT and I'm having
>>> a little problem dealing with accent characters.
>>>
>>> I have a little program that have the text sent to the user interface
>>> using something like this:
>>>
>>> QtGui.QPushButton(self.tr('&Next >'))
>>>
>>>
>>> self.setWindowTitle(self.tr('Setup').arg(len(self.history)).arg(self.num_pages))
>>>
>>> self.topTitle = QtGui.QLabel(self.tr('<center><b>Warning</center>')
>>>
>>> self.set_status_text('Username', True)
>>>
>>> label_text = 'Warning'
>>> self.top_label = QtGui.QLabel(self.tr(label_text))
>>>
>>> In fact this is some excerpt of parts of the code. What I want to do
>>> is translate every word of the text to Portuguese. I tried simply
>>> translating but then it gives me an error of outside ASCII range.
>>>
>>> Then I added coding: utf-8 at the top of the file. Changed self.tr to
>>> self.trUtf8 with no luck. I tried self.tr(unicode("Some text")) with
>>> no luck too.
>>>
>>> What I want to do is simply tranlate this program, I dont want it to
>>> be internationalized, I mean, changing it in a way that it can be
>>> tranlated again easily.
>>>
>>> The Portuguese characters have é, ç, ê, ô, etc. I tried with
>>> self.trUtf8 and putting the code in hex like this self.trUtf8("\00C9")
>>> without success. I've read a lot of pages telling how to print unicode
>>> characters but I couldnt print a single character right now.
>>>
>>> If anyone can help me I will really appreciate, just to give me some
>>> more things to try!!
>>>
>>> Thanks in advance.
>>>
>>> Gabriel.
>>>       
>> I'm French and I've lmett this problem with the accents. Here is a solution
>> that I use.
>>
>> Instead of
>>         self.button.setText(self.tr("Créer les deux fichiers"))
>> I use in my class dial_Projet2
>>         self.button.setText(QtGui.QApplication.translate("dial_Projet2",
>> "Créer les deux fichiers", None, QtGui.QApplication.UnicodeUTF8))
>>
>> Christophe.
>>
>>
>>     
>
> Cristophe,
>
> Can I use your example without the need of changing my code?
>
> What you are saying is something like this:
>
> self.topTitle =
> QtGui.QLabel(QtGui.QApplication.translate("dial_Projet2", "Créer les
> deux fichiers", None, QtGui.QApplication.UnicodeUTF8))
>
> or
>
> QtGui.QPushButton(QtGui.QApplication.translate("dial_Projet2", "Créer
> les deux fichiers", None, QtGui.QApplication.UnicodeUTF8))
>
> Is that possible?
>
> And what are the parameters? The context (dial_project2) needs to be what?
>
> []'s
> Gabriel.
All of this is possible.

dial_project_2 is just my GUI class. For example, the example that I 
give comes frome something like that
class dial_Projet2(QtGui.QDialog, Ui_dial_Projet2):
    ....

Ui_dial_Projet2 is a Python file created from a Dialog built with Qt 
Designer.

I hope that I'm clear.

Christophe.

PS : Maybe it is possible to subclass the metho tr(). I never try for 
the moment but it could be an easy way to implement unicode without 
doing boring changes.




More information about the PyQt mailing list