I have the following sample code:

]import wx

class TestFrame(wx.Frame):

    def __init__(self):

        wx.Frame.__init__(self, None, title="tree example", size=(600,30))

        

        self.tree = wx.TreeCtrl(self)

       

        root = self.tree.AddRoot("Example")

        

        items = [

               "test1",

               "test2",

               "test3",]

      

        self.AddTreeNodes(root, items)

      

        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)

     

        self.tree.Expand(root)

        

      

    def AddTreeNodes(self, parentItem, items):

        

       

        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[0])

                

    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(None)

frame = TestFrame()

frame.Show()

app.MainLoop()

I want to display some checkboxes when i select test1, test2 or test3 from tree control. The problem is that i don't know how to display a checkbox for example only when I select test1. When I don't select test1 i want the checkboxes to be hidden. What I trying to do is displaying certain options only when i click the proper node. Can someone help me ?
Thank you.

Are you talking about something like this ...

# example of wx.TreeCtrl()

import wx

class TestFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, title="tree example", size=(300,130))
        self.cb1 = wx.CheckBox(self, -1, "test1", pos=(280,10))
        self.cb1.Hide()
        self.tree = wx.TreeCtrl(self, size=(280,100))
        root = self.tree.AddRoot("Example")
        
        items = ["test1",
               "test2",
               "test3",]
      
        self.AddTreeNodes(root, items)
      
        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)
     
        self.tree.Expand(root)
      
    def AddTreeNodes(self, parentItem, items):
        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[0])
                
    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())
        if self.GetItemText(evt.GetItem()) == "test1":
            self.cb1.SetValue(True)
            self.cb1.Show()
        else:
            self.cb1.Hide()
            
    def OnActivated(self, evt):
        print "OnActivated:    ", self.GetItemText(evt.GetItem())


app = wx.PySimpleApp(None)
frame = TestFrame()
frame.Show()
app.MainLoop()
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.