0

I have created a tkinter application which uses a framework very similar to the one shown in this video https://www.youtube.com/watch?v=jBUpjijYtCk

Essentially there is a main class which uses other classes below it as pages. These pages are created as soon as the program is ran.

My problem is that I need data from one of these pages to be sent to another page so that it can display the appropriate data depending on the data that is sent from the previous page.

I have simplified my program down to showcase the issue

from tkinter import *

class TkinterApp(Tk):
    def __init__(self):
        Tk.__init__(self)

        container = Frame(self)
        container.grid(row=1, column=0, sticky="nesw")
        self.frames = {}

        for p in (ClassA, ClassB):
            frame = p(parent = container, controller = self)
            self.frames[p] = frame
            frame.grid(row=0, column=0, sticky="nesw")

        self.ShowFrame(ClassA)

    def ShowFrame(self, page_name):
        page = self.frames[page_name]
        page.tkraise()
    
    def PassText(self, text):
        self.frames[ClassB].GetData(text)


class ClassA(Frame):
    def __init__(self, parent, controller):
        Frame.__init__(self, parent)
        self.controller = controller

        button1 = Button(self, text="button1", command=lambda: self.SubmitInfo("button1"))
        button1.grid(row=0, column=0)

    def SubmitInfo(self, data):
        self.controller.ShowFrame(ClassB)
        self.controller.PassText(data)


class ClassB(Frame):
    def __init__(self, parent, controller):
        Frame.__init__(self, parent)
        self.controller = controller

        self.label = Label(self, text=self.data)
        self.label.grid(column=0, row=0)

    def GetData(self, data):
        self.data = data[0]

app = TkinterApp()
app.mainloop()

Desired outcome: the text shown on the ClassB page being "button1"

Current outcome: AttributeError: 'ClassB' object has no attribute 'data' to my knowledge, this is happening as the class TkinterApp is causing ClassB to be ran before the value of data exists.

Any help would be greatly appreciated

Masha
  • 19
  • 3
  • 1
    Do either of these answer your question? https://stackoverflow.com/q/32212408/7432 https://stackoverflow.com/questions/33646605/how-to-access-variables-from-different-classes-in-tkinter – Bryan Oakley Mar 27 '22 at 14:42
  • @BryanOakley This works, but only for when a button is calling the variable. The issue with mine is that the data on the page needs to change depending on an input on a previous page – Masha Mar 27 '22 at 21:26
  • I recommend looking at all of the answers linked to here: https://stackoverflow.com/a/7557028/7432 – Bryan Oakley Mar 27 '22 at 22:31

1 Answers1

1

In line 44, remove Label widget parameter text=self.data

Add .configure in line 49, self.label.configure(text=self.data)

snippet:

class ClassB(Frame):
    def __init__(self, parent, controller):
        Frame.__init__(self, parent)
        self.controller = controller

        self.label = Label(self) #<-- line 44
        self.label.grid(column=0, row=0)

    def GetData(self, data):
        self.data = data[0]
        self.label.configure(text=self.data) #Add in <---line 49
toyota Supra
  • 3,181
  • 4
  • 15
  • 19