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.
Syntax: wx.TreeCtrl.TreeCtrl(parent, id=ID_ANY, pos=DefaultPosition, size=DefaultSize, style=TR_DEFAULT_STYLE, validator=DefaultValidator, name=TreeCtrlNameStr)
Parameters
Parameter Input Type Description parent wx.Window Parent window. Must not be None. id wx.WindowID Window identifier. The value ID_ANY indicates a default value. pos wx.Point Window position. If wx.DefaultPosition is specified then a default position is chosen. size wx.Size Window size. If wx.DefaultSize is specified then the window is sized appropriately. style long Window Style. validator wx.Validator Window Validator name string Window 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
self .tree.Expand( self .root)
# show frame
self .Show()
if __name__ = = '__main__' :
app = wx.App(redirect = False )
frame = MainFrame()
app.MainLoop()
|
Output Window: