Home arrow Python arrow Page 5 - Organization Methods Beyond Sizers

Boxing in Controls - Python

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

Sometimes, it is helpful to group related controls together in a box. This can be done with the wxStaticBox widget very easily:

from wxPython.wx import *

class Window ( wxFrame ):

   def __init__ ( self ):

      wxFrame.__init__ ( self, None, -1, 'Static Box', size = ( 300, 300 ) )

      # Create a panel to house everything

      self.panel = wxPanel ( self, -1 )

      # Create the wxStaticBox

      self.box = wxStaticBox ( self.panel, -1, 'Just a Box', size = ( 200, 200 ), pos = ( 5, 5 ) )

      # Create a button

      self.button = wxButton ( self.panel, -1, 'Just a button', pos = ( 25, 25 ) )

      self.Show ( True )

application = wxPySimpleApp()

Window()

application.MainLoop()

One thing to note here is that the wxStaticBox is not the parent of our wxButton. If you set it as the parent of anything, your application will crash, and that's not a key ingredient for a healthy application.

If we want to organize controls in a wxStaticBox, we can use wxStaticBoxSizer, which works like wxBoxSizer. This can be hooked up to our wxStaticBox. We'll add another button to our box, though, and since this method is not too friendly with absolute positioning, we'll use wxBoxSizer. We'll create two of these to center everything:

from wxPython.wx import *

class Window ( wxFrame ):

   def __init__ ( self ):

      wxFrame.__init__ ( self, None, -1, 'Static Box', size = ( 300, 300 ) )

      # Create a panel to house everything

      self.panel = wxPanel ( self, -1 )

      # Create vertical box sizer

      self.sizer = wxBoxSizer ( wxVERTICAL )

      # Create a horizontal box sizer -- we're going to center everything

      self.horizontal = wxBoxSizer ( wxHORIZONTAL )

      # Create the wxStaticBox

      self.box = wxStaticBox ( self.panel, -1, 'Just a Box' )

      # Create our wxStaticBoxSizer

      self.boxSizer = wxStaticBoxSizer ( self.box, wxHORIZONTAL )

      # Create some buttons

      self.button1 = wxButton ( self.panel, -1, 'Just a button' )

      self.button2 = wxButton ( self.panel, -1, 'Another.' )

      # Add the buttons to the sizer

      self.boxSizer.Add ( self.button1 )

      self.boxSizer.Add ( self.button2 )

      # Configure the box sizers

      self.horizontal.Add ( ( 0, 0 ), 1, wxEXPAND )

      self.horizontal.Add ( self.boxSizer )

      self.horizontal.Add ( ( 0, 0 ), 1, wxEXPAND )

      self.sizer.Add ( ( 0, 0 ), 1, wxEXPAND )

      self.sizer.Add ( self.horizontal, 0, wxALIGN_CENTER )

      self.sizer.Add ( ( 0, 0 ), 1, wxEXPAND )

      # Attach everything

      self.panel.SetSizerAndFit ( self.sizer )

      self.Show ( True )

application = wxPySimpleApp()

Window()

application.MainLoop()

Conclusion

This article has shown you that, beyond using simple sizers, there are additional ways to organize the controls in your application, giving the user more control over what he or she sees, which makes him or her appreciate your application more. Organization can be done in a variety of ways, including the ones that this article has outlined: switching panels, using tabs, using a list, using a drop-down list and using a wxStaticBox to organize closely related widgets in your application. Ease-of-use must be considered in any application, and this article has given you some tools to use in the process.



 
 
>>> 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: