Skip to content
Related Articles
Open in App
Not now

Related Articles

wxPython – Delete() method in wx.TreeCtrl

Improve Article
Save Article
  • Last Updated : 13 Sep, 2022
Improve Article
Save Article

In this article we will learn about Delete() method associated with the class wx.TreeCtrl of wxPython. Delete() function is simply used in order to delete the specific item from the tree it can be root or terminal item.

A EVT_TREE_DELETE_ITEM event will be generated.

This function may cause a subsequent call to GetNextChild to fail.


wx.TreeCtrl.Delete(self, item)


Parameter           type                       Description
itemwx.TreeItemIditem that we want to get deleted from the Tree Control 

Code Example:


import wx
class TreePanel(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)
        # initialize Tree Control
        self.tree = wx.TreeCtrl(self, wx.ID_ANY, wx.DefaultPosition, (100, 150),
        # create Tree Control using Create() method
        # Add root to Tree Control
        self.root = self.tree.AddRoot('Root')
        # Add item to root
        self.itm = self.tree.AppendItem(self.root, 'Item')
        # Add item to 'itm'
        self.si1 = self.tree.AppendItem(self.itm, "Sub Item")
        # Add another item
        self.si2 = self.tree.AppendItem(self.itm, "Another Sub Item")
        # Expand whole tree
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.tree, 0, wx.EXPAND)
        # Add button in frame
        self.btn = wx.Button(self, 1, "Delete", (10, 170))
        # Bind event function with button
        self.btn.Bind(wx.EVT_BUTTON, self.onclick)
    def onclick(self, e):
        # Delete si2 from the tree
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()


before clicking button

after clicking button

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!