python - 'method' object is not subscriptable -
i'm trying use bryan oakley's solution this question when try edit frames fit purpose error. code is:
import tkinter 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 = {} 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 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 create page", font = "verdana 20 bold") button = tk.button(self, text="go 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 view page", font = "verdana 20 bold") button = tk.button(self, text="go 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 answer obvious, haven't been using tkinter long.
you forgot 's' in show_frame
:
replace self.frame[page_name] = frame
self.frames[page_name] = frame
, code should run.
Comments
Post a Comment