wx.AuiNotebook

http://www.blog.pythonlibrary.org/wp-content/uploads/2009/12/auiNotebookDemo.png

The AuiNotebook is the only notebook control that does not inherit from the generic BookCtrlBase. Be sure to study this control’s API to fully understand how to work with it as the AuiNotebook has several methods that are unlike any of the other notebook controls. For example, the AuiNotebook allows the user to rearrange the tabs, drag the tab into its own area (as you can see from the screenshot above) or even drag the tab from one AuiNotebook to another. The following code snippet will allow you to do all these things:

   1 import wx
   2 import wx.aui
   3 
   4 ########################################################################
   5 class TabPanel(wx.Panel):
   6     """
   7     This will be the first notebook tab
   8     """
   9     #----------------------------------------------------------------------
  10     def __init__(self, parent):
  11         """"""
  12         
  13         wx.Panel.__init__(self, parent=parent, id=wx.ID_ANY)
  14         
  15         sizer = wx.BoxSizer(wx.VERTICAL)
  16         txtOne = wx.TextCtrl(self, wx.ID_ANY, "")
  17         txtTwo = wx.TextCtrl(self, wx.ID_ANY, "")
  18         
  19         sizer = wx.BoxSizer(wx.VERTICAL)
  20         sizer.Add(txtOne, 0, wx.ALL, 5)
  21         sizer.Add(txtTwo, 0, wx.ALL, 5)
  22         
  23         self.SetSizer(sizer)
  24  
  25 class DemoPanel(wx.Panel):
  26     """
  27     This will be the first notebook tab
  28     """
  29     #----------------------------------------------------------------------
  30     def __init__(self, parent):
  31         """"""
  32         wx.Panel.__init__(self, parent=parent, id=wx.ID_ANY)
  33  
  34         # create the AuiNotebook instance
  35         nb = wx.aui.AuiNotebook(self)
  36  
  37         # add some pages to the notebook
  38         pages = [(TabPanel(nb), "Tab 1"),
  39                  (TabPanel(nb), "Tab 2"),
  40                  (TabPanel(nb), "Tab 3")]
  41         for page, label in pages:
  42             nb.AddPage(page, label)
  43  
  44         sizer = wx.BoxSizer(wx.VERTICAL)
  45         sizer.Add(nb, 1, wx.EXPAND)
  46         self.SetSizer(sizer)
  47  
  48 ########################################################################
  49 class DemoFrame(wx.Frame):
  50     """
  51     Frame that holds all other widgets
  52     """
  53  
  54     #----------------------------------------------------------------------
  55     def __init__(self):
  56         """Constructor"""
  57         wx.Frame.__init__(self, None, wx.ID_ANY,
  58                           "AUI-Notebook Tutorial",
  59                           size=(600,400))
  60         panel = DemoPanel(self)
  61         self.Show()
  62  
  63 #----------------------------------------------------------------------
  64 if __name__ == "__main__":
  65     app = wx.PySimpleApp()
  66     frame = DemoFrame()
  67     app.MainLoop()

For this simple example, I just created an instance of AuiNotebook by instantiating against wx.aui.AuiNotebook(self) and passing in a wx.Panel. Then I added some pages using the AuiNotebook’s AddPage() method. Notice also that you can close any tab by clicking on its individual “x”. Now you know how to make your own AuiNotebook!

All code was tested on the following:

For Further Information

Note: The tutorial on this page is a modified version of the tutorial on my blog

AuiNotebook (last edited 2009-12-13 21:02:03 by 208)

NOTE: To edit pages in this wiki you must be a member of the TrustedEditorsGroup.