Skip to content
Related Articles

Related Articles

wxPython – GetFirstChild() method in wx.TreeCtrl

Improve Article
Save Article
  • Last Updated : 24 Jan, 2021
Improve Article
Save Article

Prerequisites: wxPython

In this article we are going to learn about GetFirstChild() method in wx.TreeCtrl class of wxPython. GetFirstChild() method returns the first child; call GetNextChild for the next child.

This function requires a ‘cookie’ parameter to be passed to it, which is opaque for the application but is necessary for the library to make these function enumerate on one and the same object simultaneously. The cookie passed to GetFirstChild and GetNextChild should be the same variable.

GetFirstChild() method returns an invalid tree item (i.e. wx.TreeItemId.IsOk returns False) if there are no further children.


wx.TreeCtrl.GetFirstChild(self, item)


Parameters TypeDescription
item wx.TreeItemIditem that we want to ensure to be visible.



import wx
class MyTree(wx.TreeCtrl):
    def __init__(self, parent, id, pos, size, style):
        wx.TreeCtrl.__init__(self, parent, id, pos, size, style)
class TreePanel(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)
        # create tree control in window
        self.tree = MyTree(self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize,
        self.root = self.tree.AddRoot('root')
        self.tree.SetPyData(self.root, ('key', 'value'))
        # add item to root
        item = self.tree.AppendItem(self.root, "Item")
        item2 = self.tree.AppendItem(self.root, "Item")
        L = self.tree.GetFirstChild(self.root)
        # print tuple of PySwigObject returned from GetFirstChild function
        # expand all nodes of the tree
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.tree, 100, wx.EXPAND)
class MainFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, parent=None, title='TreeCtrl Demo')
        panel = TreePanel(self)
if __name__ == '__main__':
    app = wx.App(redirect=False)
    frame = MainFrame()


(<wx._controls.TreeItemId; proxy of <Swig Object of type ‘wxTreeItemId *’ at 0x556c60f40440> >, <Swig Object of type ‘void *’ at 0x1>)

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!