📌  相关文章
📜  wxPython - wx.TreeCtrl 中的 GetChildrenCount() 方法

📅  最后修改于: 2022-05-13 01:54:39.960000             🧑  作者: Mango

wxPython - wx.TreeCtrl 中的 GetChildrenCount() 方法

在本文中,我们将学习与 wxPython 的 wx.TreeCtrl 类关联的 GetChildren 方法。返回分支中的项目数。 GetChildren() 方法有两个参数,第一个是 item,另一个是递归的。

recursively 是一个布尔参数,如果 recursively 为 True,则返回后代的总数,否则,只计算一级孩子。 GetChildrenCount() 返回 int(与 TreeCtrl 项关联的子项总数)。

参数:

ParametersTypeDescription
itemwx.TreeItemIditem that we want to ensure to be visible.
recursivelybooleanfetch children recursively or not.

示例 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()


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


输出:

示例 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()

输出: