Using the below code originally posted here how can i use a 4th module with the PageList()
command and a new page class below it to display it in my WindowHandler()
class? through other research i believe you would have to stop using Windowhandler()
as an instance of the main application class.
The goal of this is to have the stacked frames in different files while each file can update the frames={}
list without any having to add it in the main class. a similar example is given here for different pages but the call to those pages must still be added within the main class.
Nav.py
import tkinter as tk
from Page import PageList
import Mypages
class Windowhandler(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)
PageList("Page1", container, self, Mypages.PageOne)
PageList("Page2", container, self, Mypages.PageTwo)
self.show_frame("Page1")
def show_frame(self, cont):
frameref = PageList.frames[cont]
print(frameref)
frameref.tkraise()
app = Windowhandler()
app.mainloop()
Page.py
class PageList():
frames = {}
def __init__(self, name, parent, cont, ref):
self.frames[name] = ref(parent=parent, controller=cont)
Mypages.py
import tkinter as tk
class PageOne(tk.Frame):
def __init__(self, parent, controller):
this = tk.Frame.__init__(self, parent)
label = tk.Label(this, text="Welcome to Page 1")
label.pack(pady=10, padx=10)
button1 = tk.Button(this, text="Back to Home",
command=lambda: controller.show_frame("Page2"))
button1.pack()
class PageTwo(tk.Frame):
def __init__(self, parent, controller):
this = tk.Frame.__init__(self, parent)
label = tk.Label(this, text="Welcome to Page 2")
label.pack(pady=10, padx=10)
button1 = tk.Button(this, text="Back to Home",
command=lambda: controller.show_frame("NewPage"))
button1.pack()
Psuedo.py
import tkinter as tk
import Nav
PageList("NewPage", Nav.container, WindowHandler, NewPage)
class NewPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
btn1 = Nav(self, text="A Button" command=lambda: Controller.show_frame("Page1"))
btn1.pack(pady=(10, 4))