在button cli上从另一个类更改类对象

2024-10-04 03:22:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我在用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()


Tags: pathtextselfmastertopdefcolumnbutton