]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/XMLtreeview.py
5 py2
= sys
.version
[0] == '2'
9 from xml
.parsers
import expat
12 from xml
.parsers
import pyexpat
13 parsermodule
= pyexpat
18 #----------------------------------------------------------------------
21 def runTest(frame
, nb
, log
):
22 dlg
= wx
.MessageDialog(
23 frame
, 'This demo requires the XML package. '
24 'See http://www.python.org/sigs/xml-sig/',
25 'Sorry', wx
.OK | wx
.ICON_INFORMATION
33 class XMLTree(wx
.TreeCtrl
):
34 def __init__(self
, parent
, ID
):
35 wx
.TreeCtrl
.__init
__(self
, parent
, ID
)
36 self
.nodeStack
= [self
.AddRoot("Root")]
38 # Trees need an image list to do DnD...
39 self
.il
= wx
.ImageList(16,16)
40 self
.SetImageList(self
.il
)
42 # event handlers for DnD
43 self
.Bind(wx
.EVT_TREE_BEGIN_DRAG
, self
.OnBeginDrag
)
44 self
.Bind(wx
.EVT_TREE_END_DRAG
, self
.OnEndDrag
)
47 def OnBeginDrag(self
, event
):
48 item
= event
.GetItem()
50 if item
!= self
.GetRootItem():
51 self
.draggingItem
= item
52 event
.Allow() # if DnD of this item is okay Allow it.
55 def OnEndDrag(self
, evt
):
56 itemSrc
= self
.draggingItem
57 itemDst
= evt
.GetItem()
58 self
.draggingItem
= None
60 if not itemDst
.IsOk():
61 print "Can't drag to here..."
64 # For this simple example just take the text of the source item
65 # and append it to the destination item. In real life you would
66 # possibly want to copy subtrees...
67 text
= self
.GetItemText(itemSrc
)
68 self
.AppendItem(itemDst
, text
)
72 # Define a handler for start element events
73 def StartElement(self
, name
, attrs
):
77 id = self
.AppendItem(self
.nodeStack
[-1], name
)
78 self
.nodeStack
.append(id)
80 def EndElement(self
, name
):
81 self
.nodeStack
= self
.nodeStack
[:-1]
83 def CharacterData(self
, data
):
88 self
.AppendItem(self
.nodeStack
[-1], data
)
91 def LoadTree(self
, filename
):
93 Parser
= parsermodule
.ParserCreate()
95 # Tell the parser what the start element handler is
96 Parser
.StartElementHandler
= self
.StartElement
97 Parser
.EndElementHandler
= self
.EndElement
98 Parser
.CharacterDataHandler
= self
.CharacterData
101 ParserStatus
= Parser
.Parse(open(filename
,'r').read(), 1)
104 def runTest(frame
, nb
, log
):
105 win
= XMLTree(nb
, -1)
106 win
.LoadTree("paper.xml")
109 #----------------------------------------------------------------------
117 if __name__
== '__main__':
120 run
.main(['', os
.path
.basename(sys
.argv
[0])])