0

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

3 回答 3

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 替换为函数调用。

于 2012-06-19T19:54:10.657 回答
0
remove_button.grid(row=0, column=2)
refresh_button = tk.Button(master=buttons_frame, width=7, text='Refresh', 
command=seeTodo)

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

于 2020-04-17T09:10:40.737 回答
-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()

`

于 2016-01-14T02:51:26.527 回答