Organization Methods Beyond Sizers (Page 1 of 5 )
A well-designed application will have its widgets organized neatly so that a user can easily access them. To accomplish this, we can use sizers to organize widgets, making them easier to locate and use. However, there are more methods of organization beyond sizers. This article will introduce you to methods of organization that work in conjunction with sizers and such, so you can organize your widgets even more, further benefitting those who use your applications.
Switching Panels
A simple way to organize widgets is to give the user the option to change the controls on the screen completely with a single click. This can be done by switching panels when the user presses a button on the current panel. Each panel would have its own widgets. Going this route isn't very hard. All that is necessary is to create several wxPanel subclasses with their own sets of widgets and then a wxFrame. Buttons for navigation would be placed on the panels. When a button is clicked, the panels would be quickly switched.
Let's take a big leap and put out the whole thing:
from wxPython.wx import *
# Create the frame
class Window ( wxFrame ):
def __init__ ( self ):
wxFrame.__init__ ( self, None, -1, 'Organized Application', size = ( 250, 250 ) )
# Create an empty variable to house our panel
self.panel = None
self.Show ( True )
# Create the methods that switch the panels
def goPanel1 ( self, event ):
self.panel = instance1
instance1.Show ( True )
instance2.Show ( False )
instance3.Show ( False )
def goPanel2 ( self, event ):
self.panel = instance2
instance1.Show ( False )
instance2.Show ( True )
instance3.Show ( False )
def goPanel3 ( self, event ):
self.panel = instance3
instance1.Show ( False )
instance2.Show ( False )
instance3.Show ( True )
# Create one panel that uses absolute positioning
class Panel1 ( wxPanel ):
def __init__ ( self, parent ):
wxPanel.__init__ ( self, parent, -1 )
# Create some navigation buttons
self.button2 = wxButton ( self, 2, 'Panel 2', pos = ( 5, 5 ) )
self.button3 = wxButton ( self, 3, 'Panel 3', pos = ( 50, 35 ) )
# Hook up the navigation events
EVT_BUTTON ( self, 2, parent.goPanel2 )
EVT_BUTTON ( self, 3, parent.goPanel3 )
# Set the size of the panel to match the frame
self.SetSize ( parent.GetClientSize() )
# Do not make the panel visible at first
self.Show ( False )
# Create one panel that uses a horizontal box sizer
class Panel2 ( wxPanel ):
def __init__ ( self, parent ):
wxPanel.__init__ ( self, parent, -1 )
# Create sizer
self.sizer = wxBoxSizer ( wxHORIZONTAL )
# Create some navigation buttons
self.button1 = wxButton ( self, 1, 'Panel 1' )
self.button3 = wxButton ( self, 3, 'Panel 3' )
# Add the navigation buttons
self.sizer.Add ( self.button1 )
self.sizer.Add ( self.button3 )
# Hook up the navigation events
EVT_BUTTON ( self, 1, parent.goPanel1 )
EVT_BUTTON ( self, 3, parent.goPanel3 )
# Resize the sizer and add it
self.sizer.SetMinSize ( parent.GetClientSize() )
self.SetSizerAndFit ( self.sizer )
# Do not make the panel visible at first
self.Show ( False )
# Create one panel that uses a vertical box sizer
class Panel3 ( wxPanel ):
def __init__ ( self, parent ):
wxPanel.__init__ ( self, parent, -1 )
# Create sizer
self.sizer = wxBoxSizer ( wxVERTICAL )
# Create some navigation buttons
self.button1 = wxButton ( self, 1, 'Panel 1' )
self.button2 = wxButton ( self, 2, 'Panel 2' )
# Add the navigation buttons
self.sizer.Add ( self.button1 )
self.sizer.Add ( self.button2 )
# Hook up the navigation events
EVT_BUTTON ( self, 1, parent.goPanel1 )
EVT_BUTTON ( self, 2, parent.goPanel2 )
# Resize the sizer and add it
self.sizer.SetMinSize ( parent.GetClientSize() )
self.SetSizerAndFit ( self.sizer )
# Do not make the panel visible at first
self.Show ( False )
application = wxPySimpleApp()
frame = Window()
# Create the panels
instance1 = Panel1 ( frame )
instance2 = Panel2 ( frame )
instance3 = Panel3 ( frame )
# Set the default panel
frame.goPanel1 ( None )
application.MainLoop()
First, we create our main frame class with an empty variable, panel. We then create three different panel classes, and create instances of everything. When the user pushes a button, the corresponding method is called, and the panel is switched. It's nothing new or complex; it's just a bit long.
Next: Using Tabs for Navigation >>
More Python Articles
More By Peyton McCullough