0

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!

Blaiz
  • 105
  • 1
  • 6
  • 12
  • why are you using a label to store a file path. Why not just use a variable `filePath = "my/file_path/to/file.txt"` and if you want to be able to share information between classes you will need to store that data as a class attribute using `self.` – Mike - SMT Sep 11 '17 at 15:28
  • 1
    You also need to provide a testable example code. You 2 classes are not functional without the rest of your code as `UploadPage` and `PageOne` don't get called in your example. – Mike - SMT Sep 11 '17 at 15:30
  • Please create a [mcve]. The answer probably lies in leveraging the controller, but we have no idea what it does or what it looks like. – Bryan Oakley Sep 11 '17 at 16:03
  • My hunch is that this is a duplicate of https://stackoverflow.com/questions/33646605/how-to-access-variables-from-different-classes-in-tkinter-python-3 and/or https://stackoverflow.com/questions/32212408/how-to-get-variable-data-from-a-class – Bryan Oakley Sep 11 '17 at 16:04

1 Answers1

0

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)
Right leg
  • 16,080
  • 7
  • 48
  • 81
  • Normally, the point of the controller is so that you don't have to pass instances around. Instead, ask the controller for an instance, or have the controller maintain a shared data structure. The problem with this solution is that the controller needs to know which pages need to know about other pages. – Bryan Oakley Sep 11 '17 at 16:42