Home arrow Python arrow Organization Methods Beyond Sizers

Organization Methods Beyond Sizers

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.

TABLE OF CONTENTS:
  1. Organization Methods Beyond Sizers
  2. Using Tabs for Navigation
  3. Using a List for Navigation
  4. Using a Drop-Down List for Navigation
  5. Boxing in Controls
By: Peyton McCullough
Rating: starstarstarstarstar / 6
August 15, 2005

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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. 



 
 
>>> More Python Articles          >>> More By Peyton McCullough
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PYTHON ARTICLES

- Python Big Data Company Gets DARPA Funding
- Python 32 Now Available
- Final Alpha for Python 3.2 is Released
- Python 3.1: String Formatting
- Python 3.1: Strings and Quotes
- Python 3.1: Programming Basics and Strings
- Tuples and Other Python Object Types
- The Dictionary Python Object Type
- String and List Python Object Types
- Introducing Python Object Types
- Mobile Programming using PyS60: Advanced UI ...
- Nested Functions in Python
- Python Parameters, Functions and Arguments
- Python Statements and Functions
- Statements and Iterators in Python

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: