tdbridger
tdbridger

Reputation: 59

'method' object is not subscriptable

I'm trying to use Bryan Oakley's solution to this question but when I try to edit the frames to fit my purpose I get this error. My code is:

import tkinter as tk

class SampleApp(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 (MainMenu, CreatePage, ViewPage):
            page_name = F.__name__
            frame = F(parent=container, controller=self)
            self.frames[page_name] = frame
            frame.grid(row=0, column=0, sticky="nsew")

        self.show_frame("MainMenu")

    def show_frame(self, page_name):
        frame = self.frame[page_name]
        frame.tkraise()

class MainMenu(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        controller.title("Main Menu")
        controller.configure(background="light green")

        titlelbl = tk.Label(controller,
                            text="GOLF SCORECARDS",
                            fg = "dark green",
                            font = "Verdana 20 bold")

        newbtn = tk.Button(controller,
                           text="Create new scorecard",
                           fg = "light green",
                           bg = "dark green", 
                           command = lambda: controller.show_frame("CreatePage"))

        viewbtn = tk.Button(controller,
                            text="View existing scorecard", 
                            fg = "light green",
                            bg = "dark green", 
                            command = lambda: controller.show_frame("ViewPage"))

        exitbtn = tk.Button(controller,
                            text="Exit the program",
                            fg = "light green",
                            bg = "dark green", 
                            command = controller.quit)

        titlelbl.pack(side="top", fill="x", pady=10)
        newbtn.pack(fill="x", padx=20, pady=30)
        viewbtn.pack(fill="x", padx=20, pady=30)
        exitbtn.pack(fill="x", padx=20, pady=30)

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

        label = tk.Label(self,
                         text="This is the create page", 
                         font = "Verdana 20 bold")

        button = tk.Button(self,
                           text="Go back to main menu",
                           command = lambda: controller.show_frame("MainMenu"))

        label.pack(side="top", fill="x", pady=10)
        button.pack()

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

        label = tk.Label(self,
                         text="This is the view page", 
                         font = "Verdana 20 bold")

        button = tk.Button(self,
                           text="Go back to main menu",
                           command = lambda: controller.show_frame("MainMenu"))

        label.pack(side="top", fill="x", pady=10)
        button.pack()

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

Sorry if the answer is obvious, I haven't been using tkinter for very long.

Upvotes: 1

Views: 3242

Answers (1)

j_4321
j_4321

Reputation: 16169

You just forgot a 's' in show_frame:

Replace self.frame[page_name] = frame by self.frames[page_name] = frame and the code should run.

Upvotes: 2

Related Questions