]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/tests/txml.py
2 Build a GUI Tree (wxWindows) from an XML file
7 from xml
.parsers
import pyexpat
9 from wxPython
.wx
import *
11 class MyFrame(wxFrame
):
12 def __init__(self
, parent
, id, title
):
13 wxFrame
.__init
__(self
, parent
, id, title
, wxPoint(100, 100), wxSize(160,100))
15 menu
.Append (1001,"Open")
16 menu
.Append (1002,"Close")
17 menu
.Append (1003,"Exit")
19 menubar
.Append (menu
,"File")
20 self
.SetMenuBar(menubar
)
24 self
.frame
= MyFrame(NULL
, -1, "Tree View of XML")
25 self
.tree
= wx
.wxTreeCtrl(self
.frame
, -1)
26 EVT_MENU(self
, 1001, self
.OnOpen
)
27 EVT_MENU(self
, 1002, self
.OnClose
)
28 EVT_MENU(self
, 1003, self
.OnExit
)
30 self
.SetTopWindow(self
.frame
)
33 def OnOpen(self
,event
):
34 f
= wxFileDialog(self
.frame
,"Select a file",".","","*.xml",wxOPEN
)
35 if f
.ShowModal() == wxID_OK
:
38 def OnClose(self
,event
):
39 self
.tree
= wx
.wxTreeCtrl(self
.frame
, -1)
42 def OnExit(self
,event
):
43 self
.OnCloseWindow(event
)
45 def OnCloseWindow(self
, event
):
51 # Define a handler for start element events
52 def StartElement( name
, attrs
):
54 NodeStack
.append(app
.tree
.AppendItem(NodeStack
[-1],name
))
56 def EndElement( name
):
58 NodeStack
= NodeStack
[:-1]
60 def CharacterData ( data
):
62 if string
.strip(data
):
63 app
.tree
.AppendItem(NodeStack
[-1],data
)
69 Parser
= pyexpat
.ParserCreate()
71 # Tell the parser what the start element handler is
72 Parser
.StartElementHandler
= StartElement
73 Parser
.EndElementHandler
= EndElement
74 Parser
.CharacterDataHandler
= CharacterData
77 ParserStatus
= Parser
.Parse(open(f
,'r').read(), 1)
83 NodeStack
= [app
.tree
.AddRoot("Root")]