MVC tkinter 模型。在帧之间切换:在 运行 上添加新帧

MVC tkinter model. Switch between frames: add new one on the run

我正在遵循 Brian 在回答“在 tkinter 中的两个帧之间切换”时制作的 MVC 模型。他把画框叠起来(都是一开始就做的),然后我们随意展示。

我尝试在 运行 上添加另一个框架。我知道它已创建(因为在创建它之前出现错误,之后没有错误),但我无法显示它。我错过了什么? 谢谢

import tkinter as tk                # python 3
from tkinter import font  as tkfont # python 3
#import Tkinter as tk     # python 2
#import tkFont as tkfont  # python 2

class SampleApp(tk.Tk):

    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)

        self.title_font = tkfont.Font(family='Helvetica', size=18, weight="bold", slant="italic")

        # the container is where we'll stack a bunch of frames
        # on top of each other, then the one we want visible
        # will be raised above the others
        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 (StartPage, PageOne, PageTwo):
            page_name = F.__name__
            frame = F(parent=container, controller=self)
            self.frames[page_name] = frame

            # put all of the pages in the same location;
            # the one on the top of the stacking order
            # will be the one that is visible.
            frame.grid(row=0, column=0, sticky="nsew")

        self.show_frame("StartPage")

    def show_frame(self, page_name):
        '''Show a frame for the given page name'''
        frame = self.frames[page_name]
        frame.tkraise()

    def add_new(self):
        ''' Create a new frame on the run '''
        self.frames["PageNew"] = PageNew(parent=tk.Frame(self), controller=self)
        self.frames["PageNew"].grid(row=0, column=0, sticky="nsew")      


class StartPage(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        label = tk.Label(self, text="This is the start page", font=controller.title_font)
        label.pack(side="top", fill="x", pady=10)

        button1 = tk.Button(self, text="Go to Page One",
                        command=lambda: controller.show_frame("PageOne"))
        button2 = tk.Button(self, text="Go to Page Two",
                        command=lambda: controller.show_frame("PageTwo"))
        button3 = tk.Button(self, text="Go to a New Page ",
                        command=lambda: controller.show_frame("PageNew"))        
        button1.pack()
        button2.pack()
        button3.pack()


class PageOne(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        label = tk.Label(self, text="This is page 1", font=controller.title_font)
        label.pack(side="top", fill="x", pady=10)
        button = tk.Button(self, text="Create a new page and go to the start page",
                       command=self.on_click)
        button.pack()

    def on_click(self):
        self.controller.add_new()
        self.controller.show_frame("StartPage")    


class PageTwo(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        label = tk.Label(self, text="This is page 2", font=controller.title_font)
        label.pack(side="top", fill="x", pady=10)
        button1 = tk.Button(self, text="Go to the start page",
                       command=lambda: controller.show_frame("StartPage"))   
        button2 = tk.Button(self, text="Go to the new page",
                       command=lambda: controller.show_frame("PageNew"))        
        button1.pack()
        button2.pack()

class PageNew(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        label = tk.Label(self, text="This is the new page", font=controller.title_font)
        label.pack(side="top", fill="x", pady=10)
        button = tk.Button(self, text="Go to the start page",
                       command=lambda: controller.show_frame("StartPage"))
        button.pack()


if __name__ == "__main__":
    app = SampleApp()
    app.mainloop()

你的问题是你的 PageNew 的父级:

self.frames["PageNew"] = PageNew(parent=tk.Frame(self), controller=self)

它的父项是您创建的 Frame,但它没有打包在 GUI 中,因此您看不到它,也看不到它的子项 PageNew。为了让 PageNew 和其他页面一样,你需要给它相同的父页面,这里是 container。 由于您将需要 __init__ 之外的 container,您需要将其设为 SampleApp 的属性,即将 container = tk.Frame(self) 替换为 self.container = tk.Frame(self)

现在,在 add_new 中,您可以使用

创建您的 PageNew

self.frames["PageNew"] = PageNew(parent=self.container, controller=self)