Tkinter + Unicode -- possible FIX

Bob van der Poel bvdpoel at kootenay.com
Mon Jan 27 11:46:31 EST 2003


"vincent wehren" <v.wehren at home.nl> wrote in message news:<b12j8e$bof$1 at news2.tilbu1.nb.home.nl>...
> "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?
> 

Well, the biggest problem in the particular case I'm working on is
that the program has dozens of EXISTING places where the problem might
occur. Add to this, as I stated in another post, that the biggest
problem is the inconsistency of the entire process... And, I'm sort of
convinced that having encoding enabled is the proper way to do things.




More information about the Python-list mailing list