Consider this code:
from tkinter import *
from tkinter.ttk import *
tk=Tk()
def sub():
var=StringVar(value='default value')
def f(): pass
Entry(tk,textvariable=var).pack()
Button(tk,text='OK',command=f).pack()
sub()
mainloop()
We expect the value of var
appears in the entry, but actually it doesn't.
The weird thing is that if I put the statement var.get()
in the callback function of the button, the value of var
will apear.
Is that a bug caused by some kind of local variable optimization in Python? And what can I do to make sure that the value of textvariable
will always appear in the entry?
Please execuse me for my poor English.