Edited version:
I am new to Tkinter and would like to perform a page switching functionality similar to the answer to this question (Switch between two frames in tkinter?). However, rather than have all of my pages in one single .py file, I would like to call each of the pages from its own .py file.
The reason why I want this is that later I will have multiple sub-pages, which will each represent different tools.
My question is,
How can I still keep the same frame switching functionality while having each of my pages being called from different python files?
Here is what I have tried so far:
app.py
from page1 import *
import tkinter as tk
from tkinter import Tk, Frame, ttk
class AppWindow(Tk):
def __init__(self, *args, **kwargs):
Tk.__init__(self, *args, **kwargs)
# create a container for all the widgets (buttons, etc)
container = 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 loops for switching between pages
for F in (HomePage, PageOne, Tool1):
frame = F(container, self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame(HomePage)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
class HomePage(Frame):
def __init__(self, parent, controller):
Frame.__init__(self, parent)
label = ttk.Label(self, text="App Main Window", font=LARGE_FONT)
label.pack(pady=10, padx=10)
proc_btn = ttk.Button(self, text="Go to Page One",
command=lambda: controller.show_frame(PageOne))
proc_btn.pack(ipadx=5, ipady=5, expand=1)
if __name__ == "__main__":
app = AppWindow()
app.mainloop()
page1.py
from apptest import *
import tkinter as tk
from tkinter import Tk, Frame, ttk
class PageOne(Frame):
def __init__(self, parent, controller):
Frame.__init__(self, parent)
label = ttk.Label(self, text="Page One !!!!!!!!", font=LARGE_FONT)
label.pack(pady=10, padx=10)
proc1_btn = ttk.Button(self, text="Tool 1",
command=lambda: controller.show_frame(Tool1))
proc1_btn.pack(ipadx=5, ipady=5, expand=1)
home_btn = ttk.Button(self, text="Homepage",
command=lambda: controller.show_frame(HomePage))
home_btn.pack(ipadx=5, ipady=5, expand=1)
class Tool1(Frame):
def __init__(self, parent, controller):
Frame.__init__(self, parent)
label = ttk.Label(self, text="Tool 1 !!!!!!!!", font=LARGE_FONT)
label.pack(pady=10, padx=10)
# Find what
ttk.Label(self, text='Find:')
keyword = ttk.Entry(self, width=30)
keyword.focus()
keyword.pack(ipadx=5, ipady=5, expand=1)
# Replace with:
ttk.Label(self, text='Replace:')
replacement = ttk.Entry(self, width=30)
replacement.pack(ipadx=5, ipady=5, expand=1)
proc1_btn = ttk.Button(self, text="PageOne",
command=lambda: controller.show_frame(PageOne))
proc1_btn.pack(ipadx=5, ipady=5, expand=1)
home_btn = ttk.Button(self, text="Homepage",
command=lambda: controller.show_frame(HomePage))
home_btn.pack(ipadx=5, ipady=5, expand=1)
My problem occurs when I try to use the "HomePage" button to go back to the HomePage when I am at the "PageOne" and "Tool1" pages. Here is the error that I am seeing.
Exception in Tkinter callback
Traceback (most recent call last):
File "...\lib\tkinter\__init__.py", line 1705, in __call__
return self.func(*args)
File "...\GUI\page1.py", line 21, in <lambda>
command=lambda: controller.show_frame(HomePage))
File "C.../GUI/app.py", line 52, in show_frame
frame = self.frames[cont]
KeyError: <class 'apptest.HomePage'>
Thank you for your help in advance
Answer:
The answer to this question can be found here: Switch between two frames in tkinter in separates files