tkinter + unicode + bug or feature??

Eric Brunel eric.brunel at pragmadev.com
Mon Jan 27 03:59:26 EST 2003


Bob van der Poel wrote:
[snip]
> If my program takes strings entered by a user in a Entry() widget and I 
> take that data, convert it from a possible unicode string to the user's 
> current locale, will the result always be a regular string? Really, what 
> I'm trying to do is to avoid having my program crash when I do something 
> like:
> 
>     a=entrywidget.get()
>     if a == somestring:
>        .....
> 
> Current, 'somestring' IS a regular string. And if 'a' is a unicode the 
> program aborts. So, I'm planning on replacing get() with myget() which 
> will just do:
> 
>     a=widget.get().encode(userEncoding)

I'd use:

a = widget.get().encode(userEncoding, 'replace')

This will replace any character in the string that is not in userEncoding by a 
'?', but it should never raise an exception (if I'm not mistaken...). And if 
your user can type a character, it should be in his/her local encoding, so 
replacements shouldn't occur.
-- 
- Eric Brunel <eric.brunel at pragmadev.com> -
PragmaDev : Real Time Software Development Tools - http://www.pragmadev.com





More information about the Python-list mailing list