Instead of using tabs to switch between panels, the option to use a list is available, and it works in almost the exact same way as tabs do (as far as scripting it goes). This is done by using the wxListbook widget. This method is useful if you have a large number of panels in your application. The wxNotebook widget and the wxListbook widget are so alike in creation that we can just recycle our wxNotebook script, replacing “Notebook” with “Listbook”:
from wxPython.wx import *
# Create the main window
class Window ( wxFrame ):
def __init__ ( self ):
wxFrame.__init__ ( self, None, -1, 'Frame title.' )
# Create a wxListbook
self.listbook = wxListbook ( self, -1 )
self.Show ( True )
# Create a panel
class Panel1 ( wxPanel ):
def __init__ ( self, parent ):
wxPanel.__init__ ( self, parent, -1 )
# Put a label in the panel
self.label = wxStaticText ( self, -1, 'This is the first panel.', pos = ( 25, 25 ) )
# Create another panel
class Panel2 ( wxPanel ):
def __init__ ( self, parent ):
wxPanel.__init__ ( self, parent, -1 )
# Add a few buttons
self.button1 = wxButton ( self, -1, 'This is a button.', pos = ( 5, 5 ) )
self.button2 = wxButton ( self, -1, 'So is this.', pos = ( 30, 30 ) )
self.button3 = wxButton ( self, -1, 'And this.', pos = ( 25, 60 ) )
# Create yet another panel
class Panel3 ( wxPanel ):
def __init__ ( self, parent ):
wxPanel.__init__ ( self, parent, -1 )
# Add a few text boxes
self.text1 = wxTextCtrl ( self, -1, pos = ( 5, 5 ) )
self.text2 = wxTextCtrl ( self, -1, pos = ( 5, 50 ), style = wxTE_MULTILINE )
self.text3 = wxTextCtrl ( self, -1, pos = ( 30, 25 ), style = wxTE_PASSWORD )
application = wxPySimpleApp()
frame = Window()
# Create the panels
instance1 = Panel1 ( frame.listbook )
instance2 = Panel2 ( frame.listbook )
instance3 = Panel3 ( frame.listbook )
# Add the panels
frame.listbook.AddPage ( instance1, 'Panel One' )
frame.listbook.AddPage ( instance2, 'Panel Two' )
frame.listbook.AddPage ( instance3, 'Panel Three' )
application.MainLoop()
When the user clicks an item on the list, the corresponding panel is displayed on the screen. It is possible to put the list on any side of the main window by applying the styles wxLB_TOP, wxLB_BOTTOM, wxLB_RIGHT and wxLB_LEFT.