【问题标题】:Raising a Tkinter frame from a separate function从单独的函数中引发 Tkinter 帧
【发布时间】:2018-10-02 03:00:08
【问题描述】:

如果标题有点宽泛,请见谅。

我正在创建一个具有多个页面的 Tkinter 应用程序,并且我正在使用 this 一段代码来执行此操作。

每个页面都是一个框架,框架是通过调用“show_frame”函数来提升的。使用按钮在页面之间切换没有问题,但是我想运行某个功能并在满足条件时更改页面。

下面是一个例子:

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()


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"))
        button1.pack()
        button2.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="Go to the start page",
                           command=lambda: controller.show_frame("StartPage"))
        button.pack()

def doSomething():
    ...
    if x == y:
        "RAISE PAGE ONE"

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

如您所见,doSomething 函数位于任何类之外。我将如何从这个函数中提升 PageOne。

【问题讨论】:

    标签: python tkinter


    【解决方案1】:

    最简单的答案是使 doSomething() 成为 SampleApp 的方法。然后您就可以访问方法show_frame。但是,您的问题似乎暗示这不是一种选择。

    如果做不到这一点,我建议将 app 传递给函数,然后调用 app.show_frame("PageName")

    如果您不想将指向 app 的指针作为参数传递,则可以将 doSomething 函数传递给 app.show_frame作为 foo,然后在您希望显示页面时调用 foo("PageName")。

    例如

    def doSomething(foo):
        '''some code'''
        foo("page name")
    
    doSomething(app.show_frame)
    

    【讨论】:

    • 可以选择使其成为 SampleApp 的方法。我只是认为,如果它与类没有真正的关系,那么单独创建一个函数在编程中更为常见。如何在 SampleApp 中调用 show_frame?另外,我将如何处理参数?
    • show_frame 是 SampleApp 的一个方法,所以调用它你可以写 self.show_frame("parameter")。您的所有页面似乎都可以访问控制器 (SampleApp),因此如果要从它们调用 doSomething,您可以使用 controller.doSomething()。
    猜你喜欢
    • 2019-01-17
    • 2019-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多