Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

wxPython TreeCtrl

  • Last Updated : 08 Jul, 2020

In this article we are going to learn about and TreeCtrl and how can we add TreeCtrl to our window. A tree control presents information as a hierarchy, with items that may be expanded to show further items. Items in a tree control are referenced by wx.TreeItemId handles, which may be tested for validity by calling wx.TreeItemId.IsOk .

We will use TreeCtrl() constructor to create TreeCtrl.

 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

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


ParameterInput TypeDescription
parentwx.WindowParent window. Must not be None.
idwx.WindowIDWindow identifier. The value ID_ANY indicates a default value.
poswx.PointWindow position. If wx.DefaultPosition is specified then a default position is chosen.
sizewx.SizeWindow size. If wx.DefaultSize is specified then the window is sized appropriately.
stylelongWindow Style.
validatorwx.ValidatorWindow Validator
namestringWindow name

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 root to tree
        self.root = self.tree.AddRoot('Root ')
        # add item to root
        self.tree.AppendItem(self.root, 'Child')
        # expand tree
        # show frame
if __name__ == '__main__':
    app = wx.App(redirect = False)
    frame = MainFrame()

Output Window:

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!