[Tutor] Tkinter and non-ASCII-characters, again

Scot W. Stevenson scot@possum.in-berlin.de
Wed Oct 23 05:36:03 2002


This has been covered before, but the solution I found in the archives 
doesn't seem work for me: 

I'm asking for input into a Tkinter Entry widget, and have to be able to 
accept German Umlauts, which are not part of ASCII. Typing in Umlauts 
gives me:

================================
Exception in Tkinter callback
Traceback (most recent call last):
  File "/usr/lib/python2.2/lib-tk/Tkinter.py", line 1292, in __call__
    return apply(self.func, args)
  File "test2.py", line 513, in search
    if eval(teststring):
UnicodeError: ASCII encoding error: ordinal not in range(128)
=================================

Now, Gregor demonstrated a solution to this on the 24. Sept such that:

============================
import sys,  locale
loc = locale.getdefaultlocale()
if loc[1]:
    encoding = loc[1]
if encoding != "ascii":
    sys.setdefaultencoding(encoding)
=============================

But putting this in the "sitecustomize.py" file is not an option in this 
case, as the program might end up running on machines where I can't fool 
around with that file. Also, I get this error (SuSE 8.0, Python 2.2):

=============================
>>> encoding = locale.getdefaultlocale()[1]
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "/usr/lib/python2.2/locale.py", line 337, in getdefaultlocale
    return _parse_localename(localename)
  File "/usr/lib/python2.2/locale.py", line 271, in _parse_localename
    raise ValueError, 'unknown locale: %s' % localename
ValueError: unknown locale: de_DE@euro
==============================

so this won't work anyway. The Python Cookbook isn't much help, since it 
and other examples I've found seem to want me to do the full Unicode 
dance, which seems like complete overkill to me: All I want to do is 
switch the default character set for Tkinter from "ascii" to "iso-8859". 
It has to be a Tkinter problem, since the command line version of the same 
funktion works fine...

Terribly frustrating.

Thank you for any help,
Y, Scot

-- 
Scot W. Stevenson wrote me on Wednesday, 23. Oct 2002 in Zepernick, Germany  
       on his happy little Linux system that has been up for 540 hours        
        and has a CPU that is falling asleep at a system load of 0.07.