wxPython: How to Programmatically Change wx.Notebook Pages

Occasionally I’ll see someone on the wxPython users group ask about how to make the wx.Notebook change pages (or tabs) programmatically. So I decided it was about time I figured it out. Here is some code that works for me:

import random
import wx
 
########################################################################
class TabPanel(wx.Panel):
    #----------------------------------------------------------------------
    def __init__(self, parent, page):
        """"""
        wx.Panel.__init__(self, parent=parent)
        self.page = page
 
        colors = ["red", "blue", "gray", "yellow", "green"]
        self.SetBackgroundColour(random.choice(colors))
 
        btn = wx.Button(self, label="Change Selection")
        btn.Bind(wx.EVT_BUTTON, self.onChangeSelection)
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(btn, 0, wx.ALL, 10)
        self.SetSizer(sizer)
        
    #----------------------------------------------------------------------
    def onChangeSelection(self, event):
        """
        Change the page!
        """
        notebook = self.GetParent()
        notebook.SetSelection(self.page)
 
########################################################################
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 = wx.Notebook(panel)
        tabOne = TabPanel(notebook, 1)
        notebook.AddPage(tabOne, "Tab 1")
 
        tabTwo = TabPanel(notebook, 0)
        notebook.AddPage(tabTwo, "Tab 2")
 
        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.App(False)
    frame = DemoFrame()
    app.MainLoop()

The main thing to know is that you need to use SetSelection (or ChangeSelection) to force the Notebook widget to change pages. That’s it! This code was tested on Windows 7 with Python 2.7.3 and wxPython 2.9.3.1 (Classic). See also this discussion on Nabble.