=== wx.Toolbook === [[attachment:Toolbook_Demo.zip]] {{http://www.blog.pythonlibrary.org/wp-content/uploads/2009/12/toolbookDemo.png}} The Toolbook is a wx.Toolbar plus a wx.Notebook, which means that you use labeled bitmap buttons to control which “tab” of the notebook you are viewing. As you probably noticed with the Listbook example, I used the wxPython demo’s “images” module for its images and I use it again in my sample code here. This first piece will make up the panel that we'll base our tabs on: {{{ #!python import wx import wx.lib.mixins.listctrl as listmix musicdata = { 1 : ("Bad English", "The Price Of Love", "Rock"), 2 : ("DNA featuring Suzanne Vega", "Tom's Diner", "Rock"), 3 : ("George Michael", "Praying For Time", "Rock"), 4 : ("Gloria Estefan", "Here We Are", "Rock"), 5 : ("Linda Ronstadt", "Don't Know Much", "Rock"), 6 : ("Michael Bolton", "How Am I Supposed To Live Without You", "Blues"), 7 : ("Paul Young", "Oh Girl", "Rock"), 8 : ("Paula Abdul", "Opposites Attract", "Rock"), 9 : ("Richard Marx", "Should've Known Better", "Rock"), 10: ("Rod Stewart", "Forever Young", "Rock"), 11: ("Roxette", "Dangerous", "Rock"), 12: ("Sheena Easton", "The Lover In Me", "Rock"), 13: ("Sinead O'Connor", "Nothing Compares 2 U", "Rock"), 14: ("Stevie B.", "Because I Love You", "Rock"), 15: ("Taylor Dayne", "Love Will Lead You Back", "Rock"), 16: ("The Bangles", "Eternal Flame", "Rock"), 17: ("Wilson Phillips", "Release Me", "Rock"), 18: ("Billy Joel", "Blonde Over Blue", "Rock"), 19: ("Billy Joel", "Famous Last Words", "Rock"), 20: ("Billy Joel", "Lullabye (Goodnight, My Angel)", "Rock"), 21: ("Billy Joel", "The River Of Dreams", "Rock"), 22: ("Billy Joel", "Two Thousand Years", "Rock") } class TestListCtrl(wx.ListCtrl, listmix.ListCtrlAutoWidthMixin): def __init__(self, parent, ID, pos=wx.DefaultPosition, size=wx.DefaultSize, style=0): wx.ListCtrl.__init__(self, parent, ID, pos, size, style) listmix.ListCtrlAutoWidthMixin.__init__(self) class TabPanel(wx.Panel, listmix.ColumnSorterMixin): """ This will be the second notebook tab """ #---------------------------------------------------------------------- def __init__(self, parent): """""" wx.Panel.__init__(self, parent=parent, id=wx.ID_ANY) self.createAndLayout() def createAndLayout(self): sizer = wx.BoxSizer(wx.VERTICAL) self.list = TestListCtrl(self, wx.ID_ANY, style=wx.LC_REPORT | wx.BORDER_NONE | wx.LC_EDIT_LABELS | wx.LC_SORT_ASCENDING) sizer.Add(self.list, 1, wx.EXPAND) self.populateList() # Now that the list exists we can init the other base class, # see wx/lib/mixins/listctrl.py self.itemDataMap = musicdata listmix.ColumnSorterMixin.__init__(self, 3) self.SetSizer(sizer) self.SetAutoLayout(True) def populateList(self): self.list.InsertColumn(0, "Artist") self.list.InsertColumn(1, "Title", wx.LIST_FORMAT_RIGHT) self.list.InsertColumn(2, "Genre") items = musicdata.items() for key, data in items: index = self.list.InsertStringItem(sys.maxint, data[0]) self.list.SetStringItem(index, 1, data[1]) self.list.SetStringItem(index, 2, data[2]) self.list.SetItemData(index, key) self.list.SetColumnWidth(0, wx.LIST_AUTOSIZE) self.list.SetColumnWidth(1, wx.LIST_AUTOSIZE) self.list.SetColumnWidth(2, 100) # show how to select an item self.list.SetItemState(5, wx.LIST_STATE_SELECTED, wx.LIST_STATE_SELECTED) # show how to change the colour of a couple items item = self.list.GetItem(1) item.SetTextColour(wx.BLUE) self.list.SetItem(item) item = self.list.GetItem(4) item.SetTextColour(wx.RED) self.list.SetItem(item) self.currentItem = 0 # Used by the ColumnSorterMixin, see wx/lib/mixins/listctrl.py def GetListCtrl(self): return self.list }}} Now we can use the panel above for our Toolbook code demo, which is below. {{{ #!python import images import wx #---------------------------------------------------------------------- def getNextImageID(count): imID = 0 while True: yield imID imID += 1 if imID == count: imID = 0 ######################################################################## class ToolbookDemo(wx.Toolbook): """ Toolbook class """ #---------------------------------------------------------------------- def __init__(self, parent): """Constructor""" wx.Toolbook.__init__(self, parent, wx.ID_ANY, style= wx.BK_DEFAULT #wx.BK_TOP #wx.BK_BOTTOM #wx.BK_LEFT #wx.BK_RIGHT ) # make an image list using the LBXX images il = wx.ImageList(32, 32) for x in range(3): 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.Bind(wx.EVT_TOOLBOOK_PAGE_CHANGED, self.OnPageChanged) self.Bind(wx.EVT_TOOLBOOK_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, "Toolbook Tutorial", size=(700,400) ) panel = wx.Panel(self) notebook = ToolbookDemo(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 Toolbook follows in the footsteps of the previous controls as its lineage comes from the wx.!BookCtrlBase. This widget’s claim to fame is its look and feel (although it looks very similar to the [[http://wiki.wxpython.org/Listbook|Listbook]]) and the Toolbook’s unique events. I used some fun code from the wxPython demo to help in assigning images to the toolbar’s buttons, but that’s really the only difference of any importance. For full disclosure, read the docs for this control! '''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.Toolbook-class.html|Official wx.Toolbook 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]]''