Thursday, October 23, 2025
HomeLanguageswxPython – GetFirstChild() method in wx.TreeCtrl

wxPython – GetFirstChild() method in wx.TreeCtrl

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.

Syntax: 

wx.TreeCtrl.GetFirstChild(self, item)

Parameters:

Parameters  Type Description
item  wx.TreeItemId item that we want to ensure to be visible.

Example:

Python




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,
                           wx.TR_HAS_BUTTONS)
        # CREATE TREE ROOT
        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
        print(L)
  
        # expand all nodes of the tree
        self.tree.ExpandAllChildren(item)
  
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.tree, 100, wx.EXPAND)
        self.SetSizer(sizer)
  
  
class MainFrame(wx.Frame):
  
    def __init__(self):
        wx.Frame.__init__(self, parent=None, title='TreeCtrl Demo')
        panel = TreePanel(self)
        self.Show()
  
  
if __name__ == '__main__':
    app = wx.App(redirect=False)
    frame = MainFrame()
    app.MainLoop()


Output:

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

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS