python - 通过 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()
解决方案
您正在创建一个新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)
推荐阅读
- reactjs - 无法解决反应原生地图中的依赖关系?
- listview - Listview else 语句显示长列表
- python - python ldap3为查询设置时区
- javascript - 传递函数的动态 id
- wpf - C# WPF 使用带有 Button.Tag 的多图像源 ControlTemplate
- jquery - 如何使用jQuery从下拉列表中获取所选值的ID
- c++ - 如何使用 std::chrono 库设置特定时间?
- json - 使用 Guzzle Laravel 访问嵌套的 Json 响应
- c# - Unity 中的类继承
- ios - 在 Swift 中返回相同的函数本身有什么用