#!/usr/bin/env python # http://stackoverflow.com/questions/16746387/tkinter-treeview-widget import os import Tkinter as tk import ttk import tkFileDialog class App(tk.Frame): def __init__(self, master, path): master.title("DAE File Viewer") tk.Frame.__init__(self, master) self.masterFrame = ttk.Frame(self.master, width=1300, height=1000) self.masterFrame.grid(row=0,column=0) self.tree = ttk.Treeview(self.masterFrame, height="40") ysb = ttk.Scrollbar(self.masterFrame, orient='vertical', command=self.tree.yview) xsb = ttk.Scrollbar(self.masterFrame, orient='horizontal', command=self.tree.xview) self.tree.configure(yscroll=ysb.set, xscroll=xsb.set) self.tree.heading('#0', text=path, anchor='w') self.tree.column('#0',minwidth=0, width=1200, stretch=False) root_node = self.tree.insert('', 'end', text=os.path.basename(path), open=True) self.display_eggfile(root_node, path) self.tree.grid(row=0, column=0) ysb.grid(row=0, column=1, sticky='ns') xsb.grid(row=1, column=0, sticky='ew') self.grid() def display_eggfile(self, parent, path): if os.path.exists(path): f = open(path) lines = f.readlines() f.close() brackets = [parent] tabcount = 0 wrapwidth = 150 for num, line in enumerate(lines): line = line.replace("\n","").replace("\t","~") if line == "": continue if len(line) > wrapwidth: #if 1 == 0: newline = "" for i,t in enumerate(line): if i % wrapwidth == 0: make_split = 1 if make_split: if t == " ": newline += "\n" make_split = 0 else: newline += t else: newline += t #newline += "\n" newlines = newline.split("\n") oid = self.tree.insert(brackets[len(brackets)-1], 'end', text=newlines[0].replace("~",""), open=False) for i in range(1,len(newlines)): nline = newlines[i] oid_skip = self.tree.insert(oid, 'end', text=nline.replace("~",""), open=False) else: oid = self.tree.insert(brackets[len(brackets)-1], 'end', text=line.replace("~",""), open=False) tabs = line.count("~") #if line.find("<") != -1: if tabs > tabcount: for rep in range(tabs-tabcount): brackets.append(oid) #if line.find(">") != -1: if tabs < tabcount: for rep in range(tabcount-tabs): closed = brackets.pop() tabcount = tabs if __name__ == "__main__": root = tk.Tk() path_to_my_project =tkFileDialog.askopenfilename(filetypes=[("Collada DAE Files", "*.dae *.DAE")],parent=root) root.destroy() # Silly business to prevent tkFileDialog from interfering with Treeview scrollbar focus root = tk.Tk() app = App(root, path=path_to_my_project) app.mainloop()