I'm trying to learn how to work with frames in tkinter, wrote it in a way every page is a different class:
import tkinter as tk
class App(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self.title("Multiple Frames Test")
self.resizable(False, False)
StartPage().pack()
def switch_frames(self, to_remove, to_add):
to_remove.pack_forget()
to_add.pack()
class StartPage(tk.Frame, App):
def __init__(self):
tk.Frame.__init__(self)
self.title1 = tk.Label(self, text="This is an awesome label")
self.title1.grid(row=0, column=0, padx=(20, 20), pady=(20, 20))
self.switch_btn1 = tk.Button(self, text="Switch to SecondaryPage", command=lambda: self.switch_frames(self, SecondaryPage))
self.switch_btn1.grid(row=1, column=0, padx=(20, 20), pady=(0, 20))
class SecondaryPage(tk.Frame, App):
def __init__(self):
tk.Frame.__init__(self)
self.title2 = tk.Label(self, text="This is also an awesome label")
self.title2.grid(row=0, column=0, padx=(20, 20), pady=(20, 20))
self.switch_btn2 = tk.Button(self, text="Switch to StartPage")
self.switch_btn2.grid(row=1, column=0, padx=(20, 20), pady=(0, 20))
my_app = App()
my_app.mainloop()
After pressing the button to switch to SecondaryPage I get the error:
File "c:\Users...", line 23, in
self.switch_btn1 = tk.Button(self, text="Switch to SecondaryPage", command=lambda: self.switch_frames(self, SecondaryPage))
File "c:\Users...", line 14, in switch_frames
to_add.pack()
TypeError: pack_configure() missing 1 required positional argument: 'self'
to_add.pack()requires a reference on the left side but you use.switch_frames(..., SecondaryPage)a class definiton. Does this answer your question? TypeError: Missing one required positional argument - stovflSecondaryPage(): Are you aware, that you create a newSecondaryPage()object at every click of.Button(self, text="Switch to SecondaryPage"...)Read up on Tkinter.Pack.pack_forget-method and Stack Frames on top of each other - stovfl