'方法'对象不可订阅

时间:2017-04-07 12:07:16

标签: python python-3.x tkinter

我试图将Bryan Oakley的解决方案用于this question但是当我尝试编辑框架以符合我的目的时,我得到了这个错误。我的代码是:

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

很抱歉,如果答案很明显,我很久没有使用过tkinter了。

1 个答案:

答案 0 :(得分:2)

你刚忘了一个'在show_frame

self.frame[page_name] = frame替换为self.frames[page_name] = frame,代码应该运行。