首页
学习
活动
专区
圈层
工具
发布

Python进阶-GUI-目录树

从当前目录开始,提供一个文件列表。双击列表中任意其它目录,就会使得工具切换到新目录中,用新目录中的文件列表代替旧文件列表。

代码语言:javascript
代码运行次数:0
复制
import os
import tkinter
from time import sleep
代码语言:javascript
代码运行次数:0
复制
# DirList 构造一个代应用的对象
class DirList(object):
  def __init__(self, initdir=None):
    self.top = tkinter.Tk()
    # 创建第一个 Label 控件,文本是应用的主标题
    self.label = tkinter.Label(self.top, text="文件目录应用")
    self.label.pack()
    
    # 声明 Tk 的一个变量 cwd,用于保存当前所在的目录名
    self.cwd = tkinter.StringVar(self.top)
    # 用于显示当前的目录名
    self.dirl = tkinter.Label(self.top, fg="blue", font=("Helvetica", 12, 'bold'))
    self.dirl.pack()
    
    # 要列出的目录文件列表
    self.dirfm = tkinter.Frame(self.top)
    # 让用户在文件数超过 Listbox 的大小时能够移动列表
    self.dirsb = tkinter.Scrollbar(self.dirfm)
    self.dirsb.pack(side=tkinter.RIGHT, fill=tkinter.Y)
    self.dirs = tkinter.Listbox(self.dirfm, height=15, width=50, yscrollcommand=self.dirsb.set)
    # Listbox 的列表项可以与回调函数 setDirAndGo 连接起来
    self.dirs.bind("<Double-1>", self.setDirAndGo)
    # 通过调用 Scrollbar.config() 方法与 Listbox 连接起来
    self.dirs.config(command=self.dirs.yview)
    self.dirs.pack(side=tkinter.LEFT, fill=tkinter.BOTH)
    self.dirfm.pack()
    
    # 创建一个文本框,用户可以在其中输入想要遍历的目录名
    self.dirn = tkinter.Entry(self.top, width=50, textvariable=self.cwd)
    self.dirn.bind("<Return>", self.doLS)
    self.dirn.pack()
    
    # 用来放置3个按钮
    self.bfm = tkinter.Frame(self.top)
    self.clr = tkinter.Button(self.bfm, text="清除", command=self.clrDir, activeforeground="white", activebackground="blue")
    self.ls = tkinter.Button(self.bfm, text="文件列表", command=self.doLS, activeforeground="white", activebackground="green")
    self.quit = tkinter.Button(self.bfm, text="退出", command=self.top.quit, activeforeground="white", activebackground="red")
    
    self.clr.pack(side=tkinter.LEFT)
    self.ls.pack(side=tkinter.LEFT)
    self.quit.pack(side=tkinter.LEFT)
    self.bfm.pack()
    
    if initdir:
      self.cwd.set(os.curdir)
      self.doLS()

清空 Tk 字符串变量 cwd

代码语言:javascript
代码运行次数:0
复制
  def clrDir(self, ev=None):
    self.cwd.set = ""

设置要遍历的目录

代码语言:javascript
代码运行次数:0
复制
  def setDirAndGo(self, ev=None):
    self.last = self.cwd.get()
    self.dirs.config(selectbackground="red")
    check = self.dirs.get(self.dirs.curselection())
    if not check:
      check = os.curdir
    self.cwd.set(check)
    self.doLS()

实现遍历目录

代码语言:javascript
代码运行次数:0
复制
  def doLS(self, ev=None):
    error = ""
    tdir = self.cwd.get()
    if not os.path.exists(tdir):
      error = tdir + '没有太多文件'
    elif not os.path.isdir(tdir):
      error = tdir + '不是一个目录'
    
    if error:
      self.cwd.set(error)
      self.top.update()
      sleep(2)
      if not (hasattr(self, 'last') and self.last):
        self.last = os.curdir
      self.cwd.set(self.last)
      self.dirs.config(selectbackground="LightSkyBlue")
      self.top.update()
      return
      
    self.cwd.set("获取目录中...")
    self.top.update()
    # 获取实际文件列表
    dirlist = os.listdir(tdir)
    dirlist.sort()
    os.chdir(tdir)
    
    self.dirl.config(text=os.getcwd())
    self.dirs.delete(0, tkinter.END)
    self.dirs.insert(tkinter.END, os.curdir)
    self.dirs.insert(tkinter.END, os.pardir)
    for eachFile in dirlist:
      self.dirs.insert(tkinter.END, eachFile)
    self.cwd.set(os.curdir)
    self.dirs.config(selectbackground="LightSkyBlue")

主函数入口

代码语言:javascript
代码运行次数:0
复制
def main():
  d = DirList(os.curdir)
  tkinter.mainloop()

if __name__ == "__main__":
  main()
下一篇
举报
领券