Tkinter-Listbox and Unicode

Matthias Huening mhuening at zedat.fu-berlin.de
Tue Jan 22 14:28:25 EST 2002


Hello,

I am still having problems with Python/Tkinter/Unicode.
A simpel example.
This works, no problems at all:

----
from Tkinter import *

root = Tk()

class Demo:
    def __init__(self, root):
        self.f = Frame(root)
        self.f.pack()

        self.l = Listbox(self.f)
        self.l.pack()

        self.but = Button(self.f, text='Get them', command=self.hol)
        self.but.pack()
       
        self.label = Label(self.f, text = 'Hi')
        self.label.pack(pady=10)
        
    def hol(self):
        out = ''
        for x in self.l.get(0, 'end'):
            out = out + '%s ' % x
        prog.label.configure(text = out)

if __name__ == '__main__':
    txt = 'Mühsam ernährt sich das Eichhörnchen'
    txt_list = txt.split()

    prog = Demo(root)
    
    for x in txt_list:
        prog.l.insert(END, x)  
        
    root.mainloop()
--------

But... when I change my input to
    	txt = u'Mühsam ernährt sich das Eichhörnchen'
or to
    	txt = 'Mühsam ernährt sich das Eichhörnchen'
    	txt = unicode(txt, 'Latin-1')
the words show up in te Listbox, but I cannot retrieve them from the 
Listbox:

Exception in Tkinter callback
Traceback (most recent call last):
  File "c:\python21\lib\lib-tk\Tkinter.py", line 1285, in __call__
    return apply(self.func, args)
  File "C:\ALLESM~1\TK-PMW~1.PY", line 21, in hol
    for x in self.l.get(0,3):
  File "c:\python21\lib\lib-tk\Tkinter.py", line 2293, in get
    return self.tk.splitlist(self.tk.call(
UnicodeError: ASCII encoding error: ordinal not in range(128)


What am I doing wrong??

Matthias




More information about the Python-list mailing list