0

I use the code from Switch between two frames in tkinter.

I want to pass the variable var1 from the main.py class SampleApp() to the PageOne.py. I tried severals options without succeed.

I appreciate If someone can tell me how pass the var1 to the pageOne.

Thanks

here the code:

Main.py

import tkinter as tk

from StartPage import StartPage
from pageOne import PageOne
from pageTwo import PageTwo

pages = {
    "StartPage": StartPage, 
    "PageOne": PageOne, 
    "PageTwo": PageTwo
}

class SampleApp(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)
        self._frame = None

        self.var1 = "Test"

        self.switch_frame("StartPage")

    def switch_frame(self, page_name):
        """Destroys current frame and replaces it with a new one."""
        cls = pages[page_name]
        new_frame = cls(master = self)
        if self._frame is not None:
            self._frame.destroy()
        self._frame = new_frame
        self._frame.pack()

if __name__ == "__main__":
    app = SampleApp()
    app.mainloop()

pageOne.py

# Multi-frame tkinter application v2.3
import tkinter as tk

class PageOne(tk.Frame):
    def __init__(self, master, var1):

        print(var1)
        
        tk.Frame.__init__(self, master)
        tk.Label(self, text="This is page one").pack(side="top", fill="x", pady=10)
        tk.Button(self, text="Return to start page", command=lambda: master.switch_frame("StartPage")).pack()

if __name__ == "__main__":
    app = PageOne()
    app.mainloop()
Alberto
  • 13
  • 3
  • Why do you need to pass it? You're already passing in the instance of `SampleApp` as `master`, and `var1` is an attribute of that class. You should be able to use `master.var1`. – Bryan Oakley Aug 20 '21 at 17:37
  • Thanks Bryan. You are right, that works. – Alberto Aug 26 '21 at 17:43

0 Answers0