0

I am making a project in Tkinter where I gather some information in one frame (let's call it Frame One) and, based on that information, I want to change the layout of a different frame (let's call it Frame Two).

The code I'm attaching is a bare-bones version of what I want to achieve (my project is about 630 lines long).

The problem I ran into was how to access the frame, which is a class. Googling I came across Bryan Oakley's response on https://stackoverflow.com/questions/48731097/calling-functions-from-a-tkinter-frame-to-another#= , but when I tried to replicate it, I got an error.

EDIT: the following modification made it work, so it currently appends "testing" to testText when the button on PageTwo is clicked.



from tkinter import *
from tkinter import ttk

class MyApp(Tk):

    def __init__(self):
        Tk.__init__(self)
        container = ttk.Frame(self)
        container.pack(side="top", fill="both", expand = True)
        self.frames = {}
        for F in (PageOne, PageTwo):
            frame = F(container, self)
            self.frames[F] = frame
            frame.grid(row=0, column=0, sticky = NSEW)
        self.show_frame(PageOne)

    def show_frame(self, cont):
        frame = self.frames[cont]
        frame.tkraise()

    def get_page(self, classname):
        '''Returns an instance of a page given its class name as a string'''
        for page in self.frames.values():
            if str(page.__class__.__name__) == classname:
                return page
        return None
    def test_get_page(self, page_class):
        return self.frames[page_class]
class PageOne(ttk.Frame):
    def __init__(self, parent, controller):
        self.controller=controller
        ttk.Frame.__init__(self, parent)
        ttk.Label(self, text='PageOne').grid()
        self.testText = Text(self, height=2, width=30)

        button1 = ttk.Button(self, text='Next Page',
                                  command=lambda: controller.show_frame(PageTwo))
        self.testText.grid()
        button1.grid()


class PageTwo(ttk.Frame):
    def __init__(self, parent, controller):
        self.controller=controller
        ttk.Frame.__init__(self, parent)
        ttk.Label(self, text='PageTwo').grid()        
        button1 = ttk.Button(self, text='Previous Page',
                             command=self.testButton)#controller.show_frame(PageOne))

        button1.grid()

    def testButton(self):
        page = self.controller.test_get_page(PageOne)
        page.testText.insert(END,'testing')       
        self.controller.show_frame(PageOne)

app = MyApp()
app.title('Multi-Page Test App')
app.mainloop()



The expected result would have been for PageOne's "testText" widget to show "testing" once the 'Previous Page' button on PageTwo was clicked, but instead I got the following error:

"Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\Max\AppData\Local\Programs\Python\Python36-32\lib\tkinter\__init__.py", line 1702, in __call__
    return self.func(*args)
  File "C:\Users\Max\Dropbox\Programación\prueba.py", line 54, in testButton
    page.testText.insert(0,'testing')
AttributeError: 'PageOne' object has no attribute 'testText'"
Juan Dougnac
  • 95
  • 10

1 Answers1

1

The error is given in the stack trace. The 'testText' variable is not currently an instance variable of the PageOne object. You should set self.testText = Text(self, height=2, width=30)