wxPython - Notebook Class


Advertisements

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 −

S.N. Parameters & Description
1

wx.NB_TOP

Places tabs on the top side

2

wx.NB_LEFT

Places tabs on the left side

3

wx.NB_RIGHT

Places tabs on the right side

4

wx.NB_BOTTOM

Places tabs under the notebook pages instead of above the notebook pages

5

wx.NB_FIXEDWIDTH

All tabs will have the same width

6

wx.NB_MULTILINE

There can be several rows of tabs

Member functions of Notebook class −

S.N. Functions & Description
1

OnSelChange()

Handler function called when the page selection is changed

2

SetPadding()

Sets the amount space around each page's icon and label, in pixels

3

GetSelection()

Returns the currently selected page

4

SetSelection()

Sets the selection to the given page, returning the previous selection

5

AddPage()

Adds a new page

6

DeletePage()

Deletes a page of given index

7

InsertPage()

Inserts a new tab at the given index

8

RemovePage()

Removes the page without deleting

Event binders defined for this class are −

S.N. Events & Description
1

EVT_NOTEBOOK_PAGE_CHANGED()

The page selection was changed

2

EVT_NOTEBOOK_PAGE_CHANGING()

The page selection is about to be changed

Example

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 −

Notebook Demo Output
wxpython_major_classes.htm
Advertisements