1import os 2import tkinter as tk 3from tkinter import ttk 4root=tk.Tk() 5root.geometry('320x240') 6f=tk.Frame(root) 7tv=ttk.Treeview(f,show='tree') 8ybar=tk.Scrollbar(f,orient=tk.VERTICAL, 9 command=tv.yview) 10tv.configure(yscroll=ybar.set) 11directory='d:\\Drivers' 12tv.heading('#0',text='目录:'+directory,anchor='w') 13path=os.path.abspath(directory) 14node=tv.insert('','end',text=path,open=True) 15def traverse_dir(parent,path): 16 for d in os.listdir(path): 17 full_path=os.path.join(path,d) 18 isdir = os.path.isdir(full_path) 19 id=tv.insert(parent,'end',text=d,open=False) 20 if isdir: 21 traverse_dir(id,full_path) 22traverse_dir(node,path) 23ybar.pack(side=tk.RIGHT,fill=tk.Y) 24tv.pack() 25f.pack() 26root.mainloop()

