wxPython – Two step creation wx.TreeCtrl

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 paranthesis and then we will use Create() method and attributes to associate with Tree Control.

Syntax:

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

Parameters:

Parameter Type Description
parent wx.Window parent window/frame for Tree Control.
id wx.WindowID widget identifier to be associated with Tree Control
pos wx.Point position where to put Tree Control.
size wx.Size size of the Tree Control widget
style long style for Tree Control.
validator wx.Validator Validator associated withTree Control.
name string Name of Tree Control.

Code Example:



Python

filter_none

edit
close

play_arrow

link
brightness_4
code

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),
                           wx.TR_HAS_BUTTONS)
  
        # create Tree Control using Create() method
        self.tree.Create
        # 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
        self.tree.Expand(self.root)
  
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.tree, 0, wx.EXPAND)
        self.SetSizer(sizer)
  
         
  
class MainFrame(wx.Frame):
  
    def __init__(self):
        wx.Frame.__init__(self, parent = None, title ='TreeCtrl Demo')
        panel = TreePanel(self)
        self.Show()
  
  
if __name__ == '__main__':
    app = wx.App(redirect = False)
    frame = MainFrame()
    app.MainLoop()

chevron_right


Output:

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.




My Personal Notes arrow_drop_up

Hi there I am a student of CGC COE, Landran in CSE department and have a great interest in programming

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.