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


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

在本文中,我們將學習wxPython中的另一個函數,即與wxPython的wx.TreeCtrl類關聯的CollapseAndReset()方法。 CollapseAndReset()函數僅折疊特定項目並刪除其所有子項。

它使用TreeItemId作為參數。

用法: wx.TreeCtrl.CollapseAndReset(Self, item)

代碼示例:

蟒蛇

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 
        self.tree = MyTree(self, wx.ID_ANY, wx.DefaultPosition, 
                                 (150,150), wx.TR_HAS_BUTTONS) 
  
        # Add root to Tree Control 
        self.root = self.tree.AddRoot('Root') 
  
        # Add item to root 
        self.itm = self.tree.AppendItem(self.root, 'Item') 
  
        # Add item to 'itm' 
        self.itm2 = self.tree.AppendItem(self.itm, "Sub Item") 
  
        # Add child item to itm2 
        self.itm3 = self.tree.AppendItem(self.itm2, "Another Item") 
  
        # Expand whole tree 
        self.tree.ExpandAll() 
  
        sizer = wx.BoxSizer(wx.VERTICAL) 
        sizer.Add(self.tree, 0, wx.EXPAND) 
        self.SetSizer(sizer) 
  
        # Add button in frame 
        self.btn = wx.Button(self, 1, "Collapse", (10,100)) 
        # Bind event function with button 
        self.btn.Bind(wx.EVT_BUTTON, self.onclick) 
  
    def onclick(self, e):
        # collapse all children of itm recursively 
        self.tree.CollapseAndReset(self.itm) 
  
  
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()

輸出:

在點擊按鈕之前

單擊按鈕後




相關用法


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