显示当前目录下文件以及文件夹的GUI

跟着《Python 核心编程》照着做了一个能够显示当前目录的GUI小工具。

底层是调用的python的OS包

  1 from tkinter import *
  2 import os
  3 from time import sleep
  4 
  5 class DirList(object):
  6     def __init__(self, initdir=None):
  7         self.top = Tk()
  8         self.label = Label(self.top, text="Directory Lister v1.1")    #title and version
  9         self.label.pack()
 10 
 11         self.cwd = StringVar(self.top)
 12         # a label to display current directory's name
 13         self.dirl = Label(self.top, fg='blue', font=('Helvetica', 12, 'bold'))
 14         self.dirl.pack()
 15 
 16         self.dirfm = Frame(self.top)    #frame of the list box
 17         self.dirsb = Scrollbar(self.dirfm)    #scroll bar which helps display
 18         self.dirsb.pack(side=RIGHT, fill=Y)
 19         self.dirs = Listbox(self.dirfm, height=15, width=50, yscrollcommand=self.dirsb.set)    #list box
 20         self.dirs.bind('<Double-1>', self.setDirAndGo)
 21         self.dirsb.config(command=self.dirs.yview)    #correspond with scroll bar's change
 22         self.dirs.pack(side=LEFT,fill=BOTH)
 23         self.dirfm.pack()
 24 
 25         self.dirn = Entry(self.top, width=50, textvariable=self.cwd)
 26         self.dirn.bind('<Return>', self.doLS)
 27         self.dirn.pack()
 28 
 29         self.bfm = Frame(self.top)
 30         self.clr = Button(self.bfm, 
 31             text='Clear', 
 32             command=self.clrDir, 
 33             activeforeground='white', 
 34             activebackground='blue')
 35         self.ls = Button(self.bfm, 
 36             text='List Directory', 
 37             command=self.doLS,
 38             activeforeground='white',
 39             activebackground='green'
 40             )
 41         self.quit = Button(self.top,
 42             text='Quit',
 43             command=self.top.quit,
 44             activeforeground='white',
 45             activebackground='red'
 46             )
 47         self.clr.pack(side=LEFT, fill=Y)
 48         self.ls.pack(side=LEFT, fill=Y)
 49         self.quit.pack(side=LEFT, fill=Y)
 50         self.bfm.pack()
 51 
 52         if initdir:
 53             self.cwd.set(os.curdir)
 54     def clrDir(self, ev=None):
 55         self.cwd.set('')
 56     def setDirAndGo(self, ev=None):
 57         self.last = self.cwd.get()
 58         self.dirs.config(selectbackground='red')
 59         check = self.dirs.get(self.dirs.curselection())
 60         if not check:
 61             check = os.curdir
 62         self.cwd.set(check)
 63         self.doLS()
 64 
 65     def doLS(self, ev=None):
 66         error = ''
 67         tdir = self.cwd.get()
 68 
 69         if not tdir:
 70             tdir = os.curdir
 71         if not os.path.exists(tdir):
 72             error = tdir + ': no such file'
 73         elif not os.path.isdir(tdir):
 74             error = tdir + ': not a directory'
 75 
 76         if error:
 77             self.cwd.set(error)
 78             self.top.update()
 79             sleep(2)
 80             if not (hasattr(self, 'last') and self.last):
 81                 self.last = os.curdir
 82             self.cwd.set(self.last)
 83             self.dirs.config(selectbackground='LightSkyBlue')
 84             self.top.update
 85             return
 86         self.cwd.set('Fetching Directory Contents...')
 87         self.top.update()
 88         dirlist = os.listdir(tdir)
 89         dirlist.sort()
 90         os.chdir(tdir)
 91         self.dirl.config(text=os.getcwd())
 92         self.dirs.delete(0, END)
 93         self.dirs.insert(END, os.curdir)
 94         self.dirs.insert(END, os.pardir)
 95         for e in dirlist:
 96             self.dirs.insert(END, e)
 97         self.cwd.set(os.curdir)
 98         self.dirs.config(selectbackground='LightSkyBlue')
 99 
100 
101 def main():
102     d = DirList()
103     mainloop()
104 
105 if __name__ == '__main__':
106     main()
107 
108         
View Code

 版面搞的非常难看,不知道该怎么调整。

猜你喜欢

转载自www.cnblogs.com/AcodingDg/p/10152831.html