I am creating frames for my project and so far I have it where it goes from the home page to the main page when the button is clicked. The problem I am facing is when I try to go from the home page to the log page where I am faced with an issue of calling the show_frame() function (located in MainApplication) in class MainPage.
How would I go about using arguments in MainPage so I can move from main page to log page?
class MainApplication(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
# initialize frames
self.frames = {}
for F in (HomePage, MainPage, LogPage):
frame = F(container, self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky="nsew")
# show home page frame first
self.show_frame(HomePage)
def show_frame(self, cont): # <-- FUNCTION HERE
frame = self.frames[cont]
frame.tkraise()
class HomePage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
continue_button = ttk.Button(self, text="Enter program", width=15,
command=lambda: controller.show_frame(MainPage)) # <-- works here
continue_button.pack()
class MainPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
def success_actions(self):
self.run_script_button["text"] = "View log"
self.run_script_button.configure(
command=lambda: controller.show_frame(LogPage)) # <-- want to use here
class LogPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
pass
CodePudding user response:
It works only in HomePage because you made it inside the __init__()
method but in the MainPage you need it outside.
To solve this try setting controller as an instance variable:
class MainPage(tk.Frame):
def __init__(self, parent, controller):
self.controller = controller
tk.Frame.__init__(self, parent)
def success_actions(self):
self.run_script_button["text"] = "View log"
self.run_script_button.configure(
command=lambda: self.controller.show_frame(LogPage))