繁体   English   中英

Python Tkinter Frame Destroy 显示新数据

[英]Python Tkinter Frame Destroy to display new data

应用程序在首次加载时运行良好。 我需要更新每一帧的数据以显示用户所做的更改。 我知道我需要以某种方式破坏框架以重新加载数据。 关于如何编辑 switch_frame 以破坏到上一帧的任何建议?

class App(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        
        container = tk.Frame(self)
        container.pack(side="top", fill="both", expand="true")
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0,weight=1)

        self.frames = {}
 
        for F in (homepage, page2, page3):
            page_name = F.__name__
            frame = F(parent=container, controller=self)
            self.frames[page_name] = frame
            frame.grid(row=0, column=0, sticky="nsew")
        self.switch_frame("homepage")


    def switch_frame(self, frame_class):
        frame = self.frames[frame_class]
        frame.tkraise()

您可以使用:

your_frame_name.destroy()销毁框架

或者

your_label_or_something_other_like_button_name.config(text='you can change text or anyting other you want to set new data

我希望这对您有所帮助,如果您不明白,请随时再次询问

我知道我需要以某种方式破坏框架以重新加载数据。

首先,更改__init__以仅显示起始页。 此外,您可以创建一个字典,将 map 页面名称转换为页面类。

class App(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        self.pages = {
            "homepage": homepage,
            "page2": page2,
            "page3": page3,
        }

        self.container = tk.Frame(self)
        self.container.pack(side="top", fill="both", expand="true")

        self.switch_frame("homepage")

接下来,修改switch_frame以销毁self.container的旧内容,然后创建请求页面的新实例:

    def switch_frame(self, page_name):
        children = self.container.winfo_children()
        for child in children:
            child.destroy()

        frame_class = self.pages[page_name]
        frame = frame_class(self.container, controller=self)
        frame.pack(fill="both", expand=True)

你可以:

  • 销毁当前显示的框架(需要一个实例变量来保存它)
  • 在显示时创建框架
class App(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)

        # used instance variable so that it can be accessed in other class functions
        self.container = tk.Frame(self)
        self.container.pack(side="top", fill="both", expand="true")
        self.container.grid_rowconfigure(0, weight=1)
        self.container.grid_columnconfigure(0,weight=1)

        self.frames = {}
        self.frame = None  # hold the current frame shown

        for F in (homepage, page2, page3):
            page_name = F.__name__
            self.frames[page_name] = F
        self.switch_frame("homepage")


    def switch_frame(self, frame_class):
        # destroy current frame shown if any
        if self.frame:
            self.frame.destroy()
        # create the required frame and show it
        self.frame = self.frames[frame_class](self.container, self)
        self.frame.grid(row=0, column=0, sticky="nsew")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM