如何使用刷新按钮在 python 中完全刷新 tkinter 窗口

问题描述:

如何使用刷新按钮在 python 中完全刷新 Tkinter 窗口.

最简单的方法是将整个窗口实现为一个 tk Frame 的子类,然后销毁并重新创建它.您的代码可能如下所示:

The simplest way is to implement the entire window as a subclass of a tk Frame, and then destroy and recreate it. Your code might look something like this:

import Tkinter as tk

class Example(tk.Frame):
    def __init__(self, parent):
        tk.Frame.__init__(self, parent)
        <other code here...>

class Application:
    def __init__(self):
        self.root = tk.Tk()
        self.frame = None
        refreshButton = tk.Button(self.root, text="refresh", command=self.refresh)
        self.refresh()

    def refresh(self):
        if self.frame is not None:
            self.frame.destroy()
        self.frame = Example(self.root)
        self.frame.grid(...)

不过,子类化 Frame 并没有什么神奇之处.您只需要一个函数来创建一个框架并在其中放置一堆小部件.当您想要刷新时,只需删除框架并再次调用您的函数即可.使用类更方便一些,但类并不是绝对必要的.

Though,there's nothing really magical about subclassing Frame. You just need to have a function that creates a frame and puts a bunch of widgets in it. When you want to refresh, just delete the frame and call your function again. Using a class is a bit more convenient, but a class isn't strictly necessary.