]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/wxTreeCtrl.py
2 from wxPython
. wx
import *
6 #---------------------------------------------------------------------------
8 class MyTreeCtrl ( wxTreeCtrl
):
9 def __init__ ( self
, parent
, id , pos
, size
, style
, log
):
10 wxTreeCtrl
.__ init
__ ( self
, parent
, id , pos
, size
, style
)
13 def OnCompareItems ( self
, item1
, item2
):
14 t1
= self
. GetItemText ( item1
)
15 t2
= self
. GetItemText ( item2
)
16 self
. log
. WriteText ( 'compare: ' + t1
+ ' <> ' + t2
+ ' \n ' )
21 #---------------------------------------------------------------------------
23 class TestTreeCtrlPanel ( wxPanel
):
24 def __init__ ( self
, parent
, log
):
25 # Use the WANTS_CHARS style so the panel doesn't eat the Return key.
26 wxPanel
.__ init
__ ( self
, parent
, - 1 , style
= wxWANTS_CHARS
)
27 EVT_SIZE ( self
, self
. OnSize
)
32 self
. tree
= MyTreeCtrl ( self
, tID
, wxDefaultPosition
, wxDefaultSize
,
40 il
= wxImageList ( isz
[ 0 ], isz
[ 1 ])
41 fldridx
= il
. Add ( wxArtProvider_GetBitmap ( wxART_FOLDER
, wxART_OTHER
, isz
))
42 fldropenidx
= il
. Add ( wxArtProvider_GetBitmap ( wxART_FILE_OPEN
, wxART_OTHER
, isz
))
43 fileidx
= il
. Add ( wxArtProvider_GetBitmap ( wxART_REPORT_VIEW
, wxART_OTHER
, isz
))
44 smileidx
= il
. Add ( images
. getSmilesBitmap ())
46 self
. tree
. SetImageList ( il
)
49 # NOTE: For some reason tree items have to have a data object in
50 # order to be sorted. Since our compare just uses the labels
51 # we don't need any real data, so we'll just use None below for
54 self
. root
= self
. tree
. AddRoot ( "The Root Item" )
55 self
. tree
. SetPyData ( self
. root
, None )
56 self
. tree
. SetItemImage ( self
. root
, fldridx
, wxTreeItemIcon_Normal
)
57 self
. tree
. SetItemImage ( self
. root
, fldropenidx
, wxTreeItemIcon_Expanded
)
61 child
= self
. tree
. AppendItem ( self
. root
, "Item %d " % x
)
62 self
. tree
. SetPyData ( child
, None )
63 self
. tree
. SetItemImage ( child
, fldridx
, wxTreeItemIcon_Normal
)
64 self
. tree
. SetItemImage ( child
, fldropenidx
, wxTreeItemIcon_Expanded
)
66 last
= self
. tree
. AppendItem ( child
, "item %d-%s " % ( x
, chr ( ord ( "a" )+ y
)))
67 self
. tree
. SetPyData ( last
, None )
68 self
. tree
. SetItemImage ( last
, fldridx
, wxTreeItemIcon_Normal
)
69 self
. tree
. SetItemImage ( last
, fldropenidx
, wxTreeItemIcon_Expanded
)
71 item
= self
. tree
. AppendItem ( last
, "item %d-%s-%d " % ( x
, chr ( ord ( "a" )+ y
), z
))
72 self
. tree
. SetPyData ( item
, None )
73 self
. tree
. SetItemImage ( item
, fileidx
, wxTreeItemIcon_Normal
)
74 self
. tree
. SetItemImage ( item
, smileidx
, wxTreeItemIcon_Selected
)
76 self
. tree
. Expand ( self
. root
)
77 EVT_TREE_ITEM_EXPANDED ( self
, tID
, self
. OnItemExpanded
)
78 EVT_TREE_ITEM_COLLAPSED ( self
, tID
, self
. OnItemCollapsed
)
79 EVT_TREE_SEL_CHANGED ( self
, tID
, self
. OnSelChanged
)
80 EVT_TREE_BEGIN_LABEL_EDIT ( self
, tID
, self
. OnBeginEdit
)
81 EVT_TREE_END_LABEL_EDIT ( self
, tID
, self
. OnEndEdit
)
82 EVT_TREE_ITEM_ACTIVATED ( self
, tID
, self
. OnActivate
)
84 EVT_LEFT_DCLICK ( self
. tree
, self
. OnLeftDClick
)
85 EVT_RIGHT_DOWN ( self
. tree
, self
. OnRightClick
)
86 EVT_RIGHT_UP ( self
. tree
, self
. OnRightUp
)
90 def OnRightClick ( self
, event
):
91 pt
= event
. GetPosition ();
92 item
, flags
= self
. tree
. HitTest ( pt
)
93 self
. log
. WriteText ( "OnRightClick: %s , %s , %s \n " %
94 ( self
. tree
. GetItemText ( item
), type ( item
), item
.__ class
__ ))
95 self
. tree
. SelectItem ( item
)
98 def OnRightUp ( self
, event
):
99 pt
= event
. GetPosition ();
100 item
, flags
= self
. tree
. HitTest ( pt
)
101 self
. log
. WriteText ( "OnRightUp: %s (manually starting label edit) \n "
102 % self
. tree
. GetItemText ( item
))
103 self
. tree
. EditLabel ( item
)
107 def OnBeginEdit ( self
, event
):
108 self
. log
. WriteText ( "OnBeginEdit \n " )
109 # show how to prevent edit...
110 if self
. tree
. GetItemText ( event
. GetItem ()) == "The Root Item" :
112 self
. log
. WriteText ( "You can't edit this one... \n " )
114 # Lets just see what's visible of its children
116 root
= event
. GetItem ()
117 ( child
, cookie
) = self
. tree
. GetFirstChild ( root
, cookie
)
119 self
. log
. WriteText ( "Child [ %s ] visible = %d " %
120 ( self
. tree
. GetItemText ( child
),
121 self
. tree
. IsVisible ( child
)))
122 ( child
, cookie
) = self
. tree
. GetNextChild ( root
, cookie
)
127 def OnEndEdit ( self
, event
):
128 self
. log
. WriteText ( "OnEndEdit \n " )
129 # show how to reject edit, we'll not allow any digits
130 for x
in event
. GetLabel ():
131 if x
in string
. digits
:
132 self
. log
. WriteText ( "You can't enter digits... \n " )
137 def OnLeftDClick ( self
, event
):
138 pt
= event
. GetPosition ();
139 item
, flags
= self
. tree
. HitTest ( pt
)
140 self
. log
. WriteText ( "OnLeftDClick: %s \n " % self
. tree
. GetItemText ( item
))
141 parent
= self
. tree
. GetItemParent ( item
)
142 self
. tree
. SortChildren ( parent
)
146 def OnSize ( self
, event
):
147 w
, h
= self
. GetClientSizeTuple ()
148 self
. tree
. SetDimensions ( 0 , 0 , w
, h
)
151 def OnItemExpanded ( self
, event
):
152 item
= event
. GetItem ()
153 self
. log
. WriteText ( "OnItemExpanded: %s \n " % self
. tree
. GetItemText ( item
))
155 def OnItemCollapsed ( self
, event
):
156 item
= event
. GetItem ()
157 self
. log
. WriteText ( "OnItemCollapsed: %s \n " % self
. tree
. GetItemText ( item
))
159 def OnSelChanged ( self
, event
):
160 self
. item
= event
. GetItem ()
161 self
. log
. WriteText ( "OnSelChanged: %s \n " % self
. tree
. GetItemText ( self
. item
))
162 if wxPlatform
== '__WXMSW__' :
163 self
. log
. WriteText ( "BoundingRect: %s \n " %
164 self
. tree
. GetBoundingRect ( self
. item
, True ))
165 #items = self.tree.GetSelections()
166 #print map(self.tree.GetItemText, items)
170 def OnActivate ( self
, evt
):
171 self
. log
. WriteText ( "OnActivate: %s \n " % self
. tree
. GetItemText ( self
. item
))
174 #---------------------------------------------------------------------------
176 def runTest ( frame
, nb
, log
):
177 win
= TestTreeCtrlPanel ( nb
, log
)
180 #---------------------------------------------------------------------------
187 A tree control presents information as a hierarchy, with items that may be expanded to show further items. Items in a tree control are referenced by wxTreeItemId handles.
193 if __name__
== '__main__' :
196 run
. main ([ '' , os
. path
. basename ( sys
. argv
[ 0 ])])