Tkinter + Unicode -- possible FIX

vincent wehren v.wehren at home.nl
Mon Jan 27 07:30:28 CET 2003


"Bob van der Poel" <bvdpoel at kootenay.com> schrieb im Newsbeitrag
news:3e3446f4_1 at dns.sd54.bc.ca...
>
> 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.

What's wrong with encoding the strings as needed, meaning at the time of
print output or storage in a byte-oriented file? And promoting your
byte-oriented strings to unicode to enable comparison?




Vincent Wehren


>
> --
> 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