在本文中,我们将学习与wxPython的wx.TreeCtrl类关联的GetChildren方法。返回分支中的项目数。 GetChildren()方法采用两个参数,第一个是item,另一个是递归。
recursively是一个布尔参数,如果recursively为True,则返回子代总数,否则,仅计算一个子级。 GetChildrenCount()返回int(与TreeCtrl项关联的子项总数)。
用法:wx.TreeCtrl.GetChildrenCount()
参数:
参数 | Type | Description |
item | wx.TreeItemId | 我们想要确保可见的项目。 |
recursively | boolean | 是否递归获取孩子。 |
范例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()
输出:
2
范例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()
输出:
6
相关用法
- wxPython wx.ToolBar AddControl()用法及代码示例
- wxPython wx.ToolBar AddSeparator()用法及代码示例
- wxPython wx.RadioButton GetValue()用法及代码示例
- wxPython wx.RadioButton SetValue()用法及代码示例
- wxPython wx.RadioBOx IsItemShown()用法及代码示例
- wxPython wx.RadioBOx SetItemHelpText()用法及代码示例
- wxPython wx.RadioBox SetItemLabel()用法及代码示例
- wxPython wx.RadioBox GetItemToolTip()用法及代码示例
- wxPython wx.RadioBox GetString()用法及代码示例
- wxPython wx.RadioBox IsItemEnabled()用法及代码示例
注:本文由纯净天空筛选整理自RahulSabharwal大神的英文原创作品 wxPython – GetChildrenCount() method in wx.TreeCtrl。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。