python自学-class19(up)-递归实现文件遍历

import os
import tkinter
import tkinter.ttk
class TreeWindows:
    def __init__(self):
        self.win=tkinter.Tk()
        self.tree=tkinter.ttk.Treeview(self.win)  #树状
        self.ysb=tkinter.ttk.Scrollbar(self.win,orient="vertical",command=self.tree.yview()) #y滚动条
        self.xsb=tkinter.ttk.Scrollbar(self.win,orient="horizontal",command=self.tree.xview())
        self.tree.configure(yscroll=self.ysb.set,xscroll=self.xsb.set)#关联
        self.tree.grid(row=0,column=0)
        self.tree.heading("#0",text="Path",anchor="w")  #初始化头部
        self.tree.bind("<<TreeviewSelect>>",self.gosel)  #选中绑定
        filepath=r"D:\Python代码\class19"      #路径
        root=self.tree.insert("","end",text=filepath,open="True")   #插入一个节点
        self.laodtree(root,filepath)  #递归

        self.e=tkinter.StringVar()
        self.entry= tkinter.Entry(self.win,textvariable=self.e)
        self.entry.grid(row=0,column=2)


        self.ysb.grid(row=0,column=1,sticky="ns")
        self.xsb.grid(row=1,column=0,sticky="ew")
        self.win.grid()  #表格展示

    def laodtree(self,parent,rootpath):
        for path in os.listdir(rootpath):  #遍历当前目录
            abspath=os.path.join(rootpath,path)  #路径连接
            oid = self.tree.insert(parent,'end',text=path,open=False)#插入树枝
            if os.path.isdir(abspath):
                print("文件夹",abspath)
                self.laodtree(oid,abspath)   #递归调用

    def gosel(self,event):
        self.select=event.widget.selection()   #选择
        for idx in self.select:
            self.e.set(self.tree.item(idx)["text"])
    def show(self):
        self.win.mainloop()

mytree = TreeWindows()
mytree.show()

运行效果:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值