当我按下按钮时,我想刷新 tk 窗口,但我一直在删除。帮助将不胜感激。以下是我尝试过的。当我更改输入框并点击刷新时,我希望 tk.Label 行更新,但它没有发生。
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()
任何帮助将不胜感激。谢谢
编辑:
我也尝试过使用 update_idletasks(),但这也没有用。
sc0tt的帮助回答:
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()