[Tutor] Tkinter

Robert Johansson robert.johansson at math.umu.se
Sat Aug 1 11:05:12 CEST 2009


Dear all, I have a problem with Tkinter and swedish letters such as ä (or
'\xe4'). Here’s a small code example that counts the number of occurrences
of the letter ‘a’ is in a string given by the user:

 

from Tkinter import *

 

root=Tk()

 

def callback():

    a=textLine.get()

    print a.count('a')

 

textLine=Entry(root)

textLine.pack(side=LEFT)

searchButton=Button(root,command=callback,text='Search')

searchButton.pack(side=RIGHT)

 

root.mainloop()

 

This works fine for strings containing all letters in my alphabet. However
if I change it to count ä:s it works only for strings without our special
letters (line 5: print a.count('ä')):

 

in callback

    print a.count('ä')

UnicodeDecodeError: 'ascii' codec can't decode byte 0xe4 in position 0:
ordinal not in range(128)

 

The following works fine from command line:

 

'rät'.count('ä')

 

And if I change line 5 to just printing a it works fine to.

 

Anyone who knows how to fix this?

 

Robert

 

 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20090801/39e20553/attachment-0001.htm>


More information about the Tutor mailing list