首页 > 解决方案 > 通过 tkinter 中的帧传递变量

问题描述

我正在尝试将filename变量从SearchFolderFrame类获取到ResultFrame类,但是当用户更改其中的变量时,SearchFolderFrame它不会更改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()

标签: pythonvariablestkinter

解决方案


您正在创建一个新SearchFolderFrame实例ResultFrame。这个新实例还将有一个新filename变量,默认为空。

您可以通过执行来获取旧SearchFolderFrame实例p1 = controller.frames[SearchFolderFrame]。你也不需要打电话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)

推荐阅读