Skip to content
Related Articles
Open in App
Not now

Related Articles

wxPython – Two step creation wx.TreeCtrl

Improve Article
Save Article
  • Last Updated : 01 Feb, 2022
Improve Article
Save Article

In this article we are going to learn that how can we create a Tree Control using two step creation. In order to do that we will use Create() method in wx.TreeCtrl class. Basically, we will initialize tree control using TreeCtrl() constructor with empty parentheses and then we will use Create() method and attributes to associate with Tree Control.


wx.TreeCtrl.Create(self, parent, id=ID_ANY, pos=DefaultPosition, size=DefaultSize, style=TR_DEFAULT_STYLE, validator=DefaultValidator, name=TreeCtrlNameStr)


parentwx.Windowparent window/frame for Tree Control.
idwx.WindowIDwidget identifier to be associated with Tree Control
poswx.Pointposition where to put Tree Control.
sizewx.Sizesize of the Tree Control widget
stylelongstyle for Tree Control.
validatorwx.ValidatorValidator associated withTree Control.
namestringName of 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, 70),
        # create Tree Control using Create() method
        # Add root to Tree Control
        self.root = self.tree.AddRoot('Root')
        # Add item to root
        itm = self.tree.AppendItem(self.root, 'Item')
        # Add item to 'itm'
        self.tree.AppendItem(itm, "Sub Item")
        # Expand whole tree
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.tree, 0, wx.EXPAND)
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()


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!