import os from time import sleep from Tkinter import * class DirList(object):     def __init__(self, initDir = None):         self.top = Tk()         self.label = Label(self.top, text = 'Directory Lister')         self.label.pack()
          self.cwd = StringVar(self.top)         self.dirLabel = Label(self.top, fg = 'blue',                               font = ('Helvetica', 12, 'bold'))         self.dirLabel.pack()         self.dirFrame = Frame(self.top)         self.dirScrollbar = Scrollbar(self.dirFrame)         self.dirScrollbar.pack(side = RIGHT, fill = Y)         self.dirListbox = Listbox(self.dirFrame, height = 15,width = 50,                         yscrollcommand = self.dirScrollbar.set)         self.dirListbox.bind('<Double-l>', self.setDirAndShow)         self.dirScrollbar.config(command = self.dirListbox.yview)         self.dirListbox.pack(side = LEFT, fill = BOTH)         self.dirFrame.pack()         self.dirEntry = Entry(self.top, width = 50,                               textvariable = self.cwd)         self.dirEntry.bind('<Return>', self.showList)         self.dirEntry.pack()         self.buttonFrame = Frame(self.top)         self.clearButton = Button(self.buttonFrame, text = 'Clear',                                   command = self.clearDir,                                   activeforeground = 'white',                                   activebackground = 'blue')         self.listButton = Button(self.buttonFrame, text = 'List Directory',                                  command = self.showList,                                  activeforeground = 'white',                                  activebackground = 'green')         self.quitButton = Button(self.buttonFrame, text = 'Quit',                                  command = self.top.quit,                                  activeforeground = 'white',                                  activebackground = 'red')         self.clearButton.pack(side = LEFT)         self.listButton.pack(side = LEFT)         self.quitButton.pack(side = LEFT)         self.buttonFrame.pack()         if initDir:             self.cwd.set(os.curdir)             self.showList()
           def clearDir(self, event = None):         self.cwd.set('')          def setDirAndShow(self, event = None):         self.lastDir = self.cwd.get()         self.dirListbox.cofig(selectbackground = 'red')         check = self.dirListbox.get(self.dirListbox.curselection())         if not check:             check = os.curdir         self.cwd.set(check)         self.showList()          def showList(self, event = None):         error = ''         tmp = self.cwd.get()         if not tmp:             tmp = os.curdir         if not os .path.exists(tmp):             error = tmp + ': no such file'         elif not os.path.isdir(tmp):             error = tmp + ':not a directory'         if error:             self.cwd.set(error)             self.top.update()             sleep(2)             if not (hasattr(self, 'last') and self.lastDir):                 self.lastDir = os.curdir                 self.cwd.set(self.lastDir)                 self.dirListbox.config(selectbackground = 'LightSkyBlue')                 self.top.update()                 return         self.cwd.set('Fetching Directory contents...')         self.top.update()         dirlist = os.listdir(tmp)         os.chdir(tmp)         self.dirLabel.config(text = os.getcwd())         self.dirListbox.delete(0, END)         self.dirListbox.insert(END, os.curdir)         self.dirListbox.insert(END, os.pardir)         for eachFile in dirlist:             self.dirListbox.insert(END, eachFile)             self.cwd.set(os.curdir)             self.dirListbox.config(selectbackground = 'LightSkyBlue')
  def main():     dirList = DirList(os.curdir)     dirList.top.mainloop()     dirList.top.destroy() if __name__ == '__main__':     main()
 
  |