0

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.

0

1 Answer 1

1

The problem is that you are trying to reference the page "GamePage" before it has been created. That is because you placed the code in FrontPage.__init__. You can't reference a page before it has been created.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the answer again Bryan. I have moved the code around a bit, I'm still not getting the functionality I'm actually looking (taking user input and passing it to label on GamePage) but your answer has fixed the trace back.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.