1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
|
import Tix as tix
import xml.etree.ElementTree as ET
class XML_TREE(tix.Frame):
def __init__(self, container, xml_doc, *args, **kwds):
if not 'width' in kwds.keys():kwds['width'] = 320
if not 'height' in kwds.keys():kwds['height'] = 480
tix.Frame.__init__(self, container)
self.tree = tix.Tree(self, *args, **kwds)
self.obj_frm = None
self.objects = {}
self.tree.hlist.config(bg='white', fg='black', selectbackground='black', selectforeground='white')
self.idx = 0
self.buildTree(xml_doc.getroot())
self.tree.autosetmode()
self.tree['browsecmd'] = lambda dir=None: self.browsecmd(dir)
self.switch = 0
self.tree.grid(column=0, row=0)
self.properties = tix.ScrolledText(self, width=kwds['width']*2, height=kwds['height'])
self.properties.grid(column=1, row=0)
toto = dir(self.properties)
for item in toto:
print item
def isInteger(self, text):
for car in text:
if not car in ('1234567890'):
return False
return True
def getIdx(self):
retstr = str(self.idx)
self.idx += 1
return retstr
def buildTree(self, element, dir=None, deep=0):
if dir == None:
dir = self.getIdx()
# builds element
self.tree.hlist.add(dir, itemtype=tix.IMAGETEXT, text=element.tag, image=self.tk.call('tix', 'getimage', 'folder'))
# builds element's text
if element.text != None:
text = element.text.strip()
if text != '':
self.tree.hlist.add(dir + '.' + 'TEXT', itemtype=tix.IMAGETEXT, text="TEXT", image=self.tk.call('tix', 'getimage', 'file'))
# builds element's attributes
for key in element.attrib.keys():
tempdir = dir + '.' + key
self.tree.hlist.add(tempdir, itemtype=tix.IMAGETEXT, text=key, image=self.tk.call('tix', 'getimage', 'file'))
# builds element's children
for subelement in element:
tempdir = dir + '.' + self.getIdx()
self.buildTree(subelement, tempdir, deep+1)
self.objects[dir] = element
def browsecmd(self, dir):
self.switch ^= 1
if self.switch:
return
words = dir.split('.')
parent = '.'.join(words[:-1])
child = words[-1]
if not self.isInteger(child):
obj = self.objects[parent]
else:
obj = self.objects[dir]
text = ET.dump(obj)
self.properties.insert(text)
print text
#~ self.properties.delete('1.0', 'end')
if __name__ == "__main__":
import tkFileDialog as tkfd
def getProperties():
pass
win = tix.Tk()
#~ fname = tkfd.askopenfilename(initialdir='.\\', initialfile='noname.xml', filetypes=[("All", ".*"), ("XML files", ".xml")], title='Select xml file', parent=win)
fname = './test.xml'
if fname != None and fname != '':
xml_tree = ET.parse(fname)
tree = XML_TREE(win, xml_tree)
tree.grid()
win.deiconify()
win.resizable(height=False, width=False)
win.mainloop() |