Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TextVariable not working

Tags:

python

tkinter

I am trying to get the Text out of an Entry widget in Tkinter. It works with Entry1.get(), but it does not work using textvariable

What am I doing wrong ?

from Tkinter import *
master = Tk()
v = StringVar()

def Entered(p1):
    print 'Got: ', Entry1.get()
    print 'Got: ', v.get()

Entry1 = Entry(master, text = '', width = 25, textvariable = v)
Entry1.pack()
Entry1.bind('<Return>', Entered)
like image 224
user1530405 Avatar asked Feb 18 '16 07:02

user1530405


Video Answer


1 Answers

The problem is with text.

If you give the text argument, it seems that the textvariable.get() will return nothing. I don't know if its a bug or not.

from Tkinter import *
master = Tk()
v = StringVar()

def Entered(p1):
    print 'Got: ', Entry1.get()
    print 'Got: ', v.get()

Entry1 = Entry(master, width = 25, textvariable = v) # No text now
Entry1.pack()
Entry1.bind('<Return>', Entered)
master.mainloop()

If you enter asd it returns:

Got:  asd
Got:  asd

The interesting part that if you change the entry to:

Entry1 = Entry(master, text = 'sajt', width = 25, textvariable = v)

It will still return nothing with v.get() not sajt as i would expect.

like image 188
Gábor Erdős Avatar answered Oct 26 '22 17:10

Gábor Erdős