在 tkinter 中在两帧之间切换的功能 [英] Function to switch between two frames in tkinter

查看:25
本文介绍了在 tkinter 中在两帧之间切换的功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在查看 passing-functions-parameters-tkinter-using-lambda,并且在他的类 PageOne(tk.Frame) 中需要更多的功能.而不是使用下面的 lambda 命令(就像他那样):

I'm looking through the code at passing-functions-parameters-tkinter-using-lambda, and needed a tad more functionality inside his class PageOne(tk.Frame). Instead of using lambda commands below (as he did):

button1 = tk.Button(self, text="Back to Home",
                        command=lambda: controller.show_frame(StartPage))`

我希望能够创建一个内部具有 if/then 层次结构的函数...专门用于检查 PageOne 上的所有其他输入是否已在此之前先完成(然后我知道该怎么做)允许改变框架.

I'd like to be able to create a function that had an if/then hierarchy inside of it... specifically to check if all other inputs on PageOne had been fulfilled first (which I then know how to do) before allowing a frame change.

如果这可以使用 lambda 单独完成,那就更好了.谁能帮帮我?

If this can be done individually using lambda, even better. Can anyone help me out?

更新:使用 Bryan 的建议并重新格式化他链接的原始代码,我现在有:

Update: Using Bryan's advice and reformatting for the original code he linked, I now have:

class App(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        tk.Tk.wm_title(self, "APP") #window heading
        self.title_font = tkfont.Font(family='Helvetica', size=12) #options: weight="bold",slant="italic"

        container = tk.Frame(self) #container = stack of frames; one on top is visible
        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):
            page_name = F.__name__
            frame = F(parent=container, controller=self)
            self.frames[page_name] = frame #puts all pages in stacked order
            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"))
        button1.pack()
        button2.pack()

class PageOne(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller

####FIX PART 1####
        self.next1 = tk.Button(self,text="Next",padx=18,highlightbackground="black", 
                           command=lambda: self.maybe_switch("PageTwo"))  
        self.next1.grid(row=10,column=1,sticky='E')

####FIX PART 2####
    def maybe_switch(self, page_name):
        if ###SOMETHING###:
            self.controller.show_frame(page_name)

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

推荐答案

您不应该在 lambda 中放入任何逻辑.只需创建一个具有您想要的任何逻辑的普通函数,然后从按钮调用它.真的没有那么复杂.

You shouldn't put any logic in a lambda. Just create a normal function that has any logic you want, and call it from the button. It's really no more complicated that that.

class SomePage(...):
    def __init__(...):
        ...
        button1 = tk.Button(self, text="Back to Home", 
                        command=lambda: self.maybe_switch_page(StartPage))
        ...

    def maybe_switch_page(self, destination_page):
        if ...:
            self.controller.show_frame(destination_page)
        else:
            ...

如果您想要更通用的解决方案,请将逻辑移至 show_frame,并让它调用当前页面上的方法以验证是否可以切换.

If you want a more general purpose solution, move the logic to show_frame, and have it call a method on the current page to verify that it is OK to switch.

例如:

    class Controller(...):
        ...
        def show_frame(self, destination):
            if self.current_page is None or self.current_page.ok_to_switch():
                # switch the page
            else:
                # don't switch the page

然后,只需在每个页面类中实现 ok_to_switch 即可.

Then, it's just a matter of implementing ok_to_switch in every page class.

这篇关于在 tkinter 中在两帧之间切换的功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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