I have built a multipage tkinter app. The app is structured so that the main GUI is stored in one .py file and the other pages stored in a dict within the main app. the main app is referenced as self.controller in each of the pages.
When I try to use a method from the controller, some times it produces a KeyError and sometimes it doesn't and for the life of me I can't figure out why.
I have rewritten this set of pages a lot based on answers provided by the community and each time I do it fixes a problem but it still hasn't solved the issue I'm trying to solve (passing info between pages"
the code below is my main app (I'll blackify the code later)
import tkinter as tk
from front_page import FrontPage
from main_page import GamePage
class GUI(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
container = tk.Frame(self)
# Initialize Window
container.pack(side="top", fill="both", expand=True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
#Initialise each page
self.frames = {}
for F in (FrontPage, GamePage):
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("FrontPage")#This one works perfecly
# shows the desired frame
def show_frame(self, page_name):
frame = self.frames[page_name]
frame.tkraise()
def get_page(self, page_name):#I think the problem is actually here
return self.frames[page_name]
app = GUI()
app.geometry("400x600")
app.mainloop()
now on the FrontPage I use a button push to configure a label on the GamePage and that is where I'm getting the traceback. The implementation for this is:
class FrontPage(tk.Frame):
"""This is a class to make the front page
it inherits from tk.Frame
"""
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
self.mainframe = tk.Frame(self)
self.mainframe.pack(expand=True, fill=tk.BOTH)
(...)
gamepage = self.controller.get_page("GamePage")#this is the problem
self.forward_label = gamepage.name_label
#when I comment out these lines everything else works
def on_button(self):
self.output_frame.configure(text = self.name_entry.get())
self.forward_label.configure(text = self.name_entry.get())
this code produces the traceback
Traceback (most recent call last):
File "/Users/kevinomalley/Desktop/python_work/rapid_rpg/app_GUI.py", line 50, in
app = GUI()
File "/Users/kevinomalley/Desktop/python_work/rapid_rpg/app_GUI.py", line 22, in init
frame = F(parent=container, controller=self)
File "/Users/kevinomalley/Desktop/python_work/rapid_rpg/front_page.py", line 106, in init
gamepage = self.controller.get_page("GamePage")
File "/Users/kevinomalley/Desktop/python_work/rapid_rpg/app_GUI.py", line 40, in get_page
return self.frames[page_name]KeyError: 'GamePage'
I have no Idea why I can call the controller.show_frame('') method without issue, but the controller.get_page('') method returns the keyerror.
Help would be much appreciated.