=== wx.Choicebook === {{http://www.blog.pythonlibrary.org/wp-content/uploads/2009/12/choicebookDemo.png}} The wx.Choicebook is a fusion of the notebook and wx.Choice widgets. This allows the user to click a drop-down control to choose which page to view. The Choicebook also inherits from wx.!BookCtrlBase, so it has most of the same methods that wx.Notebook has. The screenshot above is from my original [[http://www.blog.pythonlibrary.org/2009/12/03/the-book-controls-of-wxpython-part-1-of-2/2/|tutorial]] on this widget. Let’s take a look at a quick demo: {{{ #!python import wx ######################################################################## 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 ChoicebookDemo(wx.Choicebook): """ Choicebook class """ #---------------------------------------------------------------------- def __init__(self, parent): wx.Choicebook.__init__(self, parent, wx.ID_ANY) # Create the first tab and add it to the notebook tabOne = TabPanel(self) tabOne.SetBackgroundColour("Gray") self.AddPage(tabOne, "Book One") # Create and add the second tab tabTwo = TabPanel(self) self.AddPage(tabTwo, "Book Two") # Create and add the third tab self.AddPage(TabPanel(self), "Book Three") self.Bind(wx.EVT_CHOICEBOOK_PAGE_CHANGED, self.OnPageChanged) self.Bind(wx.EVT_CHOICEBOOK_PAGE_CHANGING, self.OnPageChanging) #---------------------------------------------------------------------- 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, "Choicebook Tutorial", size=(600,400)) panel = wx.Panel(self) notebook = ChoicebookDemo(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() }}} The code above is mostly the same as that of the notebook example, but even simpler. You just use the AddPage() method to add a new page to this book control too. The rest of the methods are pretty much the same as well from what I could see from the documentation. Take note that the Choicebook does have its own set of specially named events. If you use one of the other book’s event names instead, you’ll quickly find that your event handlers are not fired. '''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.Choicebook-class.html|wx.Choicebook Official 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]]''