wxPython - CollapseAll() method in wx.TreeCtrl

wxPython - CollapseAll() method in wx.TreeCtrl

wx.TreeCtrl is a widget in the wxPython library that provides a tree control. This control is often used for displaying hierarchical data, where items have parent-child relationships, like directory structures.

The CollapseAll() method is used to collapse all items in the tree, hiding their child items.

Here's a simple example of how you can use the wx.TreeCtrl and the CollapseAll() method:

import wx

class MyFrame(wx.Frame):
    def __init__(self, parent):
        super(MyFrame, self).__init__(parent, title="wx.TreeCtrl Example")

        self.panel = wx.Panel(self)
        self.tree = wx.TreeCtrl(self.panel, pos=(10,10), size=(200,150))
        
        # Sample data
        root = self.tree.AddRoot('Root')
        item1 = self.tree.AppendItem(root, 'Item 1')
        self.tree.AppendItem(item1, 'Subitem 1.1')
        self.tree.AppendItem(item1, 'Subitem 1.2')
        
        item2 = self.tree.AppendItem(root, 'Item 2')
        self.tree.AppendItem(item2, 'Subitem 2.1')

        # Button to trigger CollapseAll
        btn = wx.Button(self.panel, label="Collapse All", pos=(220, 10))
        btn.Bind(wx.EVT_BUTTON, self.on_collapse_all)

        self.SetSize((400, 200))

    def on_collapse_all(self, event):
        self.tree.CollapseAll()

if __name__ == "__main__":
    app = wx.App(False)
    frame = MyFrame(None)
    frame.Show()
    app.MainLoop()

In this example, we created a simple wx application with a tree control and a button. When you click the "Collapse All" button, all items in the tree will be collapsed using the CollapseAll() method.


More Tags

view php-shorttags react-responsive-carousel raspberry-pi linegraph keytool parallax bulk-load beanshell spring-profiles

More Programming Guides

Other Guides

More Programming Examples