Passing variables between frames in Tkinter

0 votes
asked Sep 11, 2017 by blaiz

So basically I have 2 frames, UploadPage and PageOne:

class UploadPage(tk.Frame):


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




    theLabel = tk.Label(self, text='Upload your CSV here.', padx=10, pady=10)
    theButton = tk.Button(self, text='Browse', command=open_file)
    fileLabel = tk.Label(self, padx=10, pady=10)
    submitButton = tk.Button(self, text='Submit', command= lambda: controller.show_frame(PageOne))
    filePathLabel = tk.Label(self) #hidden label used to store file path
    theLabel.grid(row=0)
    theButton.grid(row=1, column=0)
    fileLabel.grid(row=1, column=1)
    submitButton.grid(row=3, column=0)

class PageOne(tk.Frame):

def __init__(self, parent, controller):
    tk.Frame.__init__(self, parent)
    self.controller = controller
    theLabel = tk.Label(self, text='Hi', padx=10, pady=10)
    theLabel.pack()
app = SeeAssBeeapp()
app.mainloop()

Say I want to get the text of filePathLabel in UploadPage and display it in PageOne. How do i do that? Thank you!

1 Answer

0 votes
answered Sep 11, 2017 by right-leg

You basically need the PageOne instance to know of the UploadPage instance. For that purpose, you can for instance pass the latter as parameter to the former's __init__ method:

def __init__(self, parent, controller, uploadPage=None):
    self.uploadPage = uploadPage
    ...

Now you can access filePathLabel from the PageOne instance:

if self.uploadPage is not None:
    self.uploadPage.filePathLabel

Of course, you will need controller to pass that UploadPage as parameter to PageOne.__init__:

# controller
myUploadPage = UploadPage(...)
myPageOne = PageOne(parent, controller, uploadPage=myUploadPage)
Welcome to Q&A, where you can ask questions and receive answers from other members of the community.
Website Online Counter

...