Tkinter + Unicode -- possible FIX

Bob van der Poel bvdpoel at kootenay.com
Sun Jan 26 15:32:44 EST 2003


In another thread I whinned about the entry of character outside of the
7bit ascii range into a tkinter Entry widget crashing my program.

First, the problem: Somewhere along the line the data in an Entry widget
is being checked. And if it has non-7bit character in it the return 
value for Entry.get() is type unicode, NOT type str. Now, any python 
code which attempts to compare or print this value causes the 
interpreter to exit with a line similar to:

	UnicodeError: ASCII encoding error: ordinal not in range(128)

Second, how I wasted a lot of time trying to fix this: I figured I had
to change my program, so I started to add lots of lines like:

	if type(result) != type(""):
		result=result.encode(defaultEncoding)

where the variable defaultEncoding had been determined by a call to
locale.getlocale()

All this turned out to be unnecessary.

Third, the simple fix:


In the python library files "site.py"  there is code which looks like:

          if 0:
                # Enable to support locale aware default string encodings.
                import locale
                loc = locale.getdefaultlocale()
                if loc[1]:
                    encoding = loc[1]

Simply chaning the 'if 0:' to 'if 1:' nicely fixes the problem. I'm not
sure if this relevant to all combinations of python/tkinter. But it
certainly is with python 2.2 on my Mandrake 9.0 installation.

Hope this helps someone else.

-- 
Bob van der Poel ** Wynndel, British Columbia, CANADA **
EMAIL: bvdpoel at kootenay.com
WWW:   http://www.kootenay.com/~bvdpoel








More information about the Python-list mailing list