Skip to content
Related Articles

Related Articles

wxPython – Collapse() method wx.TreeCtrl

View Discussion
Improve Article
Save Article
  • Last Updated : 08 Jul, 2020
View Discussion
Improve Article
Save Article

In this article we are going to learn about Collapse() method associated with wx.TreeCtrl class of wxPython. Collapse() method is used to simply collapse the given item, that is, the root item gets folded and child items get invisible.
Collapse() method takes a wx.TreeItemId parameter.

Syntax: wx.TreeCtrl.Collapse(item)

Parameters

Parameter Input Type Description
item wx.TreeItemId item or root which we want to collapse

Code Example:




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, wx.DefaultSize,
                                                             wx.TR_HAS_BUTTONS)
  
        self.btn = wx.Button(self, 1, "Collapse", (50, 60))
  
        # add root to self.tree
        self.root = self.tree.AddRoot('Root')
        # add item to self.root
        item = self.tree.AppendItem(self.root, 'Item')
  
        # bind event with self.btn
        self.btn.Bind(wx.EVT_BUTTON, self.onclick)
  
        self.tree.Expand(self.root)
  
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.tree, 0, wx.EXPAND)
        self.SetSizer(sizer)
  
    def onclick(self, e):
        # collapse root
        self.tree.Collapse(self.root)
  
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()


Output Window:

before clicking button


after clicking button


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!