Friday, November 21, 2025
HomeLanguageswxPython – AddRoot() method in wx.TreeCtrl

wxPython – AddRoot() method in wx.TreeCtrl

In this article we are going to learn about AddRoot() method associated with wx.TreeCtrl class of wxPython. AddRoot() is a basic method used in order to add the root node to the tree, returning the new item.

The image and selImage parameters are an index within the normal image list specifying the image to use for unselected and selected items, respectively. If image > -1 and selImage is -1, the same image is used for both selected and unselected items.

Syntax: wx.TreeCtrl.AddRoot(self, text, image=-1, selImage=-1, data=None)

Parameters

Parameter Input Type Description
text string text on node
image int image parameter is an index within the normal image list specifying the image to unselected items, respectively.
selImage int selImage parameter is an index within the normal image list specifying the image to selected items, respectively.
data TreeItemData data for root item.

Code Example:




import wx
  
class MainFrame(wx.Frame):
  
    def __init__(self):
        wx.Frame.__init__(self, parent = None, title ='TreeCtrl Demo')
        # tree control
        self.tree = wx.TreeCtrl(self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize)
  
        # add a root node to tree
        self.root = self.tree.AddRoot('Root ')
  
        # expand tree
        self.tree.Expand(self.root)
  
        # show frame
        self.Show()
  
  
if __name__ == '__main__':
    app = wx.App(redirect = False)
    frame = MainFrame()
    app.MainLoop()


Output Window:

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32407 POSTS0 COMMENTS
Milvus
97 POSTS0 COMMENTS
Nango Kala
6783 POSTS0 COMMENTS
Nicole Veronica
11929 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11999 POSTS0 COMMENTS
Shaida Kate Naidoo
6907 POSTS0 COMMENTS
Ted Musemwa
7168 POSTS0 COMMENTS
Thapelo Manthata
6863 POSTS0 COMMENTS
Umr Jansen
6847 POSTS0 COMMENTS