# tree widget demo # # 27-AUG-01 written # 11-FEB-02 sort files import os import Tree from Tkinter import * def getfiles(node): path=apply(os.path.join, node.full_id()) files=os.listdir(path) files.sort() for filename in files: full=os.path.join(path, filename) name=filename folder=0 if os.path.isdir(full): # it's a directory folder=1 elif not os.path.isfile(full): # but it's not a file name=name+' (special)' if os.path.islink(full): # it's a link name=name+' (link to '+os.readlink(full)+')' t.add_node(name=name, id=filename, flag=folder) root=Tk() root.title(os.path.basename(sys.argv[0])) # create the control t=Tree.Tree(master=root, root_id=os.sep, root_label=os.sep, get_contents_callback=getfiles, width=300) t.grid(row=0, column=0, sticky='nsew') root.grid_rowconfigure(0, weight=1) root.grid_columnconfigure(0, weight=1) sb=Scrollbar(root) sb.grid(row=0, column=1, sticky='ns') t.configure(yscrollcommand=sb.set) sb.configure(command=t.yview) sb=Scrollbar(root, orient=HORIZONTAL) sb.grid(row=1, column=0, sticky='ew') t.configure(xscrollcommand=sb.set) sb.configure(command=t.xview) t.focus_set() t.root.expand() root.mainloop()