我正在使用Tkinter编写基本的文件浏览器。我有一个具有文件列表框的主窗口。单击按钮时,将打开搜索窗口。用户输入搜索字符串,它看起来是否可以找到它。我这部分工作正常。我的问题是,我无法弄清楚如何使该文件/路径回到主窗口以刷新列表框。
class mainWindow(object):
def __init__(self,master):
self.master=master
self.homePath = "/home/scs/python"
self.fromPath = self.homePath
self.toPath = self.homePath
self.lbl=Label(master,text = "Files").grid(row=0,column=0)
self.lbl1=Label(master, text = "Destination").grid(row=0,column=2)
self.lblArrow=Label(master,text="-->").grid(row=0, column=1)
self.lbFrom=Listbox(master)
self.lbFrom.grid(row=1,column=0)
self.lbFrom.bind('<Double-Button-1>', lambda evt: self.onDoubleClk(evt, self.fromPath, self.lbFrom, "f"))
self.lbTo=Listbox(master)
self.lbTo.grid(row=1,column=2)
self.lbTo.bind('<Double-Button-1>', lambda evt: self.onDoubleClk(evt,self.toPath, self.lbTo, "t"))
self.btnSearch=Button(master, text="Search", command=self.searchValue)
self.btnSearch.grid(row=7,column=1)
self.btnCp=Button(master, text="Copy", command=self.copyFile)
self.btnCp.grid(row=5,column=1)
self.btnMv=Button(master, text="Move", command=self.moveFile)
self.btnMv.grid(row=6,column=1)
self.listItems(self.fromPath, self.lbFrom)
self.listItems(self.toPath, self.lbTo)
def listItems(self, path, box):
#print "Received path: " + path
box.delete(0,'end')
i = 0
if path != self.homePath:
box.insert(i+1, "../")
i += 1
for (path, dirs, files) in os.walk(path, topdown=True):
#print "Path: "+ str(fromPath)
#print "Dirs: " + str(dirs)
#print "Files: " + str(files)
for d in dirs:
box.insert(i+1, "D - " + d)
i += 1
for f in files:
box.insert(i+1, f)
i += 1
break
def searchValue(self):
self.w=searchWindow(self.master, self.homePath, self.lbFrom)
self.master.wait_window(self.w.top)
class searchWindow(object):
def __init__(self, master, homePath, listBox):
top=self.top=Toplevel(master)
x = root.winfo_x()
y = root.winfo_y()
self.homePath = homePath
self.listBox = listBox
top.geometry("+%d+%d" % (x+100, y+100))
self.l=Label(top,text="File Search")
self.l.grid(row=0,column=1)
self.e=Entry(top)
self.e.grid(row=0,column=2, columnspan=2)
self.ok=Button(top,text="Ok", command=self.searchOk)
self.ok.grid(row=1,column=1)
self.cancel=Button(top, text="Cancel", command = self.cancel)
self.cancel.grid(row=1,column=2)
homePath = "Blah"
def searchOk(self):
self.searchValue=self.e.get()
found=False
print "serach term: " + self.searchValue
for (path, dirs, files) in os.walk(self.homePath, topdown=True):
for f in files:
if self.searchValue == f:
print "Found it"
print os.path.abspath(f)
found=True
break
else:
print "didn't find it"
if found:
break
if not found:
self.popup()
self.top.destroy()
def cancel(self):
self.top.destroy()
def popup(self):
p=Tk()
l = Label(p, text="Search not found")
b = Button(p, text="ok", command=p.destroy)
l.pack()
b.pack()
p.mainloop()