=== wx.Notebook === The wx.Notebook is a control that many people will be familiar with if they've used Windows for very long or Mozilla Firefox. Basically, a notebook is a widget that contains one or more tabs that you can switch between. On Windows, you usually see them in preference dialogs. The following is a screenshot of what a notebook could look like: {{http://www.blog.pythonlibrary.org/wp-content/uploads/2009/12/notebookDemo.png}} Now let's create this notebook so we can learn how it's done. Each notebook tab (or page) will be made with a wx.Panel object. I have taken some code from the [[http://wxpython.org/download.php|wxPython Demo]] for these panels (and the book widgets) and modified them for this tutorial. Here's a runnable example that will generate the example shown in the screenshot above: {{{ #!python import images 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 NotebookDemo(wx.Notebook): """ Notebook class """ #---------------------------------------------------------------------- def __init__(self, parent): wx.Notebook.__init__(self, parent, id=wx.ID_ANY, style= wx.BK_DEFAULT #wx.BK_TOP #wx.BK_BOTTOM #wx.BK_LEFT #wx.BK_RIGHT ) # Create the first tab and add it to the notebook tabOne = TabPanel(self) tabOne.SetBackgroundColour("Gray") self.AddPage(tabOne, "TabOne") # Show how to put an image on one of the notebook tabs, # first make the image list: il = wx.ImageList(16, 16) idx1 = il.Add(images.Smiles.GetBitmap()) self.AssignImageList(il) # now put an image on the first tab we just created: self.SetPageImage(0, idx1) # Create and add the second tab tabTwo = TabPanel(self) self.AddPage(tabTwo, "TabTwo") # Create and add the third tab self.AddPage(TabPanel(self), "TabThree") self.Bind(wx.EVT_NOTEBOOK_PAGE_CHANGED, self.OnPageChanged) self.Bind(wx.EVT_NOTEBOOK_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, "Notebook Tutorial", size=(600,400) ) panel = wx.Panel(self) notebook = NotebookDemo(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() }}} Let’s break this down a bit. First, I create an instance of the wx.Notebook class and name it !NotebookDemo. In its “__init__”, I have some styles commented out. These styles tell the notebook where to place the tabs (i.e. on the top, left, right or bottom). The default is to place them on the top. You can comment out the default and uncomment one of the other lines to see the difference. To create the first tab, all we need to do is the following: {{{ #!python # Create the first tab and add it to the notebook tabOne = TabPanel(self) tabOne.SetBackgroundColour("Gray") self.AddPage(tabOne, "TabOne") }}} Actually, we don’t even need to set the background color, but I did that to make the text controls stand out a bit more. If we don’t set the color, we can make this into a one-liner like this: {{{ #!python self.AddPage(TabPanel(self), "TabOne") }}} The AddPage() method is the primary way to add a page to a notebook widget. This method has the following arguments: {{{ #!python AddPage(self, page, text, select, imageId) }}} I only add the page and the text for the tab. If you want, you can pass “True” as the 4th argument and make that tab be selected. The first tab will be selected by default though, so doing that would be kind of silly. The fifth argument allows the programmer to add an image to the tab; however, we do that with the next bit of code: {{{ #!python # Show how to put an image on one of the notebook tabs, # first make the image list: il = wx.ImageList(16, 16) idx1 = il.Add(images.Smiles.GetBitmap()) self.AssignImageList(il) # now put an image on the first tab we just created: self.SetPageImage(0, idx1) }}} As you can see, we first create an !ImageList and set the images to be 16×16. Then I use the “images” module from the wxPython Demo to create a smiley face and add that to the list. Next I assign the list to the notebook using !AssignImageList(). To set one of the images from the !ImageList on one of the tabs, you call !SetPageImage() and pass the tab index as the first argument and the bitmap !ImageList item instance as the second (i.e. idx1). In this example, I only add an image to the first tab. The next few lines of code adds two more tabs to the notebook and then binds a couple of events: EVT_NOTEBOOK_PAGE_CHANGING and EVT_NOTEBOOK_PAGE_CHANGED. The EVT_NOTEBOOK_PAGE_CHANGING is fired when the user has clicked on a different tab then the one currently selected and ends when the new tab is fully selected. When the next tab is fully selected is when the EVT_NOTEBOOK_PAGE_CHANGED event gets fired. One handy use for the EVT_NOTEBOOK_PAGE_CHANGING event is to veto the event should you need the user to do something before switching tabs. The [[http://www.wxpython.org/docs/api/wx.BookCtrlBase-class.html|documentation]] is your friend. You will find many handy methods there, such as !GetPage, !GetPageCount, !GetPageImage, !GetSelection, !RemovePage, and !DeletePage as well as many others. ==== Nesting Notebooks ==== The last topic I need to touch on for the wx.Notebook is how to nest the tabs. Nesting tabs is actually very easy to do in wxPython. All you need to do is create a panel with a notebook on it and then make that panel into a tab on another Notebook. It’s kind of hard to explain, so let’s look at some code instead: {{{ #!python import images import wx class PanelOne(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 NestedPanel(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) # Create some nested tabs on the first tab nestedNotebook = wx.Notebook(self, wx.ID_ANY) nestedTabOne = PanelOne(nestedNotebook) nestedTabTwo = PanelOne(nestedNotebook) nestedNotebook.AddPage(nestedTabOne, "NestedTabOne") nestedNotebook.AddPage(nestedTabTwo, "NestedTabTwo") sizer = wx.BoxSizer(wx.VERTICAL) sizer.Add(nestedNotebook, 1, wx.ALL|wx.EXPAND, 5) self.SetSizer(sizer) ######################################################################## class NestedNotebookDemo(wx.Notebook): """ Notebook class """ #---------------------------------------------------------------------- def __init__(self, parent): wx.Notebook.__init__(self, parent, id=wx.ID_ANY, style= wx.BK_DEFAULT #wx.BK_TOP #wx.BK_BOTTOM #wx.BK_LEFT #wx.BK_RIGHT ) # Create the first tab and add it to the notebook tabOne = NestedPanel(self) self.AddPage(tabOne, "TabOne") # Show how to put an image on one of the notebook tabs, # first make the image list: il = wx.ImageList(16, 16) idx1 = il.Add(images.Smiles.GetBitmap()) self.AssignImageList(il) # now put an image on the first tab we just created: self.SetPageImage(0, idx1) # Create and add the second tab tabTwo = PanelOne(self) self.AddPage(tabTwo, "TabTwo") # Create and add the third tab self.AddPage(PanelOne(self), "TabThree") self.Bind(wx.EVT_NOTEBOOK_PAGE_CHANGED, self.OnPageChanged) self.Bind(wx.EVT_NOTEBOOK_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, "Notebook Tutorial", size=(600,400) ) panel = wx.Panel(self) notebook = NestedNotebookDemo(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 basic gist of the code above is that you take a panel, but a notebook on it and then place that panel on the page of another notebook, thus getting the nested notebook. '''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.Notebook-class.html|wx.Notebook Documentation]] * [[http://wxpython.org/download.php|Official wxPython Demo]] * [[http://wiki.wxpython.org/Simple%20wx.Notebook%20Example|A Simple wx.Notebook Example]] * [[http://wiki.wxpython.org/Using%20a%20wxPython%20%27Notebook%27%20with%20panels|Using a wxPython 'Notebook' with panels]] * [[http://zetcode.com/wxpython/widgets/#notebook|Zetcode Notebook Example]] ''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]]''