=== wx.Treebook === {{http://www.blog.pythonlibrary.org/wp-content/uploads/2009/12/treebookDemo.png}} The Treebook control is a combination of the wx.TreeCtrl and the wx.Notebook. Most TreeCtrls that I’ve seen do not use images, but the wxPython demo uses them for the Treebook, so I decided to use them too. As you can see from the screenshot above, the bitmaps give the Treebook an interesting flavor. Let’s see how to make one of these controls! {{{ #!python import images import wx def getNextImageID(count): imID = 0 while True: yield imID imID += 1 if imID == count: imID = 0 ######################################################################## class TabPanel(wx.Panel): """ This will be the first notebook tab """ #---------------------------------------------------------------------- def __init__(self, parent): """""" wx.Panel.__init__(self, parent=parent, id=wx.ID_ANY) sizer = wx.BoxSizer(wx.VERTICAL) txtOne = wx.TextCtrl(self, wx.ID_ANY, "") txtTwo = wx.TextCtrl(self, wx.ID_ANY, "") sizer = wx.BoxSizer(wx.VERTICAL) sizer.Add(txtOne, 0, wx.ALL, 5) sizer.Add(txtTwo, 0, wx.ALL, 5) self.SetSizer(sizer) ######################################################################## class TreebookDemo(wx.Treebook): """ Treebook class """ #---------------------------------------------------------------------- def __init__(self, parent): """Constructor""" wx.Treebook.__init__(self, parent, wx.ID_ANY, style= wx.BK_DEFAULT #wx.BK_TOP #wx.BK_BOTTOM #wx.BK_LEFT #wx.BK_RIGHT ) il = wx.ImageList(32, 32) for x in range(6): obj = getattr(images, 'LB%02d' % (x+1)) bmp = obj.GetBitmap() il.Add(bmp) self.AssignImageList(il) imageIdGenerator = getNextImageID(il.GetImageCount()) pages = [(panelOne.TabPanel(self), "Panel One"), (panelTwo.TabPanel(self), "Panel Two"), (panelThree.TabPanel(self), "Panel Three")] imID = 0 for page, label in pages: self.AddPage(page, label, imageId=imageIdGenerator.next()) imID += 1 self.AddSubPage(page, 'a sub-page', imageId=imageIdGenerator.next()) self.Bind(wx.EVT_TREEBOOK_PAGE_CHANGED, self.OnPageChanged) self.Bind(wx.EVT_TREEBOOK_PAGE_CHANGING, self.OnPageChanging) # This is a workaround for a sizing bug on Mac... wx.FutureCall(100, self.AdjustSize) #---------------------------------------------------------------------- def AdjustSize(self): #print self.GetTreeCtrl().GetBestSize() self.GetTreeCtrl().InvalidateBestSize() self.SendSizeEvent() #---------------------------------------------------------------------- def OnPageChanged(self, event): old = event.GetOldSelection() new = event.GetSelection() sel = self.GetSelection() print 'OnPageChanged, old:%d, new:%d, sel:%d\n' % (old, new, sel) event.Skip() #---------------------------------------------------------------------- def OnPageChanging(self, event): old = event.GetOldSelection() new = event.GetSelection() sel = self.GetSelection() print 'OnPageChanging, old:%d, new:%d, sel:%d\n' % (old, new, sel) event.Skip() ######################################################################## class DemoFrame(wx.Frame): """ Frame that holds all other widgets """ #---------------------------------------------------------------------- def __init__(self): """Constructor""" wx.Frame.__init__(self, None, wx.ID_ANY, "Treebook Tutorial", size=(700,400) ) panel = wx.Panel(self) notebook = TreebookDemo(panel) sizer = wx.BoxSizer(wx.VERTICAL) sizer.Add(notebook, 1, wx.ALL|wx.EXPAND, 5) panel.SetSizer(sizer) self.Layout() self.Show() #---------------------------------------------------------------------- if __name__ == "__main__": app = wx.PySimpleApp() frame = DemoFrame() app.MainLoop() }}} As with the [[http://wiki.wxpython.org/Toolbook|Toolbook]], I once again used a some code from the wxPython demo to create this demo and reused some of my own “framework” code. Besides the specialized events of the Treebook, you should also take note that it has an !AddSubPage method, which adds a sub-node to the tree which in turn adds another page to the notebook. There are several other methods that only this control has: !CollapseNode, !ExpandNode, !GetPageParent, !GetTreeCtrl, !InsertSubPage and !IsNodeExpanded. I think they’re pretty self-explanatory, but don’t be afraid to read the documentation. '''All code was tested on the following:''' * Windows XP, wxPython 2.8.10.1 (unicode), Python 2.5 * Windows Vista, wxPython 2.8.10.1 (unicode), Python 2.5 === For Further Information === * [[http://www.wxpython.org/docs/api/wx.Treebook-class.html|Official wx.Treebook Documentation]] * [[http://wxpython.org/download.php|Official wxPython Demo]] ''Note: The tutorial on this page is a modified version of the tutorial on my [[http://www.blog.pythonlibrary.org/2009/12/03/the-book-controls-of-wxpython-part-1-of-2/|blog]]''