當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


wxPython wx.TreeCtrl GetFirstChild()用法及代碼示例

先決條件:wxPython

在本文中,我們將學習wxPython的wx.TreeCtrl類中的GetFirstChild()方法。 GetFirstChild()方法返回第一個孩子;調用GetNextChild作為下一個孩子。

此函數需要將‘cookie’參數傳遞給它,該參數對於應用程序是不透明的,但對於使這些函數同時在一個和同一對象上枚舉的庫而言是必需的。傳遞給GetFirstChild和GetNextChild的cookie應該是相同的變量。

如果沒有其他子項,則GetFirstChild()方法返回無效的樹項(即wx.TreeItemId.IsOk返回False)。

Syntax: 



wx.TreeCtrl.GetFirstChild(self, item)

Parameters:

Parameters  Type Description
item  wx.TreeItemId item that we want to ensure to be visible.

例:

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

輸出:

(<wx._controls.TreeItemId; proxy of <Swig Object of type ‘wxTreeItemId *’ at 0x556c60f40440> >, <Swig Object of type ‘void *’ at 0x1>)





相關用法


注:本文由純淨天空篩選整理自RahulSabharwal大神的英文原創作品 wxPython – GetFirstChild() method in wx.TreeCtrl。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。