前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python进阶-GUI-目录树

Python进阶-GUI-目录树

作者头像
小团子
发布2019-07-18 15:30:52
1K0
发布2019-07-18 15:30:52
举报
文章被收录于专栏:数据云团

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

代码语言:javascript
复制
import os
import tkinter
from time import sleep
代码语言:javascript
复制
# 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
复制
  def clrDir(self, ev=None):
    self.cwd.set = ""

设置要遍历的目录

代码语言:javascript
复制
  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
复制
  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
复制
def main():
  d = DirList(os.curdir)
  tkinter.mainloop()

if __name__ == "__main__":
  main()
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2019-04-27,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 数据云团 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档