4 py2
= sys
.version
[0] == '2'
6 from wxPython
.wx
import *
9 from xml
.parsers
import expat
12 from xml
.parsers
import pyexpat
13 parsermodule
= pyexpat
18 #----------------------------------------------------------------------
21 def runTest(frame
, nb
, log
):
22 dlg
= wxMessageDialog(frame
, 'This demo requires the XML package. '
23 'See http://www.python.org/sigs/xml-sig/',
24 'Sorry', wxOK | wxICON_INFORMATION
)
30 class XMLTree(wxTreeCtrl
):
31 def __init__(self
, parent
, ID
):
32 wxTreeCtrl
.__init
__(self
, parent
, ID
)
33 self
.nodeStack
= [self
.AddRoot("Root")]
35 # Trees need an image list to do DnD...
36 self
.il
= wxImageList(16,16)
37 self
.SetImageList(self
.il
)
39 # event handlers for DnD
40 EVT_TREE_BEGIN_DRAG(self
, ID
, self
.OnBeginDrag
)
41 EVT_TREE_END_DRAG(self
, ID
, self
.OnEndDrag
)
44 def OnBeginDrag(self
, event
):
45 item
= event
.GetItem()
46 if item
!= self
.GetRootItem():
47 self
.draggingItem
= item
48 event
.Allow() # if DnD of this item is okay Allow it.
51 def OnEndDrag(self
, evt
):
52 itemSrc
= self
.draggingItem
53 itemDst
= evt
.GetItem()
54 self
.draggingItem
= None
56 if not itemDst
.IsOk():
57 print "Can't drag to here..."
60 # For this simple example just take the text of the source item
61 # and append it to the destination item. In real life you would
62 # possibly want to copy subtrees...
63 text
= self
.GetItemText(itemSrc
)
64 self
.AppendItem(itemDst
, text
)
68 # Define a handler for start element events
69 def StartElement(self
, name
, attrs
):
72 id = self
.AppendItem(self
.nodeStack
[-1], name
)
73 self
.nodeStack
.append(id)
75 def EndElement(self
, name
):
76 self
.nodeStack
= self
.nodeStack
[:-1]
78 def CharacterData(self
, data
):
82 self
.AppendItem(self
.nodeStack
[-1], data
)
85 def LoadTree(self
, filename
):
87 Parser
= parsermodule
.ParserCreate()
89 # Tell the parser what the start element handler is
90 Parser
.StartElementHandler
= self
.StartElement
91 Parser
.EndElementHandler
= self
.EndElement
92 Parser
.CharacterDataHandler
= self
.CharacterData
95 ParserStatus
= Parser
.Parse(open(filename
,'r').read(), 1)
98 def runTest(frame
, nb
, log
):
100 win
.LoadTree("paper.xml")
103 #----------------------------------------------------------------------