Open In App
Related Articles

wxPython – Collapse() method wx.TreeCtrl

Improve Article
Improve
Save Article
Save
Like Article
Like

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


Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 08 Jul, 2020
Like Article
Save Article
Previous
Next
Similar Reads
Complete Tutorials