wxPython - Notebook Class

wxPython class library contains various ‘book’ control. A book control allows the user to switch between various panels in a frame. This is especially useful when a large amount of data is to be presented.
Book classes are inherited from wx.BookCtrlBase class. The following type of book controls are available −
wx.Notebook
wx.Choicebook
wx.Listbook
wx.Treebook
wx.Notebook widget presents a tabbed control. One Notebook object in a frame has one or more tabs (called Pages), each of them having a panel showing the layout of controls. The user can switch between pages by clicking on the respective tab title.
Notebook class constructor takes familiar parameters as the following −
wx.Notebook(parent, id, pos, size, style)

The following style parameters are available for customization of the widget −
wx.NB_TOP
Places tabs on the top side
wx.NB_LEFT
Places tabs on the left side
wx.NB_RIGHT
Places tabs on the right side
wx.NB_BOTTOM
Places tabs under the notebook pages instead of above the notebook pages
wx.NB_FIXEDWIDTH
All tabs will have the same width
wx.NB_MULTILINE
There can be several rows of tabs
Member functions of Notebook class −
OnSelChange()
Handler function called when the page selection is changed
SetPadding()
Sets the amount space around each page's icon and label, in pixels
GetSelection()
Returns the currently selected page
SetSelection()
Sets the selection to the given page, returning the previous selection
AddPage()
Adds a new page
DeletePage()
Deletes a page of given index
InsertPage()
Inserts a new tab at the given index
RemovePage()
Removes the page without deleting
Event binders defined for this class are −
EVT_NOTEBOOK_PAGE_CHANGED()
The page selection was changed
EVT_NOTEBOOK_PAGE_CHANGING()
The page selection is about to be changed
The following example deploys a Notebook control in a top level frame.
nb = wx.Notebook(self) 

Two classes based on wx.Panel are designed. The first, puts a multi-line TextCtrl in it.
class MyPanel1(wx.Panel): 
   def __init__(self, parent): 
      super(MyPanel1, self).__init__(parent) 
      text = wx.TextCtrl(self, style = wx.TE_MULTILINE, size = (250,150))
The second, shows a RadioBox having three radio buttons.
class MyPanel2(wx.Panel): 
   def __init__(self, parent): 
      super(MyPanel2, self).__init__(parent) 
      lblList = ['Value X', 'Value Y', 'Value Z']         
      rbox = wx.RadioBox(self, label = 'RadioBox', pos = (25,10), choices = lblList,
         majorDimension = 1, style = wx.RA_SPECIFY_ROWS)
Objects of these two panel classes are added as pages in Notebook on the top level frame.
nb.AddPage(MyPanel1(nb),"Editor") 
nb.AddPage(MyPanel2(nb),"RadioButtons")

The complete code is as follows −
import wx
  
class MyDialog(wx.Dialog): 
   def __init__(self, parent, title): 
      super(MyDialog, self).__init__(parent, title = title, size = (250,150)) 
      panel = wx.Panel(self) 
      self.btn = wx.Button(panel, wx.ID_OK, label = "ok", size = (50,20), pos = (75,50)) 
     
class Mywin(wx.Frame): 
            
   def __init__(self, parent, title): 
      super(Mywin, self).__init__(parent, title = title, size = (250,150))  
      self.InitUI() 
         
   def InitUI(self):    
      nb = wx.Notebook(self) 
      nb.AddPage(MyPanel1(nb),"Editor") 
      nb.AddPage(MyPanel2(nb),"RadioButtons") 
      self.Centre() 
      self.Show(True) 
		
class MyPanel1(wx.Panel): 
   def __init__(self, parent): 
      super(MyPanel1, self).__init__(parent) 
      text = wx.TextCtrl(self, style = wx.TE_MULTILINE, size = (250,150)) 
		
class MyPanel2(wx.Panel): 
   def __init__(self, parent): 
      super(MyPanel2, self).__init__(parent) 
      lblList = ['Value X', 'Value Y', 'Value Z']         
      rbox = wx.RadioBox(self, label = 'RadioBox', pos = (25,10), choices = lblList,
         majorDimension = 1, style = wx.RA_SPECIFY_ROWS) 
     
ex = wx.App() 
Mywin(None,'NoteBook demo') 
ex.MainLoop()
The above code produces the following output −
 Print
 Add Notes
 Bookmark this page