下麵的文章討論與wxPython的wx.TreeCtrl類關聯的GetCount()方法。 GetCount()方法用於獲得控件中存在的項目總數。
用法:wx.TreeCtrl.GetCount(self)
參數:
此方法不需要任何參數。
返回值:
它返回一個整數,即控件中的項目數。
下麵描述了如何實現它以產生某些函數:
範例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.GetCount())
# 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()
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")
# print total items in treectrl
print(self.tree.GetCount())
# 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()
輸出:
7
範例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")
# print number of items in treectrl
print(self.tree.GetCount())
# 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()
輸出:
3
相關用法
- wxPython wx.RadioBox GetCount()用法及代碼示例
- 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()用法及代碼示例
注:本文由純淨天空篩選整理自RahulSabharwal大神的英文原創作品 wxPython – GetCount() method in wx.TreeCtrl。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。