How to create a tree control (Phoenix)

Keywords : Tree control.


Introduction :

wx.TreeCtrl displays data in a hierarchy.

First of all we must create a root item.

root = self.tree.AddRoot('Programmer')

In our case, root item will not be displayed, because we used wx.TR_HIDE_ROOT flag in our constructor.

self.tree = wx.TreeCtrl(panel1, 1, wx.DefaultPosition, (-1,-1), wx.TR_HIDE_ROOT|wx.TR_HAS_BUTTONS)

We add items to the root item withAppendItem() method.

os = self.tree.AppendItem(root, 'Operating Systems')
pl = self.tree.AppendItem(root, 'Programming Languages')
tk = self.tree.AppendItem(root, 'Toolkits')

We can similarly create several levels by simply adding items to existing items.

We catch events with wx.EVT_TREE_SEL_CHANGED() event handler.

wx.EVT_TREE_SEL_CHANGED(self.tree, 1, self.OnSelChanged)

Various constructor style flags:

(info by ZetCode / Jan Bodnar).


Demonstrating :

Tested py3.x, wx4.x and Win10.

Are you ready to use some samples ? ;)

Test, modify, correct, complete, improve and share your discoveries ! (!)


Sample one :

With wx.Panel

img_sample_one_a.png

   1 # Sample_one_a.py
   2 
   3 """
   4 
   5 Author : Jan Bodnar
   6 Website : zetcode.com
   7 
   8 """
   9 
  10 import wx
  11 
  12 # class MyFrame
  13 # class MyApp
  14 
  15 #---------------------------------------------------------------------------
  16 
  17 class MyFrame(wx.Frame):
  18     def __init__(self, parent, id, title):
  19         wx.Frame.__init__(self, parent, id, title,
  20                           wx.DefaultPosition, wx.Size(500, 480))
  21 
  22         self.SetIcon(wx.Icon('./icons/wxwin.ico', wx.BITMAP_TYPE_ICO))
  23 
  24         #------------
  25         
  26         panel1 = wx.Panel(self, -1, style=wx.WANTS_CHARS)
  27         panel2 = wx.Panel(self, -1, style=wx.WANTS_CHARS)
  28         panel2.SetBackgroundColour('#c4c4c4')
  29         
  30         #------------
  31         
  32         self.tree = wx.TreeCtrl(panel1, 1,
  33                                 wx.DefaultPosition,
  34                                 wx.DefaultSize,
  35                                 wx.TR_HIDE_ROOT |
  36                                 wx.TR_HAS_BUTTONS)
  37         self.tree.SetBackgroundColour('#cce8ff')
  38         
  39         root = self.tree.AddRoot('Programmer')
  40         
  41         os = self.tree.AppendItem(root, 'Operating Systems')
  42         pl = self.tree.AppendItem(root, 'Programming Languages')
  43         tk = self.tree.AppendItem(root, 'Toolkits')
  44 
  45         cl = self.tree.AppendItem(pl, 'Compiled languages')
  46         sl = self.tree.AppendItem(pl, 'Scripting languages')
  47 
  48         self.tree.AppendItem(os, 'Linux')
  49         self.tree.AppendItem(os, 'FreeBSD')
  50         self.tree.AppendItem(os, 'OpenBSD')
  51         self.tree.AppendItem(os, 'NetBSD')
  52         self.tree.AppendItem(os, 'Solaris')
  53         self.tree.AppendItem(cl, 'Java')
  54         self.tree.AppendItem(cl, 'C++')
  55         self.tree.AppendItem(cl, 'C')
  56         self.tree.AppendItem(cl, 'Pascal')
  57         self.tree.AppendItem(sl, 'Python')
  58         self.tree.AppendItem(sl, 'Ruby')
  59         self.tree.AppendItem(sl, 'Tcl')
  60         self.tree.AppendItem(sl, 'PHP')
  61         self.tree.AppendItem(tk, 'Qt')
  62         self.tree.AppendItem(tk, 'MFC')
  63         self.tree.AppendItem(tk, 'wxPython')
  64         self.tree.AppendItem(tk, 'GTK+')
  65         self.tree.AppendItem(tk, 'Swing')
  66 
  67         self.tree.Bind(wx.EVT_TREE_SEL_CHANGED, self.OnSelChanged, self.tree)
  68         self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)
  69         
  70         #------------
  71         
  72         self.display = wx.StaticText(panel2, -1, '', (10, 10), style=wx.ALIGN_CENTRE)
  73 
  74         #------------
  75         
  76         hbox = wx.BoxSizer(wx.HORIZONTAL)
  77         vbox = wx.BoxSizer(wx.VERTICAL)
  78         
  79         vbox.Add(self.tree, 1, wx.EXPAND)
  80 
  81         hbox.Add(panel1, 1, wx.EXPAND)
  82         hbox.Add(panel2, 1, wx.EXPAND)
  83 
  84         panel1.SetSizer(vbox)
  85         self.SetSizer(hbox)
  86 
  87         #------------
  88         
  89         self.Centre()
  90 
  91     #-----------------------------------------------------------------------
  92 
  93     def OnSelChanged(self, event):
  94         self.item = event.GetItem()
  95         self.display.SetLabel(self.tree.GetItemText(self.item))
  96         self.tree.GetItemText(self.item)
  97 
  98         event.Skip()
  99         
 100 
 101     def OnCloseWindow(self, event):
 102         self.Destroy()
 103         
 104 #---------------------------------------------------------------------------
 105 
 106 class MyApp(wx.App):
 107     def OnInit(self):
 108         frame = MyFrame(None, -1, 'wx.TreeCtrl (wx.Panel)')
 109         frame.Show(True)
 110         self.SetTopWindow(frame)
 111         
 112         return True
 113 
 114 #---------------------------------------------------------------------------
 115 
 116 if __name__ == '__main__':
 117     app = MyApp(0)
 118     app.MainLoop()


With wx.SplitterWindow

img_sample_one_b.png

   1 # Sample_one_b.py
   2 
   3 """
   4 
   5 Author : Adil Hasan
   6 https://wiki.wxpython.org/AnotherTutorialTreeCtrlComment
   7 
   8 """
   9 
  10 import wx
  11 
  12 # class MyTree
  13 # class MyFrame
  14 # class MyApp
  15 
  16 #---------------------------------------------------------------------------
  17 
  18 class MyTree(wx.TreeCtrl):
  19     """
  20     Our customized TreeCtrl class.
  21     """
  22     def __init__(self, parent, id, position, size, style):
  23         """
  24         Initialize our tree.
  25         """
  26         wx.TreeCtrl.__init__(self, parent, id, position, size, style)
  27        
  28         root = self.AddRoot('Programmer')
  29         
  30         os = self.AppendItem(root, 'Operating Systems')
  31         pl = self.AppendItem(root, 'Programming Languages')
  32         tk = self.AppendItem(root, 'Toolkits')
  33            
  34         cl = self.AppendItem(pl, 'Compiled languages')
  35         sl = self.AppendItem(pl, 'Scripting languages')
  36 
  37         self.AppendItem(os, 'Linux')
  38         self.AppendItem(os, 'FreeBSD')
  39         self.AppendItem(os, 'OpenBSD')
  40         self.AppendItem(os, 'NetBSD')
  41         self.AppendItem(os, 'Solaris')        
  42         self.AppendItem(cl, 'Java')
  43         self.AppendItem(cl, 'C++')
  44         self.AppendItem(cl, 'C')
  45         self.AppendItem(cl, 'Pascal')
  46         self.AppendItem(sl, 'Python')
  47         self.AppendItem(sl, 'Ruby')
  48         self.AppendItem(sl, 'Tcl')
  49         self.AppendItem(sl, 'PHP')
  50         self.AppendItem(tk, 'Qt')
  51         self.AppendItem(tk, 'MFC')
  52         self.AppendItem(tk, 'wxPython')
  53         self.AppendItem(tk, 'GTK+')
  54         self.AppendItem(tk, 'Swing')
  55 
  56 #---------------------------------------------------------------------------
  57         
  58 class MyFrame(wx.Frame):
  59     """
  60     Our customized window class.
  61     """
  62     def __init__(self, parent, id, title):
  63         """
  64         Initialize our window.
  65         """
  66         wx.Frame.__init__(self, parent, id, title,
  67                           wx.DefaultPosition, wx.Size(500, 480))
  68 
  69         self.SetIcon(wx.Icon('./icons/wxwin.ico', wx.BITMAP_TYPE_ICO))
  70 
  71         #------------
  72         
  73         # Create a splitter window.
  74         self.splitter = wx.SplitterWindow(self, -1, style=wx.SP_LIVE_UPDATE)
  75         
  76         # Create the left panel.
  77         leftPanel = wx.Panel(self.splitter, -1)
  78         
  79         # Create our tree and put it into the left panel.
  80         self.tree = MyTree(leftPanel, 1,
  81                            wx.DefaultPosition,
  82                            wx.DefaultSize,
  83                            wx.TR_HIDE_ROOT |
  84                            wx.TR_HAS_BUTTONS)
  85         self.tree.SetBackgroundColour('#d8f0d4')
  86 
  87         # Bind the OnSelChanged method to the tree.
  88         self.tree.Bind(wx.EVT_TREE_SEL_CHANGED, self.OnSelChanged, id=1)
  89 
  90         # Create the right panel.
  91         rightPanel = wx.Panel(self.splitter, -1, style=wx.SUNKEN_BORDER)
  92         rightPanel.SetBackgroundColour('#f0d4e9')
  93         
  94         # Create a widget to display static text 
  95         # and store it in the right panel.
  96         self.display = wx.StaticText(rightPanel, -1, '',
  97                                      style=wx.ALIGN_LEFT)
  98         
  99         # Put the left and right panes into the split window.
 100         self.splitter.SplitVertically(leftPanel, rightPanel, 200)
 101 
 102         # Minimum size of subwindow.
 103         self.splitter.SetMinimumPaneSize(1)         
 104 
 105         #------------
 106 
 107         # Create a box sizer that will contain the left panel contents.
 108         leftBox = wx.BoxSizer(wx.VERTICAL)
 109         
 110         # Add the tree to the box sizer.
 111         leftBox.Add(self.tree, 1, wx.EXPAND)
 112         
 113         # Set the size of the right panel to that required by the tree.
 114         leftPanel.SetSizer(leftBox)
 115         
 116         # Create the right box sizer that will contain the panel's contents.
 117         rightBox = wx.BoxSizer(wx.VERTICAL)
 118         
 119         # Add the display widget to the right panel.
 120         rightBox.Add(self.display, 0, wx.ALL, 10)
 121 
 122         # Set the size of the right panel to that 
 123         # required by the display widget.
 124         rightPanel.SetSizer(rightBox)
 125         
 126         #------------
 127         
 128         # Create the window in the centre of the screen.
 129         self.Centre()
 130 
 131     #-----------------------------------------------------------------------
 132         
 133     def OnSelChanged(self, event):
 134         """
 135         Method called when selected item is changed.
 136         """
 137         
 138         # Get the selected item object.
 139         item =  event.GetItem()
 140         
 141         # Display the selected item text in the text widget.
 142         self.display.SetLabel(self.tree.GetItemText(item))
 143 
 144 #---------------------------------------------------------------------------
 145         
 146 class MyApp(wx.App):
 147     """
 148     Our application class.
 149     """
 150     def OnInit(self):
 151         """
 152         Initialize by creating the split window with the tree.
 153         """
 154         
 155         frame = MyFrame(None, -1, 'wx.TreeCtrl (wx.SplitterWindow)')
 156         frame.Show(True)
 157         self.SetTopWindow(frame)
 158         
 159         return True
 160 
 161 #---------------------------------------------------------------------------
 162     
 163 if __name__ == '__main__':
 164     app = MyApp(0)
 165     app.MainLoop()


Sample two

img_sample_two.png

   1 # sample_two.py
   2 
   3 """
   4 
   5 Author : Rahul Sabharwal
   6 https://www.geeksforgeeks.org/wxpython-treectrl/
   7 
   8 """
   9 
  10 import wx
  11 
  12 # class MyFrame
  13 # class MyApp
  14 
  15 #---------------------------------------------------------------------------
  16   
  17 class MyFrame(wx.Frame): 
  18     def __init__(self, parent, id, title): 
  19         wx.Frame.__init__(self, parent, -1, title) 
  20 
  21         self.SetIcon(wx.Icon('./icons/wxwin.ico', wx.BITMAP_TYPE_ICO))
  22 
  23         #------------
  24         
  25         # Tree control 
  26         self.tree = wx.TreeCtrl(self, wx.ID_ANY,
  27                                 wx.DefaultPosition,
  28                                 wx.DefaultSize) 
  29         self.tree.SetBackgroundColour('#d9f3fa')
  30         
  31         # Add root to tree 
  32         self.root = self.tree.AddRoot('Root ')
  33         
  34         # Add item to root 
  35         self.tree.AppendItem(self.root, 'Child 1') 
  36         self.tree.AppendItem(self.root, 'Child 2') 
  37         self.tree.AppendItem(self.root, 'Child 3') 
  38         self.tree.AppendItem(self.root, 'Child 4') 
  39         self.tree.AppendItem(self.root, 'Child 5') 
  40         self.tree.AppendItem(self.root, 'Child 6') 
  41         self.tree.AppendItem(self.root, 'Child 7') 
  42         self.tree.AppendItem(self.root, 'Child 8')
  43         
  44         # Expand tree 
  45         self.tree.Expand(self.root) 
  46 
  47         #------------
  48         
  49         # Show frame 
  50         self.Show() 
  51   
  52 #---------------------------------------------------------------------------
  53         
  54 class MyApp(wx.App):
  55     def OnInit(self):
  56         frame = MyFrame(None, -1, 'wx.TreeCtrl')
  57         frame.Show(True)
  58         self.SetTopWindow(frame)
  59         
  60         return True
  61 
  62 #---------------------------------------------------------------------------
  63     
  64 app = MyApp(0)
  65 app.MainLoop()


Sample three

img_sample_three.png

   1 # sample_three.py
   2 
   3 """
   4 
   5 Author : Rahul Sabharwal
   6 https://www.geeksforgeeks.org/wxpython-assignimagelist-method-in-wx-treectrl/?ref=rp
   7 
   8 """
   9 
  10 import wx
  11 
  12 # class MyFrame
  13 # class MyApp
  14 
  15 #---------------------------------------------------------------------------
  16   
  17 class MyFrame(wx.Frame): 
  18     def __init__(self, parent, id, title): 
  19         wx.Frame.__init__(self, parent, -1, title) 
  20 
  21         self.SetIcon(wx.Icon('./icons/wxwin.ico', wx.BITMAP_TYPE_ICO))
  22 
  23         #------------
  24         
  25         # Tree control 
  26         self.tree = wx.TreeCtrl(self, wx.ID_ANY,
  27                                 wx.DefaultPosition,
  28                                 wx.DefaultSize) 
  29         self.tree.SetBackgroundColour('#f1f2f2')
  30         
  31         # Create imagelist 
  32         il = wx.ImageList(16, 16) 
  33   
  34         # Add images to image list 
  35         one = il.Add(wx.Image('./bitmaps/plus.png', wx.BITMAP_TYPE_PNG).Scale(16, 16).ConvertToBitmap()) 
  36         two = il.Add(wx.Image('./bitmaps/close.png').Scale(16, 16).ConvertToBitmap()) 
  37   
  38         # Asign image list to tree 
  39         self.tree.AssignImageList(il) 
  40   
  41         # Add a root node to tree 
  42         self.root = self.tree.AddRoot('Root ', 0) 
  43   
  44         # Add item to self.root 
  45         self.tree.AppendItem(self.root, 'Item 1', 1) 
  46         self.tree.AppendItem(self.root, 'Item 2', 1)  
  47         self.tree.AppendItem(self.root, 'Item 3', 1)  
  48         self.tree.AppendItem(self.root, 'Item 4', 1)  
  49         self.tree.AppendItem(self.root, 'Item 5', 1)  
  50         self.tree.AppendItem(self.root, 'Item 6', 1)  
  51         self.tree.AppendItem(self.root, 'Item 7', 1) 
  52         self.tree.AppendItem(self.root, 'Item 8', 1) 
  53         
  54         # Expand tree 
  55         self.tree.Expand(self.root) 
  56 
  57         #------------
  58         
  59         # Show frame 
  60         self.Show()
  61         
  62 #---------------------------------------------------------------------------
  63         
  64 class MyApp(wx.App):
  65     def OnInit(self):
  66         frame = MyFrame(None, -1, 'wx.TreeCtrl (AssignImageList)')
  67         frame.Show(True)
  68         self.SetTopWindow(frame)
  69         
  70         return True
  71 
  72 #---------------------------------------------------------------------------
  73     
  74 app = MyApp(0)
  75 app.MainLoop()


Sample four

img_sample_four.png

   1 # sample_four.py
   2 
   3 """
   4 
   5 Author : Rahul Sabharwal
   6 https://www.geeksforgeeks.org/wxpython-collapse-method-wx-treectrl/?ref=rp
   7 
   8 """
   9 
  10 import wx
  11 
  12 # class MyTree
  13 # class MyTreePanel
  14 # class MyFrame
  15 # class MyApp
  16 
  17 #---------------------------------------------------------------------------
  18   
  19 class MyTree(wx.TreeCtrl):   
  20     def __init__(self, parent, id, pos, size, style): 
  21         wx.TreeCtrl.__init__(self, parent, id, pos, size, style) 
  22   
  23 #---------------------------------------------------------------------------
  24         
  25 class MyTreePanel(wx.Panel):   
  26     def __init__(self, parent): 
  27         wx.Panel.__init__(self, parent) 
  28   
  29         # Create tree control 
  30         self.tree = MyTree(self, wx.ID_ANY,
  31                            wx.DefaultPosition,
  32                            wx.DefaultSize,
  33                            wx.TR_HAS_BUTTONS) 
  34         self.tree.SetBackgroundColour('#f8f094')
  35         
  36         # Add root to self.tree 
  37         self.root = self.tree.AddRoot('Root') 
  38 
  39         # Add item to self.root 
  40         item = self.tree.AppendItem(self.root, 'Item 1') 
  41         item = self.tree.AppendItem(self.root, 'Item 2')
  42         item = self.tree.AppendItem(self.root, 'Item 3') 
  43         item = self.tree.AppendItem(self.root, 'Item 4') 
  44         item = self.tree.AppendItem(self.root, 'Item 5')
  45         item = self.tree.AppendItem(self.root, 'Item 6') 
  46         item = self.tree.AppendItem(self.root, 'Item 7')         
  47         item = self.tree.AppendItem(self.root, 'Item 8')
  48 
  49         # Expand whole tree 
  50         self.tree.Expand(self.root)
  51         
  52         #------------
  53         
  54         # Add button in panel 
  55         self.btn = wx.Button(self, 1, "&Collapse")
  56         
  57         # Bind event with self.btn 
  58         self.btn.Bind(wx.EVT_BUTTON, self.OnClick) 
  59 
  60         #------------
  61         
  62         sizer = wx.BoxSizer(wx.VERTICAL) 
  63         sizer.Add(self.tree, 1, wx.EXPAND) 
  64         sizer.Add(self.btn, 0, wx.EXPAND) 
  65         self.SetSizer(sizer) 
  66 
  67     #-----------------------------------------------------------------------  
  68 
  69     def OnClick(self, event): 
  70         # Collapse root 
  71         self.tree.Collapse(self.root) 
  72 
  73 #---------------------------------------------------------------------------
  74         
  75 class MyFrame(wx.Frame): 
  76     def __init__(self, parent, id, title): 
  77         wx.Frame.__init__(self, parent, -1, title)  
  78 
  79         self.SetIcon(wx.Icon('./icons/wxwin.ico', wx.BITMAP_TYPE_ICO))
  80 
  81         #------------
  82         
  83         panel = MyTreePanel(self) 
  84 
  85         #------------
  86         
  87         # Show frame 
  88         self.Show()
  89         
  90 #---------------------------------------------------------------------------
  91         
  92 class MyApp(wx.App):
  93     def OnInit(self):
  94         frame = MyFrame(None, -1, 'wx.TreeCtrl (Collapse)')
  95         frame.Show(True)
  96         self.SetTopWindow(frame)
  97         
  98         return True
  99 
 100 #---------------------------------------------------------------------------
 101     
 102 app = MyApp(0)
 103 app.MainLoop()


Sample five

img_sample_five.png

   1 # sample_five.py
   2 
   3 """
   4 
   5 Author : Rahul Sabharwal
   6 https://www.geeksforgeeks.org/wxpython-clearfocuseditem-method-in-wx-treectrl/?ref=rp
   7 
   8 """
   9 
  10 import wx
  11 
  12 # class MyTree
  13 # class MyFrame
  14 # class MyApp
  15 
  16 #---------------------------------------------------------------------------
  17   
  18 class MyTree(wx.Panel): 
  19   
  20     def __init__(self, parent): 
  21         wx.Panel.__init__(self, parent) 
  22   
  23         # create Tree Control in frame 
  24         self.tree = wx.TreeCtrl(self, wx.ID_ANY,
  25                                 wx.DefaultPosition,
  26                                 wx.DefaultSize,
  27                                 wx.TR_HAS_BUTTONS) 
  28         self.tree.SetBackgroundColour('#fddef5')
  29         
  30         # Create root for Tree Control 
  31         self.root = self.tree.AddRoot('Root') 
  32   
  33         # Add item to root 
  34         item = self.tree.AppendItem(self.root, 'Item 1') 
  35         item = self.tree.AppendItem(self.root, 'Item 2')
  36         item = self.tree.AppendItem(self.root, 'Item 3') 
  37         item = self.tree.AppendItem(self.root, 'Item 4') 
  38         item = self.tree.AppendItem(self.root, 'Item 5') 
  39         item = self.tree.AppendItem(self.root, 'Item 6') 
  40         item = self.tree.AppendItem(self.root, 'Item 7')
  41         item = self.tree.AppendItem(self.root, 'Item 8') 
  42   
  43         # Clear focused item 
  44         self.tree.ClearFocusedItem() 
  45   
  46         # expand tree 
  47         self.tree.Expand(self.root) 
  48 
  49         #------------
  50         
  51         sizer = wx.BoxSizer(wx.VERTICAL) 
  52         sizer.Add(self.tree, 1, wx.EXPAND) 
  53         self.SetSizer(sizer) 
  54 
  55 #---------------------------------------------------------------------------
  56         
  57 class MyFrame(wx.Frame):
  58     """
  59     Main root frame for tree control.
  60     """
  61     def __init__(self, parent, id, title): 
  62         wx.Frame.__init__(self, parent, -1, title)  
  63 
  64         self.SetIcon(wx.Icon('./icons/wxwin.ico', wx.BITMAP_TYPE_ICO))
  65 
  66         #------------
  67         
  68         panel = MyTree(self) 
  69 
  70         #------------
  71         
  72         # Show frame 
  73         self.Show()
  74         
  75 #---------------------------------------------------------------------------
  76         
  77 class MyApp(wx.App):
  78     def OnInit(self):
  79         frame = MyFrame(None, -1, 'wx.TreeCtrl (ClearFocusedItem)')
  80         frame.Show(True)
  81         self.SetTopWindow(frame)
  82         
  83         return True
  84 
  85 #---------------------------------------------------------------------------
  86     
  87 app = MyApp(0)
  88 app.MainLoop()


Sample six

img_sample_six.png

   1 # sample_six.py
   2 
   3 """
   4 
   5 Author : Rahul Sabharwal
   6 https://www.geeksforgeeks.org/wxpython-collapseallchildren-method-in-wx-treectrl/?ref=rp
   7 
   8 """
   9 
  10 import wx
  11 
  12 # class MyTree
  13 # class MyTreePanel
  14 # class MyFrame
  15 # class MyApp
  16 
  17 #---------------------------------------------------------------------------
  18   
  19 class MyTree(wx.TreeCtrl): 
  20     def __init__(self, parent, id, pos, size, style): 
  21         wx.TreeCtrl.__init__(self, parent, id, pos, size, style) 
  22   
  23 #---------------------------------------------------------------------------
  24         
  25 class MyTreePanel(wx.Panel):  
  26     def __init__(self, parent): 
  27         wx.Panel.__init__(self, parent) 
  28   
  29         # Create Tree Control 
  30         self.tree = MyTree(self, wx.ID_ANY,
  31                            wx.DefaultPosition,
  32                            wx.DefaultSize,
  33                            wx.TR_HAS_BUTTONS) 
  34         self.tree.SetForegroundColour('#ffffff')
  35         self.tree.SetBackgroundColour('#000000')
  36         
  37         # Add root to Tree Control 
  38         self.root = self.tree.AddRoot('Root') 
  39   
  40         # Add item to root 
  41         self.itm = self.tree.AppendItem(self.root, 'Item') 
  42   
  43         # Add item to 'itm' 
  44         self.itm2 = self.tree.AppendItem(self.itm, "Sub Item") 
  45   
  46         # Add child item to itm2 
  47         self.itm3 = self.tree.AppendItem(self.itm2, "Another Item") 
  48         self.itm4 = self.tree.AppendItem(self.itm2, "Another Item")
  49         self.itm5 = self.tree.AppendItem(self.itm2, "Another Item") 
  50         self.itm6 = self.tree.AppendItem(self.itm2, "Another Item")
  51         self.itm7 = self.tree.AppendItem(self.itm2, "Another Item")
  52         self.itm8 = self.tree.AppendItem(self.itm2, "Another Item") 
  53         self.itm9 = self.tree.AppendItem(self.itm2, "Another Item")
  54         
  55         # Expand whole tree 
  56         self.tree.Expand(self.root) 
  57 
  58         #------------
  59         
  60         # Add button in frame 
  61         self.btn = wx.Button(self, 1, "Collapse") 
  62 
  63         # Bind event function with button 
  64         self.btn.Bind(wx.EVT_BUTTON, self.OnClick) 
  65         
  66         #------------
  67         
  68         sizer = wx.BoxSizer(wx.VERTICAL) 
  69         sizer.Add(self.tree, 1, wx.EXPAND) 
  70         sizer.Add(self.btn, 0, wx.EXPAND)
  71         self.SetSizer(sizer) 
  72   
  73     #-----------------------------------------------------------------------
  74   
  75     def OnClick(self, event): 
  76         # Collapse all children of itm recursively 
  77         self.tree.CollapseAllChildren(self.itm) 
  78 
  79 #---------------------------------------------------------------------------
  80         
  81 class MyFrame(wx.Frame):
  82     def __init__(self, parent, id, title): 
  83         wx.Frame.__init__(self, parent, -1, title)  
  84 
  85         self.SetIcon(wx.Icon('./icons/wxwin.ico', wx.BITMAP_TYPE_ICO))
  86 
  87         #------------
  88         
  89         panel = MyTreePanel(self) 
  90 
  91         #------------
  92         
  93         # Show frame 
  94         self.Show()
  95         
  96 #---------------------------------------------------------------------------
  97         
  98 class MyApp(wx.App):
  99     def OnInit(self):
 100         frame = MyFrame(None, -1, 'wx.TreeCtrl (CollapseAllChildren)')
 101         frame.Show(True)
 102         self.SetTopWindow(frame)
 103         
 104         return True
 105 
 106 #---------------------------------------------------------------------------
 107     
 108 app = MyApp(0)
 109 app.MainLoop()


Download source

source.zip


Additional Information

Link :


https://wiki.wxpython.org/TitleIndex

https://docs.wxpython.org/


Thanks to

Jan Bodnar (sample_one.py coding), Adil Hasan (sample_two.py coding), Rahul Sabharwal (sample_three /four / five / six.py coding), the wxPython community...


About this page

Date(d/m/y) Person (bot) Comments :

21/01/21 - Ecco (Created page for wxPython Phoenix).


Comments

- blah, blah, blah....

How to create a tree control (Phoenix) (last edited 2021-01-21 14:07:08 by Ecco)

NOTE: To edit pages in this wiki you must be a member of the TrustedEditorsGroup.