Added the sample code from wxPython In Action to the samples dir
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@42925 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
62
wxPython/samples/wxPIA_book/Chapter-15/tree_simple.py
Normal file
62
wxPython/samples/wxPIA_book/Chapter-15/tree_simple.py
Normal file
@@ -0,0 +1,62 @@
|
||||
import wx
|
||||
import data
|
||||
|
||||
class TestFrame(wx.Frame):
|
||||
def __init__(self):
|
||||
wx.Frame.__init__(self, None, title="simple tree", size=(400,500))
|
||||
|
||||
# Create the tree
|
||||
self.tree = wx.TreeCtrl(self)
|
||||
|
||||
# Add a root node
|
||||
root = self.tree.AddRoot("wx.Object")
|
||||
|
||||
# Add nodes from our data set
|
||||
self.AddTreeNodes(root, data.tree)
|
||||
|
||||
# Bind some interesting events
|
||||
self.Bind(wx.EVT_TREE_ITEM_EXPANDED, self.OnItemExpanded, self.tree)
|
||||
self.Bind(wx.EVT_TREE_ITEM_COLLAPSED, self.OnItemCollapsed, self.tree)
|
||||
self.Bind(wx.EVT_TREE_SEL_CHANGED, self.OnSelChanged, self.tree)
|
||||
self.Bind(wx.EVT_TREE_ITEM_ACTIVATED, self.OnActivated, self.tree)
|
||||
|
||||
# Expand the first level
|
||||
self.tree.Expand(root)
|
||||
|
||||
|
||||
def AddTreeNodes(self, parentItem, items):
|
||||
"""
|
||||
Recursively traverses the data structure, adding tree nodes to
|
||||
match it.
|
||||
"""
|
||||
for item in items:
|
||||
if type(item) == str:
|
||||
self.tree.AppendItem(parentItem, item)
|
||||
else:
|
||||
newItem = self.tree.AppendItem(parentItem, item[0])
|
||||
self.AddTreeNodes(newItem, item[1])
|
||||
|
||||
def GetItemText(self, item):
|
||||
if item:
|
||||
return self.tree.GetItemText(item)
|
||||
else:
|
||||
return ""
|
||||
|
||||
def OnItemExpanded(self, evt):
|
||||
print "OnItemExpanded: ", self.GetItemText(evt.GetItem())
|
||||
|
||||
def OnItemCollapsed(self, evt):
|
||||
print "OnItemCollapsed:", self.GetItemText(evt.GetItem())
|
||||
|
||||
def OnSelChanged(self, evt):
|
||||
print "OnSelChanged: ", self.GetItemText(evt.GetItem())
|
||||
|
||||
def OnActivated(self, evt):
|
||||
print "OnActivated: ", self.GetItemText(evt.GetItem())
|
||||
|
||||
|
||||
app = wx.PySimpleApp(redirect=True)
|
||||
frame = TestFrame()
|
||||
frame.Show()
|
||||
app.MainLoop()
|
||||
|
Reference in New Issue
Block a user