Skip to content
Related Articles

Related Articles

Improve Article

wxPython – Collapse() method wx.TreeCtrl

  • Last Updated : 08 Jul, 2020

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

ParameterInput TypeDescription
itemwx.TreeItemIditem 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

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course




My Personal Notes arrow_drop_up
Recommended Articles
Page :