0
votes

I suppose this is a simple question but I can't seem to figure it out how to properly access a variable from another class. I am trying to access the variable dir_name (located in class MainPage) so it can be opened using subprocess library when the frame transitions to class LogPage.

class MainApplication(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)

        # initialize container
        container = tk.Frame(self)
        container.pack(side="top", fill="both", expand=True)
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)

        # initialize frames
        self.frames = {}
        for F in (HomePage, MainPage, LogPage):
            frame = F(container, self)
            self.frames[F] = frame
            frame.grid(row=0, column=0, sticky="nsew")

        # show home page frame first
        self.show_frame(HomePage)

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

class MainPage(tk.Frame):
    def __init__(self, parent):
        tk.Frame.__init__(self, parent)

    def get_dir(self):
        # ask user to select directory and display selection as label
        dir_name = filedialog.askdirectory(mustexist=True) # <-- variable to access

class LogPage(tk.Frame):
    def __init__(self, parent):
        tk.Frame.__init__(self, parent)

        back_button = ttk.Button(self, text="View changes", width=25,)
                                 # command=call(["open", dir_name])) <-- want to access here
        back_button.pack(side="left", pady=(0, 10), padx=(33, 0))
Please update your question with the code which creates instances of these classes. - quamrana
dir_name is an instance variable. Does the instance of LogPage have access to the instance of MainPage that contains this data? - Pranav Hosangadi