Adil Hasan

Email: <paradox2005 AT SPAMFREE gmail DOT com>


This is a little extension to the AnotherTutorial TreeCtrl example. I've included a splitter window and broke out the TreeCtrl stuff as an extension to the TreeCtrl class. I'm putting this as a comment in AnotherTutorialTreeCtrlComment and will add a comment to the tutorial page as well.

# treectrlwithsplitter.py

import wx

class MyTree(wx.TreeCtrl):
    '''Class of our TreeCtrl
    '''
    def __init__(self, parent, id, position, size, style):
        '''Initialize our tree
        '''
        wx.TreeCtrl.__init__(self, parent, id, position, size, style)
        root = self.AddRoot('Programmer')
        os = self.AppendItem(root, 'Operating Systems')
        pl = self.AppendItem(root, 'Programming Languages')
        tk = self.AppendItem(root, 'Toolkits')
        self.AppendItem(os, 'Linux')
        self.AppendItem(os, 'FreeBSD')
        self.AppendItem(os, 'OpenBSD')
        self.AppendItem(os, 'NetBSD')
        self.AppendItem(os, 'Solaris')
        cl = self.AppendItem(pl, 'Compiled languages')
        sl = self.AppendItem(pl, 'Scripting languages')
        self.AppendItem(cl, 'Java')
        self.AppendItem(cl, 'C++')
        self.AppendItem(cl, 'C')
        self.AppendItem(cl, 'Pascal')
        self.AppendItem(sl, 'Python')
        self.AppendItem(sl, 'Ruby')
        self.AppendItem(sl, 'Tcl')
        self.AppendItem(sl, 'PHP')
        self.AppendItem(tk, 'Qt')
        self.AppendItem(tk, 'MFC')
        self.AppendItem(tk, 'wxPython')
        self.AppendItem(tk, 'GTK+')
        self.AppendItem(tk, 'Swing')

class MyFrame(wx.Frame):
    '''Class of our widget
    '''
    def __init__(self, parent, id, title):
        '''Initialize our widget
        '''
        wx.Frame.__init__(self, parent, id, title,
                          wx.DefaultPosition, wx.Size(450, 350))

        self.splitter = wx.SplitterWindow(self, -1)

        panel1 = wx.Panel(self.splitter, -1)
        box1 = wx.BoxSizer(wx.VERTICAL)
        self.tree = MyTree(panel1, 1, wx.DefaultPosition, (-1, -1),
                           wx.TR_HIDE_ROOT|wx.TR_HAS_BUTTONS)

        self.tree.Bind(wx.EVT_TREE_SEL_CHANGED, self.OnSelChanged, id=1)
        box1.Add(self.tree, 1, wx.EXPAND)
        panel1.SetSizer(box1)

        panel2 = wx.Panel(self.splitter, -1)
        box2 = wx.BoxSizer(wx.VERTICAL)
        self.display = wx.StaticText(panel2, -1, '', (10, 10),
                                     style=wx.ALIGN_CENTRE)
        box2.Add(self.display, 1, wx.EXPAND)
        panel2.SetSizer(box2)

        self.splitter.SplitVertically(panel1, panel2)
        self.Centre()

    def OnSelChanged(self, event):
        '''Method called when selected item is changed
        '''
        item =  event.GetItem()
        self.display.SetLabel(self.tree.GetItemText(item))

class MyApp(wx.App):
    '''Our application class
    '''
    def OnInit(self):
        '''Initialize by creating the frame
        '''
        frame = MyFrame(None, -1, 'treectrl.py')
        frame.Show(True)
        self.SetTopWindow(frame)
        return True

if __name__ == '__main__':
    app = MyApp(0)
    app.MainLoop()

AdilHasan (last edited 2008-04-07 13:51:28 by mimosait)

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