Tkinter / Unicode and UTF-8

Thomas pushcold at hotpop.foo
Thu Nov 20 17:12:06 EST 2003


On Thu, 20 Nov 2003 21:00:22 +0100, Martin v. Löwis wrote:

> Thomas <pushcold at hotpop.foo> writes:
> 
>> Now I switched to Fedora Core 1 Linux (where Python/Tk does not 
>> work without fixing it - but I described that in another thread) 
>> and I have to pass UTF-8 encoded strings to Tk widgets (i.e. I 
>> cannot directly pass Unicode strings any more).
> 
> Then you fixed it incorrectly.

Hi Martin!

I just used the 'Python' and 'tkinter' RPMs from www.python.org to
update ('rpm -U ...') the RPMs provided with the Fedora Core 1 Linux
distribution. (The RPMs of the distribution did not allow to use any
Python/Tk application because Python was compiled with the UCS4 option
whereas Tcl/Tk uses UCS2 (if I understand this point correctly).)


Now, the following example does not work correctly:

  from Tkinter import *
  tk = Tk()

  txt = Text(tk)
  txt.pack()
  message = u"hello"
  txt.insert('1.0', message)

  tk.mainloop()

(The above example will display h\x00e\x001 in the Text widget.)


But the following works (only the 'UTF-8' part is new here):

  from Tkinter import *
  tk = Tk()

  txt = Text(tk)
  txt.pack()
  message = u"hello"
  txt.insert('1.0', message.encode('UTF-8'))

  tk.mainloop()

(I just used the Text widget as an example here; the same holds
for many other widgets, e.g. menus.)

In some Tcl/Tk documentation, I read that Tk widgets expect UTF-8;
somewhere else (don't remember the URLs), I read that _tkinter.c
handles this (by encoding Unicode strings with UTF-8 for Tk widgets);
that was the reason why I thought there might have been a change
in Tkinter recently.

But (my main problem!): I still do not understand why the first
example does not work, while the second does!?

Thomas

-- 
mailto: pushcold at hotpop.foo (where: foo = com)





More information about the Python-list mailing list