1

I am trying to get the filename variable from the SearchFolderFrame class to the ResultFrame class but when the user changes the variable in SearchFolderFrame it doesn't change in ResultFrame.

class ImageGroupingApp(tkinter.Tk):
    #initalise variables in the class
    def __init__(self, *args, **kwargs):
        #initalise tkinter
        tkinter.Tk.__init__(self,*args,**kwargs)
        #creating a frame to contain everything in the app
        container = tkinter.Frame(self)
        #declaring the position of the frame
        container.pack(fill="both",expand = True)
        
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)

        #specifing a dictionary that contains all the frames
        self.frames = {}

        for F in (SearchFolderFrame,ResultFrame):
            #setting frame to the first frame the user sees
            frame = F(container,self)

            self.frames[F] = frame
            #sticky is used for alinment
            frame.grid(row=0, column=0, sticky="nsew")
            
        
        
        self.show_frame(SearchFolderFrame)
        
    #creating a method to show frames
    def show_frame(self,cont):
        frame = self.frames[cont]
        
        frame.tkraise()

class SearchFolderFrame(tkinter.Frame):
    def __init__(self,parent, controller):
        tkinter.Frame.__init__(self,parent)
        
        self.filename = tkinter.StringVar()

        def select_folder():
            filePath = tkinter.filedialog.askdirectory()
            self.filename.set(filePath)
            #print(filePath)

        def get_filename():
            return self.filename

        def searchBtn():
            self.path = self.filename
            controller.show_frame(ResultFrame)

        selectFolderLabel = tkinter.Label(self, text = "Select Folder:")
        selectFolderLabel.grid(row=0,column=0)
        selectFolderLabel.columnconfigure(1,weight=1)
        selectFolderLabel.rowconfigure(1,weight=1)

        thresholdLabel = tkinter.Label(self, text = "Threshold(%):")
        thresholdLabel.grid(row=1,column=0)
        thresholdLabel.columnconfigure(1,weight=1)
        thresholdLabel.rowconfigure(1,weight=1)

        featuresLabel = tkinter.Label(self, text = "Features:")
        featuresLabel.grid(row=2,column=0)
        featuresLabel.columnconfigure(1,weight=1)
        featuresLabel.rowconfigure(1,weight=1)

        searchBtn = tkinter.Button(self,text="Search",
                                   command=lambda: controller.show_frame(ResultFrame))
        searchBtn.grid(row=3,column=1)
        searchBtn.columnconfigure(1,weight=1)
        searchBtn.rowconfigure(1,weight=1)

        selectFolderBtn = tkinter.Button(self,text="...",command = select_folder)
        selectFolderBtn.grid(row=0,column=2)
        selectFolderBtn.columnconfigure(1,weight=1)
        selectFolderBtn.rowconfigure(1,weight=1)

        folderPathLabel = tkinter.Label(self, textvariable = self.filename)
        folderPathLabel.grid(row=0,column=1)
        folderPathLabel.columnconfigure(1,weight=1)
        folderPathLabel.rowconfigure(1,weight=1)


class ResultFrame(tkinter.Frame):
    def __init__(self,parent, controller):
        tkinter.Frame.__init__(self,parent)

        p1 = SearchFolderFrame(parent, controller)
        self.path = p1.filename
        self.path.set(p1.filename)
        print(self.path)

        pathLabel = tkinter.Label(self, textVariable = self.path)
        pathLabel.grid(row=0,column=1)
        pathLabel.columnconfigure(1,weight=1)
        pathLabel.rowconfigure(1,weight=1)


if __name__ == '__main__':
    app = ImageGroupingApp()
    app.title("Image Grouping")
    app.geometry('700x500')
    app.mainloop()

martineau
  • 119,623
  • 25
  • 170
  • 301

1 Answers1

0

You are creating a new SearchFolderFrame instance in ResultFrame. This new instance will also have a new filename variable, which defaults to being empty.

You can get the old SearchFolderFrame instance by doing p1 = controller.frames[SearchFolderFrame]. You also need to not call self.path.set(p1.filename):

class ResultFrame(tkinter.Frame):
    def __init__(self,parent, controller):
        tkinter.Frame.__init__(self,parent)

        p1 = controller.frames[SearchFolderFrame]
        self.path = p1.filename
        print(self.path)

        pathLabel = tkinter.Label(self, textVariable = self.path)
        pathLabel.grid(row=0,column=1)
        pathLabel.columnconfigure(1,weight=1)
        pathLabel.rowconfigure(1,weight=1)
MegaIng
  • 7,361
  • 1
  • 22
  • 35