I would like to refresh tk window when I push a button, but I have been striking out. Help would be appreciated. Below is what I have tried. I would like the tk.Label line to update when I change the input box and hit refresh, but it isn’t happening.
import Tkinter as tk
class MAIN(object):
def __init__(self, master, **kwargs):
frame = tk.Frame(master, borderwidth=5)
frame.grid()
et1 = tk.Entry(frame)
et1.insert(0, '10')
et1.grid(row=0,column=0,sticky=tk.W)
tk.Label(frame, text = et1.get()).grid(row=1, column=0, sticky=tk.W)
refresh = tk.Button(frame, text='Refresh', command = frame.update())
refresh.grid(row=2, column=0, sticky=tk.W)
root=tk.Tk()
app=MAIN(root)
root.mainloop()
Any Help would be greatly appreciated. Thanks
Edit:
I have also tried to use the update_idletasks(), but that didn’t work either.
Answer from sc0tt’s help:
import Tkinter as tk
class MAIN(object):
def __init__(self, master, **kwargs):
frame = tk.Frame(master, borderwidth=5)
frame.grid()
self.et1 = tk.Entry(frame)
self.et1.insert(0, 10)
self.et1.grid(row=0,column=0,sticky=tk.W)
self.label_contents = tk.StringVar()
self.label_contents.set(self.et1.get())
tk.Label(frame, textvariable=self.label_contents).grid(row=1, column=0, sticky=tk.W)
refresh = tk.Button(frame, text='Refresh', command = self.refresh_hit)
refresh.grid(row=2, column=0, sticky=tk.W)
def refresh_hit(self):
self.label_contents.set(self.et1.get())
root=tk.Tk()
app=MAIN(root)
root.mainloop()
calling update/update_idletasks won’t do that for you. Here is a way of doing it using a simple lambda in the command for the button. It takes the value of the text box and alters the variable associated with the label.
import Tkinter as tk
Edit: lambda replaced by function call.