Open In App

wxPython - GetFirstChild() method in wx.TreeCtrl

Last Updated : 24 Jan, 2021
Summarize
Comments
Improve
Suggest changes
Share
Like Article
Like
Report

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 TypeDescription
item wx.TreeItemIditem 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>)


Next Article
Practice Tags :

Similar Reads