2 from wxPython
.wx
import *
6 #---------------------------------------------------------------------------
8 class TestTreeCtrlPanel(wxPanel
):
9 def __init__(self
, parent
, log
):
10 wxPanel
.__init
__(self
, parent
, -1)
15 self
.tree
= wxTreeCtrl(self
, tID
, wxDefaultPosition
, wxDefaultSize
,
16 wxTR_HAS_BUTTONS | wxTR_EDIT_LABELS
)# | wxTR_MULTIPLE)
18 self
.il
= wxImageList(16, 16)
19 idx1
= self
.il
.Add(wxBitmap('bitmaps/smiles.bmp', wxBITMAP_TYPE_BMP
))
20 idx2
= self
.il
.Add(wxBitmap('bitmaps/open.bmp', wxBITMAP_TYPE_BMP
))
21 idx3
= self
.il
.Add(wxBitmap('bitmaps/new.bmp', wxBITMAP_TYPE_BMP
))
22 idx4
= self
.il
.Add(wxBitmap('bitmaps/copy.bmp', wxBITMAP_TYPE_BMP
))
23 idx5
= self
.il
.Add(wxBitmap('bitmaps/paste.bmp', wxBITMAP_TYPE_BMP
))
25 self
.tree
.SetImageList(self
.il
)
27 self
.root
= self
.tree
.AddRoot("The Root Item")
28 self
.tree
.SetItemImage(self
.root
, idx1
)
31 child
= self
.tree
.AppendItem(self
.root
, "Item %d" % x
)
32 self
.tree
.SetItemImage(child
, idx2
)
33 self
.tree
.SetItemSelectedImage(child
, idx3
)
35 last
= self
.tree
.AppendItem(child
, "item %d-%s" % (x
, chr(ord("a")+y
)))
36 self
.tree
.SetItemImage(last
, idx4
)
37 self
.tree
.SetItemSelectedImage(last
, idx5
)
39 self
.tree
.AppendItem(last
, "item %d-%s-%d" % (x
, chr(ord("a")+y
), z
))
41 self
.tree
.Expand(self
.root
)
42 EVT_TREE_ITEM_EXPANDED (self
, tID
, self
.OnItemExpanded
)
43 EVT_TREE_ITEM_COLLAPSED (self
, tID
, self
.OnItemCollapsed
)
44 EVT_TREE_SEL_CHANGED (self
, tID
, self
.OnSelChanged
)
45 EVT_TREE_BEGIN_LABEL_EDIT(self
, tID
, self
.OnBeginEdit
)
46 EVT_TREE_END_LABEL_EDIT (self
, tID
, self
.OnEndEdit
)
48 EVT_LEFT_DCLICK(self
.tree
, self
.OnLeftDClick
)
49 EVT_RIGHT_DOWN(self
.tree
, self
.OnRightClick
)
50 EVT_RIGHT_UP(self
.tree
, self
.OnRightUp
)
52 def OnRightClick(self
, event
):
53 pt
= event
.GetPosition();
54 item
= self
.tree
.HitTest(pt
)
55 self
.log
.WriteText("OnRightClick: %s\n" % self
.tree
.GetItemText(item
))
56 self
.tree
.SelectItem(item
)
58 def OnRightUp(self
, event
):
59 pt
= event
.GetPosition();
60 item
= self
.tree
.HitTest(pt
)
61 self
.log
.WriteText("OnRightUp: %s (manually starting label edit)\n"
62 % self
.tree
.GetItemText(item
))
63 self
.tree
.EditLabel(item
)
67 def OnBeginEdit(self
, event
):
68 self
.log
.WriteText("OnBeginEdit\n")
69 # show how to prevent edit...
70 if self
.tree
.GetItemText(event
.GetItem()) == "The Root Item":
72 self
.log
.WriteText("You can't edit this one...\n")
75 def OnEndEdit(self
, event
):
76 self
.log
.WriteText("OnEndEdit\n")
77 # show how to reject edit, we'll not allow any digits
78 for x
in event
.GetLabel():
79 if x
in string
.digits
:
80 self
.log
.WriteText("You can't enter digits...\n")
86 def OnLeftDClick(self
, event
):
87 pt
= event
.GetPosition();
88 item
= self
.tree
.HitTest(pt
)
89 self
.log
.WriteText("OnLeftDClick: %s\n" % self
.tree
.GetItemText(item
))
92 def OnSize(self
, event
):
93 w
,h
= self
.GetClientSizeTuple()
94 self
.tree
.SetDimensions(0, 0, w
, h
)
97 def OnItemExpanded(self
, event
):
98 item
= event
.GetItem()
99 self
.log
.WriteText("OnItemExpanded: %s\n" % self
.tree
.GetItemText(item
))
101 def OnItemCollapsed(self
, event
):
102 item
= event
.GetItem()
103 self
.log
.WriteText("OnItemCollapsed: %s\n" % self
.tree
.GetItemText(item
))
105 def OnSelChanged(self
, event
):
106 self
.item
= event
.GetItem()
107 self
.log
.WriteText("OnSelChanged: %s\n" % self
.tree
.GetItemText(self
.item
))
109 #---------------------------------------------------------------------------
111 def runTest(frame
, nb
, log
):
112 win
= TestTreeCtrlPanel(nb
, log
)
115 #---------------------------------------------------------------------------
135 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.
138 -------------------------
142 wxTreeCtrl(wxWindow* parent, wxWindowID id, const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxDefaultSize, long style = wxTR_HAS_BUTTONS, const wxValidator& validator = wxDefaultValidator, const wxString& name = "listCtrl")
144 Constructor, creating and showing a tree control.
149 parent = Parent window. Must not be NULL.
151 id = Window identifier. A value of -1 indicates a default value.
153 pos = Window position.
155 size = Window size. If the default size (-1, -1) is specified then the window is sized appropriately.
157 style = Window style. See wxTreeCtrl.
159 validator = Window validator.