如何使用刷新按钮在 python 中完全刷新 tkinter 窗口 [英] How do I refresh tkinter window totally in python with a refresh button

查看:351
本文介绍了如何使用刷新按钮在 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.

这篇关于如何使用刷新按钮在 python 中完全刷新 tkinter 窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆