1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
|
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()
|