Windows Errors? Fix Them Before They Spread
Repair common Windows errors and clear accumulated junk for a smoother, more stable PC - no reinstall needed.Free scan · no reinstallOutdated Drivers Are Slowing You Down
One free scan finds every outdated or missing driver and matches the right update for your exact hardware.Free scan · exact hardware matchA wxPython wx.TreeCtrl displays hierarchical items that users can expand, collapse, select, and navigate. For a small tree, create a root with AddRoot, add descendants with AppendItem, and expand the branch you want shown. For larger or remotely loaded trees, add children on the first expansion instead of building every node up front.
How to create a basic wx.TreeCtrl
The minimal pattern is to create the control with a parent window, add a root, append child items, and expand the root. The example below uses the native control and a frame sizer so it fills the window:
import wx
app = wx.App()
frame = wx.Frame(None, title="TreeCtrl example", size=(400, 300))
tree = wx.TreeCtrl(frame, style=wx.TR_HAS_BUTTONS)
root = tree.AddRoot("Root")
child = tree.AppendItem(root, "Child")
tree.Expand(root)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(tree, 1, wx.EXPAND)
frame.SetSizer(sizer)
frame.Show()
app.MainLoop()
AddRoot returns the root item identifier; AppendItem returns an identifier for the new child. Keep these identifiers to add further descendants or act on particular items. The official wx.TreeCtrl overview describes each tree item as having a label and an optional icon.
How to add data and organize items
A tree item is more than its displayed label. Associate a domain object or other application-specific value with an item rather than trying to encode all state in the label. The control uses opaque wx.TreeItemId values to identify items; optional per-item data can be attached with wx.TreeItemData and retrieved with GetItemData. The control manages the lifetime of associated data when items are deleted, as documented in the TreeCtrl overview.
What’s actually slowing this PC down?
Pick the symptom - the matching free tool is one click away.
#1 Best Overall
For example, a file browser can display a file name while associating the corresponding path or model object with that item. This keeps presentation and application state separate. The 2017 DZone TreeCtrl tutorial demonstrates using Python data with tree items and building a tree from XML elements.
How to populate a tree only when a branch expands
Building every node at startup can be wasteful when a tree represents a large or remote hierarchy. The wxPython documentation recommends creating the root first and adding each item’s immediate children when the user first expands that item, using wx.EVT_TREE_ITEM_EXPANDING. Track which items have already been populated: otherwise, collapsing and expanding the same branch can append duplicate children. See the official overview’s lazy-population guidance.
Rank #2
A common implementation approach is to keep a set of populated item identifiers or attach a populated flag to the associated item data. Bind the event, check the flag, fetch or construct immediate children only when needed, append them, and then mark the item as populated. If loading can fail, avoid marking the item populated until the children were successfully created, so a later expansion can retry.
How to respond to selection and expansion
Bind the tree events your application needs on the control. For example, an expansion handler can populate a branch, while a selection handler can update a details pane from the selected item’s data. The exact handler should use the event’s item identifier rather than infer identity from the visible label, because labels need not be unique.
Free tools Windows power users keep installed
One-click scans. No signup required.
tree.Bind(wx.EVT_TREE_ITEM_EXPANDING, self.on_item_expanding)
tree.Bind(wx.EVT_TREE_SEL_CHANGED, self.on_selection_changed)
These bindings belong in a panel or frame class where the handler methods can access the tree and the application’s data model. The expansion event is especially useful for deferred population; selection events are useful when selecting a node should display related information elsewhere in the interface.
Useful native TreeCtrl operations
The native control includes operations for walking, sorting, editing, and inspecting the tree. Its documented keyboard navigation includes the arrow keys, HOME, END, +, -, and *. DEL and INS have no default action, so an application must define their behavior if needed. The wx.TreeCtrl documentation describes these capabilities.
GetFirstChildandGetNextChildenumerate an item’s children.SortChildrensorts a node’s children alphabetically by default.HitTestidentifies the item at a screen position, which is useful for pointer interactions.EditLabelstarts in-place label editing.- Selection, visibility, and expanded-state queries let code inspect the tree’s current state.
Should you use wx.TreeCtrl or CustomTreeCtrl?
Use the native wx.TreeCtrl when its standard tree behavior and platform-native appearance are sufficient. The wxPython AGW library’s CustomTreeCtrl is an alternative when the interface needs richer item presentation or interactions. Its documentation says it supports TreeCtrl methods and most styles, while adding features such as check and radio items, hyperlink items, multiline labels, embedded widgets, customized drag-and-drop, and ellipsis with tooltips for long items. Compare the documented feature sets in the CustomTreeCtrl reference.
| Need | Native wx.TreeCtrl | AGW CustomTreeCtrl |
|---|---|---|
| Native platform look and standard tree behavior | Designed as the regular tree control | Custom-drawn alternative; verify appearance and behavior in your target environment |
| Checkboxes, radio items, and check propagation | Not established as built-in in the cited overview | Documented; includes styles such as TR_AUTO_CHECK_CHILD, TR_AUTO_CHECK_PARENT, and TR_AUTO_TOGGLE_CHILD |
| Hyperlinks, multiline labels, or embedded widgets | Not established as built-in in the cited overview | Documented features |
| Long labels | Not established as providing ellipsis and tooltips in the cited overview | Supports ellipsis and tooltips |
| Drag-and-drop customization | Not established by the cited overview | Offers customized drag-and-drop options |
| Documentation revision metadata | Not stated in the cited overview | The AGW page records version 2.7 and a latest revision dated 9 Aug. 2018 |
The AGW page’s version and revision are historical documentation metadata, not a guarantee of compatibility with every current wxPython release. Check the reference documentation and test the control with the wxPython version and platforms your application supports before choosing it.
Best Value
When a tree control is a good fit
A TreeCtrl is suited to data with a parent-child hierarchy: for example, XML elements, folders, categories, or nested objects. Store the underlying object or identifier with each item, populate large branches on demand, and use the native control unless the application genuinely needs CustomTreeCtrl’s additional item types or presentation features.
Quick Recap
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

