MyTetra Share
Делитесь знаниями!
Unicode in GTK+ / строки в GTK python2
Время создания: 13.05.2017 14:23
Раздел: Python - PyGTK - Basics - кодировка
Запись: xintrea/mytetra_db_mcold/master/base/1493589729r72pt3dowl/text.html на raw.githubusercontent.com

GTK+ uses UTF-8 encoded strings for all text. This means that if you call a method that returns a string you will always obtain an instance of the str type. The same applies to methods that expect one or more strings as parameter, they must be UTF-8 encoded. However, for convenience PyGObject will automatically convert any unicode instance to str if supplied as argument:

>>> from gi.repository import Gtk
>>> label = Gtk.Label()
>>> unicode_string = u"Fu\u00dfb\u00e4lle"
>>> label.set_text(unicode_string)
>>> txt = label.get_text()
>>> type(txt), txt
(<type 'str'>, 'Fu\xc3\x9fb\xc3\xa4lle')
>>> txt == unicode_string
__main__:1: UnicodeWarning: Unicode equal comparison failed to convert
both arguments to Unicode - interpreting them as being unequal
False

Note the warning at the end. Although we called Gtk.Label.set_text() with a unicode instance as argument, Gtk.Label.get_text() will always return a str instance. Accordingly, txt and unicode_string are not equal.

This is especially important if you want to internationalize your program using gettext. You have to make sure that gettext will return UTF-8 encoded 8-bit strings for all languages. In general it is recommended to not use unicode objects in GTK+ applications at all and only use UTF-8 encoded str objects since GTK+ does not fully integrate with unicode objects. Otherwise, you would have to decode the return values to Unicode strings each time you call a GTK+ method:

>>> txt = label.get_text().decode("utf-8")
>>> txt == unicode_string
True
Так же в этом разделе:
 
MyTetra Share v.0.59
Яндекс индекс цитирования