wxPython – GetChildrenCount() method in wx.TreeCtrl
In this article, we are going to learn about GetChildrenCount method associated with wx.TreeCtrl class of wxPython. Returns the number of items in the branch. GetChildrenCount() method takes two parameters first is item and another is recursively.
recursively is a boolean parameter, and If recursively is True, returns the total number of descendants, otherwise, only one level of children is counted. GetChildrenCount() returns int (total number of children associated with TreeCtrl item).
Syntax: wx.TreeCtrl.GetChildrenCount()
Parameters:
Parameters | Type | Description |
item | wx.TreeItemId | item that we want to ensure to be visible. |
recursively | boolean | fetch children recursively or not. |
Example 1:
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" ) item3 = self .tree.AppendItem(item, "SubItem" ) item4 = self .tree.AppendItem(item, "SubItem" ) item5 = self .tree.AppendItem(item2, "SubItem" ) item6 = self .tree.AppendItem(item, "SubItem" ) # expand root node print ( self .tree.GetChildrenCount( self .root, False )) # expand all nodes of the tree self .tree.Expand() 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:
2
Example 2:
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" ) item3 = self .tree.AppendItem(item, "SubItem" ) item4 = self .tree.AppendItem(item, "SubItem" ) item5 = self .tree.AppendItem(item2, "SubItem" ) item6 = self .tree.AppendItem(item, "SubItem" ) # expand root node print ( self .tree.GetChildrenCount( self .root, True )) # 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:
6