用按钮刷新tk窗口

时间:2012-06-19 19:31:11

标签: python tkinter

当我按下按钮时,我想刷新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()

3 个答案:

答案 0 :(得分:2)

调用update / update_idletasks不会为你做这件事。这是一种在按钮的命令中使用简单的lambda来实现它的方法。它采用文本框的值并更改与标签关联的变量。 将Tkinter导入为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)
        label_contents = tk.StringVar()
        label_contents.set(et1.get())
        tk.Label(frame, textvariable=label_contents).grid(row=1, column=0, sticky=tk.W)
        refresh = tk.Button(frame, text='Refresh', command = self.refresh_clicked)
        refresh.grid(row=2, column=0, sticky=tk.W)

    #per comments:
    def refresh_clicked(event):
        #update labels here
root=tk.Tk()
app=MAIN(root)
root.mainloop()

编辑: lambda替换为函数调用。

答案 1 :(得分:0)

remove_button.grid(row=0, column=2)
refresh_button = tk.Button(master=buttons_frame, width=7, text='Refresh', 
command=seeTodo)

要刷新窗口,请使用按钮再次调用该窗口。

答案 2 :(得分:-1)

这是我使这个脚本工作的唯一方法,否则我会在未定义的“self”上收到错误。还有其他方法可以在refresh_clicked上添加函数吗?

`

class MAIN(object):


    def __init__(self, master, **kwargs):
        global label_contents
        global et1
        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)
        label_contents = tk.StringVar()
        label_contents.set(et1.get())
        tk.Label(frame, textvariable=label_contents).grid(row=1, column=0, sticky=tk.W)
        refresh = tk.Button(frame, text='Refresh', command = self.refresh_clicked)
        refresh.grid(row=2, column=0, sticky=tk.W)

    #per comments:
    def refresh_clicked(event):
         global label_contents
         global et1
         label_contents.set(et1.get())

root=tk.Tk()
app=MAIN(root)
root.mainloop()

`