Thu 3 Dec 2009
The “Book” Controls of wxPython (Part 1 of 2)
Posted by Mike under Python, wxPython
[24] Comments
wx.Choicebook
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. Let’s take a look at a quick demo:
import wx import panelOne, panelTwo, panelThree ######################################################################## 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 = panelOne.TabPanel(self) tabOne.SetBackgroundColour("Gray") self.AddPage(tabOne, "Book One") # Create and add the second tab tabTwo = panelTwo.TabPanel(self) self.AddPage(tabTwo, "Book Two") # Create and add the third tab self.AddPage(panelThree.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.
wx.Listbook
The Listbook control uses a ListCtrl to instead of tabs to control the notebook. In this case, you can actually use labeled pictures to change tabs. It’s kind of weird, but I think it’s kind of cool too. As with the Choicebook, this control inherits from the BookCtrlBase and has the same methods. The primary differences seem to lie with the Listbook’s appearance and in its unique set of events. Let’s take a quick peek at my demo to see how to create one!
import images import wx import panelOne, panelTwo, panelThree ######################################################################## class ListbookDemo(wx.Listbook): """""" #---------------------------------------------------------------------- def __init__(self, parent): """Constructor""" wx.Listbook.__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) 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=imID) imID += 1 self.Bind(wx.EVT_LISTBOOK_PAGE_CHANGED, self.OnPageChanged) self.Bind(wx.EVT_LISTBOOK_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, "Listbook Tutorial", size=(700,400) ) panel = wx.Panel(self) notebook = ListbookDemo(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 Listbook’s “tab” controls can be set run along any of the sides, just like the wx.Notebook. You can also attach images with the same methods we used in the Notebook example at the beginning. I shortened the code up a bit by putting the panels in a list and looping over it, but other than that, there’s not much more to write about concerning this widget.
(Click page 3 to continue)
-
http://launchpad.net/whyteboard Steven Sproat
-
http://launchpad.net/whyteboard Steven Sproat
-
Mo
-
Mo
-
RL Frost
-
RL Frost
-
http://www.pythonlibrary.org/ mld
-
http://www.pythonlibrary.org/ mld
-
wxLover
-
wxLover
-
MrC
-
driscollis
-
driscollis
-
driscollis
-
MrC
-
MrC
-
driscollis
-
MrC
-
driscollis
-
MrC
-
jd
-
Anonymous


