#!/usr/bin/env python # http://stackoverflow.com/questions/16746387/tkinter-treeview-widget import os import Tkinter as tk import ttk import tkFileDialog, gzip def check_gzip(dataPath): f = open(dataPath,"rb") magic = f.read(2) f.close() if magic == '\037\213': return 1 else: return 0 class App(tk.Frame): def __init__(self, master, path): master.title("Egg 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_poserfile(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_poserfile(self, parent, path): if os.path.exists(path): if check_gzip(path): f = gzip.GzipFile(path) else: f = open(path,"r") lines = f.readlines() f.close() if len(lines) == 1 and lines[0].count("\r"): lines = lines[0].split("\r") brackets = [parent] templine = "" for num, line in enumerate(lines): line = line.replace("\n","").replace("\r","") if line == "": continue if templine != "": line = templine + line templine = "" continue if (num < len(lines)-1): if (lines[num+1].replace(" ","").replace("\t","").replace("\n","").replace("\r","") == "{"): templine = line line += lines[num+1] oid = self.tree.insert(brackets[len(brackets)-1], 'end', text=line.replace("\t",""), open=False) if line.find("{") != -1: for rep in range(line.count("{")): brackets.append(oid) if line.find("}") != -1: for rep in range(line.count("}")): closed = brackets.pop() if __name__ == "__main__": root = tk.Tk() filetypes = ["*.cr2 *.CR2 *.crz *.CRZ", "*.pp2 *.PP2 *.ppz *.PPZ", "*.pz2 *.PZ2 *.p2z *.P2Z", "*.pz3 *.PZ3 *.pzz *.PZZ", "*.lt2 *.LT2 *.ltz *.LTZ", "*.fc2 *.fcz *.FC2 *.FCZ", "*.obj *.obz *.OBJ *.OBZ", "*.hd2 *.hdz *.HD2 *.HDZ", "*.mt5 *.mz5 *.MT5 *.MZ5", "*.cm2 *.cmz *.CM2 *.CMZ", "*.hr2 *.hrz *.HR2 *.HRZ", "*.mc6 *.mcz *.MC6 *.MCZ"] types = " ".join(filetypes) path_to_my_project =tkFileDialog.askopenfilename(filetypes=[("Poser Files", types)],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()