Open In App
Related Articles

wxPython – Collapse() method wx.TreeCtrl

Improve Article
Save Article
Like 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)


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,
        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)
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.tree, 0, wx.EXPAND)
    def onclick(self, e):
        # collapse root
class MainFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, parent = None, title ='TreeCtrl Demo')
        panel = TreePanel(self)
if __name__ == '__main__':
    app = wx.App(redirect = False)
    frame = MainFrame()

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
Similar Reads
Complete Tutorials