• 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()

     效果如下:

      至此,转载请注明出处。

  • 相关阅读:
    yun2win-iOS端IM SDK使用方法
    题解
    普通乘法,加法等时间复杂度计算
    noip2014 解方程(本博文转载于http://blog.csdn.net/popoqqq/article/details/40984859,略有删减)
    检查
    关于对拍 (来自老胡)
    2014 NOIP 赛前自我整理提醒。
    USACO 2014 JAN 滑雪录像
    Vue 双向绑定原理
    Vue 路由
  • 原文地址:https://www.cnblogs.com/wcwnina/p/8017834.html
Copyright © 2020-2023  润新知