How to connect a variable to Entry widget? How to connect a variable to Entry widget? tkinter tkinter

How to connect a variable to Entry widget?


I think you want something like this. In the example below, I created a variable myvar and assigned it to be textvariable of both a Label and Entry widgets. This way both are coupled and changes in the Entry widget will reflect automatically in Label.

You can also set trace on variables, e.g. to write to stdout.

from tkinter import *root = Tk()root.title("MyApp")myvar = StringVar()def mywarWritten(*args):    print "mywarWritten",myvar.get()myvar.trace("w", mywarWritten)label = Label(root, textvariable=myvar)label.pack()text_entry = Entry(root, textvariable=myvar)text_entry.pack()root.mainloop()