在多个 Tkinter GUI 框架之间导航 [英] Navigating between multiple Tkinter GUI frames

查看:37
本文介绍了在多个 Tkinter GUI 框架之间导航的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在网上找到了一些代码来创建具有多个框架的 tkinter GUI.我尝试修改代码以包含一个框架,每个框架上都显示导航按钮,而不是每次都重复代码.我不断收到错误消息,无法弄清楚如何将导航按钮连接到相应的框架.这是我得到的最接近的代码:

I found some code online to create a tkinter GUI with multiple frames. I tried to modify the code to include a frame with navigation buttons to be displayed on each frame, rather than repeating the code each time. I keep getting error messages and cannot work out how to connect the navigation buttons to the corresponding frame. This is the code for the closest I have got:

import Tkinter as tk


LARGE_FONT= ("Verdana", 12)

class SeaofBTCapp(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 (StartPage, PageOne, PageTwo):

            frame = F(container, self)

            self.frames[F] = frame

            frame.grid(row=0, column=0, sticky="nsew")

        self.show_frame(StartPage)

    def show_frame(self, cont):

        frame = self.frames[cont]
        frame.tkraise()


class StartPage(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self,parent)
        label = tk.Label(self, text="Start Page", font=LARGE_FONT)
        label.pack(pady=10,padx=10)

        button = tk.Button(self, text="Visit Page 1",
                        command=lambda: controller.show_frame(PageOne))
        button.pack()

        button2 = tk.Button(self, text="Visit Page 2",
                        command=lambda: controller.show_frame(PageTwo))
        button2.pack()


class PageOne(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        label = tk.Label(self, text="Page One!!!", font=LARGE_FONT)
        label.pack(pady=10,padx=10)

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

        button2 = tk.Button(self, text="Page Two",
                        command=lambda: controller.show_frame(PageTwo))
        button2.pack()

        btns = MainButtonFrame(self,SeaofBTCapp)
        btns.pack()


class PageTwo(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        label = tk.Label(self, text="Page Two!!!", font=LARGE_FONT)
        label.pack(pady=10,padx=10)

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

        button2 = tk.Button(self, text="Page One",
                        command=lambda: controller.show_frame(PageOne))
        button2.pack()

        btns = MainButtonFrame(self,SeaofBTCapp)
        btns.pack()

class MainButtonFrame(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        Pages = [StartPage,PageOne,PageTwo ]
        for button in Pages:
            NewButton = tk.Button(self, text=str(button),
                        command=lambda: controller.show_frame(button))
            NewButton.pack()

app = SeaofBTCapp()
app.mainloop()

我得到的错误信息是:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Python27\lib\lib-tk\Tkinter.py", line 1536, in __call__
    return self.func(*args)
  File "<ipython-input-13-dcc5fdbf1581>", line 103, in <lambda>
    command=lambda: controller.show_frame(button))
TypeError: unbound method show_frame() must be called with SeaofBTCapp 
instance as first argument (got classobj instance instead)

如果您能建议我在按钮命令中做错了什么,我们将不胜感激.如果我可以让它工作,我将使用它作为框架来构建更大的 GUI.谢谢!

Any help would be much appreciated in suggesting what I am doing wrong in the button commands. If I can get this working I will use it as a framework to build a bigger GUI on. Thanks!

推荐答案

MainButtonFrame 的初始化器需要一个父级和一个控制器.控制器必须是主应用程序的实例,而不是.将您创建 MainButtonFrame 的方式更改为:

The initializer of MainButtonFrame needs a parent and a controller. The controller must be the instance of the main app, not the class. Change how you create MainButtonFrame to be this:

btns = MainButtonFrame(self,controller)

这篇关于在多个 Tkinter GUI 框架之间导航的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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