TextVariable not working

I am trying to get text from 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)
+4
source share
1 answer

The problem is text.

If you give an argument text, it seems like textvariable.get()nothing will return. I do not know if his mistake was 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 will return:

Got:  asd
Got:  asd

The interesting part is that if you change the entry to:

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

It still will not return anything using v.get()not sajt, as I expected.

+1
source

Source: https://habr.com/ru/post/1629319/


All Articles