tkinter StringVar: Get back something differently than I set

Post here if you need help with creating a Graphical User Interface in Python.

tkinter StringVar: Get back something differently than I set

Postby rovf » Mon Feb 03, 2014 5:48 pm

In short:
I have a string.
Code: Select all
s='....'

I print the string to the terminal. It displays fine.
Code: Select all
print s

I put the string into a tkinter StringVar
Code: Select all
text_v.set(s)

I fetch the string back from this StringVar.
Code: Select all
s=text_v.get()

I try to print this string again. I get an exception.
Code: Select all
print s

The exception I get is: UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1

From what I read in various forums, the reason could be that my terminal does not support the unicode characters used. However, since I could print the string fine in the first place, this can't be the reason here.

I have a self-contained example showing the problem, and wanted to include it in this post, but if I do this, I always get a SQL error. I guess this is also related to the fact that my code contains unicode characters. Hence, to show the example program, I did two things:

- I uploaded it as attachment, and for the safe side
- I also provide a screen shot of the program (just in case the upload also gets messed up). Here it is:
Code: Select all
#!/usr/bin/python
# -*- coding: utf-8 -*-
from Tkinter import *

def test():
    global text_v
    print text_v.get()
   
root=Tk()
text_v=StringVar()
textarea=Entry(root,textvariable=text_v)
s='東京'
print s
text_v.set(s)
#####  print text_v.get()
button=Button(root,text='押してよ!',command=test)
textarea.pack()
button.pack()
root.geometry('+70+50')
root.mainloop()
root.destroy()


A few notes:

- Line 13 displays the string 東京 correctly on the command line of my terminal

- After the mainloop is entered, the string is shown in the entry field, and the button text is also shown correctly. Doesn't look like an encoding problem to me.

- When I press the button, the exception is thrown in line 7

- When I uncomment line 15 and run the program again, the exception is already thrown in this line, 15.

What's causing this, and how can I remedy it?
Attachments
screenshot-138.jpg
screenshot-138.jpg (165.37 KiB) Viewed 421 times
getvtest_py.txt
(393 Bytes) Downloaded 30 times
Last edited by stranac on Mon Feb 03, 2014 6:13 pm, edited 1 time in total.
Reason: Inserted code in code tags and stuff.
rovf
 
Posts: 25
Joined: Fri Aug 16, 2013 4:35 pm

Re: tkinter StringVar: Get back something differently than I

Postby stranac » Mon Feb 03, 2014 6:09 pm

Tkinter gives you a unicode string.
When you try printing it, python tries encoding it using the default encoding, which is 'ascii'.
Since ascii can't represent those characters, an exception is raised.

You need to explicitly decode the unicode string before printing it:
Code: Select all
print text_v.get().encode('utf-8')


Sorry about the sql errors.
The database settings are kinda messed up, so it can't handle some characters correctly.
And since it's already filled with weirdly encoded strings, converting to different encoding is non-trivial.
I hope to be able to fix it when I'm updating the forum.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1144
Joined: Thu Feb 07, 2013 3:42 pm

Re: tkinter StringVar: Get back something differently than I

Postby rovf » Tue Feb 04, 2014 10:20 am

> When you try printing it, python tries encoding it using the default encoding, which is 'ascii'.

Why does this problem occur only when I extract the string from a StringVar? After all, after assigning the string literally to my variable, I could print it without problems.

Ronald
rovf
 
Posts: 25
Joined: Fri Aug 16, 2013 4:35 pm

Re: tkinter StringVar: Get back something differently than I

Postby stranac » Tue Feb 04, 2014 10:57 am

Because tkinter deals with unicode strings, not bytes("default" strings in python 2).
In order to display unicode it needs to encode it.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1144
Joined: Thu Feb 07, 2013 3:42 pm


Return to GUI

Who is online

Users browsing this forum: No registered users and 4 guests