[Tutor] i18n on Entry widgets

Kent Johnson kent37 at tds.net
Wed Aug 17 19:27:24 CEST 2005


Jorge Louis de Castro wrote:
> Hi,
> 
> How do I set the encoding of a string? I'm reading a string on a Entry 
> widget and it may use accents and other special characters from languages 
> other than English.
> When I send the string read through a socket the socket is automatically 
> closed. Is there a way to encode any special characters on a string?

First you have to know what the encoding is of the string you get from the Entry. IIRC a Tkinter widget will give you an ASCII string if possible, otherwise a Unicode string. You could check this by
  print repr(data)
where data is the string you get from the Entry.

Next you have to encode the unicode string to the encoding you want on the socket. If you want utf-8, you would use
  socket_data = data.encode('utf-8')
This will work if data is ASCII or Unicode. There are many other supported encodings; see http://docs.python.org/lib/standard-encodings.html for a list.

Kent



More information about the Tutor mailing list