Python GUI编程实例

  1 import os
  2 from time import sleep
  3 from tkinter import *
  4 from tkinter.messagebox import showinfo
  5 
  6 
  7 class DirList(object):
  8     def __init__(self, initdir=None):
  9         self.top = Tk()
 10         self.label = Label(master=self.top, text='Directory Lister V1.0')
 11         self.label.pack()
 12 
 13         self.cwd = StringVar(master=self.top)
 14 
 15         self.dirl = Label(self.top, fg='blue', font=('Helvetica', 14, 'bold'))
 16         self.dirl.pack()
 17 
 18         self.dirfm = Frame(master=self.top)
 19         self.dirsb = Scrollbar(master=self.dirfm)
 20         self.dirsb.pack(side=RIGHT,fill=Y)    # fill=Y,垂直填充空间排列
 21 
 22         self.dirs = Listbox(master=self.dirfm, height=15, width=50, yscrollcommand=self.dirsb.set)
 23         self.dirs.bind('<Double-1>', func=self.setDirAndGo)     # <Double-1>,双击显示路径列表
 24         self.dirsb.config(command=self.dirs.yview)
 25         self.dirs.pack(side=LEFT, fill=BOTH)
 26         self.dirfm.pack()
 27 
 28         self.dirn = Entry(master=self.top, width=50, textvariable=self.cwd)
 29         self.dirn.bind('<Return>', func=self.doLS)
 30         self.dirn.pack()
 31 
 32         self.bfm = Frame(master=self.top)
 33         self.cleer = Button(master=self.bfm, text='清除', command=self.clrDir, activeforeground='white',
 34                           activebackground='blue')
 35         self.ls = Button(master=self.bfm, text='显示列表', command=self.doLS, activeforeground='white',
 36                          activebackground='green')
 37         self.quit = Button(master=self.bfm, text='退出', command=self.top.quit, activeforeground='white',
 38                            activebackground='red')
 39         self.cleer.pack(side=LEFT)
 40         self.ls.pack(side=LEFT)
 41         self.quit.pack(side=LEFT)
 42         self.bfm.pack()
 43 
 44         if initdir:
 45             self.cwd.set(os.curdir)
 46             self.doLS()
 47 
 48     def setDirAndGo(self, ev=None):
 49         self.last = self.cwd.get()
 50         self.dirs.config(selectbackground='red')
 51         chek = self.dirs.get(self.dirs.curselection())
 52         if not chek:
 53             chek = os.curdir
 54         self.cwd.set(chek)
 55         self.doLS()
 56 
 57     def doLS(self, ev=None):
 58         error = ''
 59         tdir = self.cwd.get()
 60         if not tdir:
 61             tdir = os.curdir
 62         if not os.path.exists(tdir):
 63             error = tdir + ':未找到文件,请检查路径!'
 64         elif not os.path.isdir(tdir):
 65             error = tdir + ':不是一个路径!'
 66 
 67 
 68         if error:
 69             # self.cwd.set(error)
 70             showinfo(title='提示',message=error)
 71             self.top.update()
 72             # sleep(2)
 73             if not (hasattr(self, 'last') and self.last):
 74                 self.last = os.curdir
 75                 self.cwd.set(self.last)
 76                 self.dirs.config(selectbackground='LightSkyBlue')
 77                 self.top.update()
 78                 return
 79 
 80 
 81         if not os.path.isdir(tdir):
 82             self.cwd.set('')
 83         else:
 84             self.cwd.set('获取目录内容中...')
 85         self.top.update()
 86         dirlist = os.listdir(tdir)
 87         dirlist.sort()
 88         os.chdir(tdir)
 89 
 90         self.dirl.config(text=os.getcwd())
 91         self.dirs.delete(0, END)
 92         self.dirs.insert(END, os.curdir)
 93         self.dirs.insert(END, os.pardir)
 94 
 95         for eachfile in dirlist:
 96             self.dirs.insert(END, eachfile)
 97 
 98         self.cwd.set(os.curdir)
 99         self.dirs.config(selectbackground='LightSkyBlue')
100 
101     def clrDir(self, ev=None):
102         self.cwd.set('')
103 
104 
105 if __name__ == '__main__':
106     dir = DirList(os.curdir)
107     mainloop()

 效果如下:

 

  至此,转载请注明出处。

posted @ 2017-12-10 19:39  失恋的蔷薇  阅读(2268)  评论(0编辑  收藏  举报