Home arrow Python arrow Page 4 - Organization in wxPython

Grid Bag Sizers - Python

While absolute positioning of controls may work in some odd cases, absolute positioning isn't convenient or pretty. This article will explain methods that can be used to better oganize controls in your wxPython aplications.

TABLE OF CONTENTS:
  1. Organization in wxPython
  2. Box Sizers
  3. Box Sizers Continued
  4. Grid Bag Sizers
By: Peyton McCullough
Rating: starstarstarstarstar / 14
June 29, 2005

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

While box sizers are nice to work with, grid bag sizers are also nice. They allow controls to be put in a grid. For example, say we wanted four buttons arranged in a square. This can be done with a grid bag sizer. Let's take a look at how it is done:

from wxPython.wx import *

class SizerFrame ( wxFrame ):

   def __init__ ( self ):

      wxFrame.__init__ ( self, None, -1, 'Sizers' )

      self.panel = wxPanel ( self, -1 )
      self.sizer = wxGridBagSizer ( 2, 2 )
      self.sizer.Add ( wxButton ( self.panel, wxID_ANY, 'Button 1' ), ( 0, 0 ) )
      self.sizer.Add ( wxButton ( self.panel, wxID_ANY, 'Button 2' ), ( 0, 1 ) )
      self.sizer.Add ( wxButton ( self.panel, wxID_ANY, 'Button 3' ), ( 1, 0 ) )
      self.sizer.Add ( wxButton ( self.panel, wxID_ANY, 'Button 7' ), ( 1, 1 ) )
      self.panel.SetSizerAndFit ( self.sizer )
      self.SetClientSize ( self.panel.GetSize() )
      self.Show ( True )

application = wxPySimpleApp()
window = SizerFrame()
application.MainLoop()

As you can see, it's not very hard. Let's manipulate our grid a bit. Let's widen the horizontal border to ten pixels instead of two and make the buttons expandable. Let's also get rid of the second button and make the first button take up the whole row:

from wxPython.wx import *

class SizerFrame ( wxFrame ):

   def __init__ ( self ):

      wxFrame.__init__ ( self, None, -1, 'Sizers' )

      self.panel = wxPanel ( self, -1 )
      self.sizer = wxGridBagSizer ( 2, 10 )
      self.sizer.Add ( wxButton ( self.panel, wxID_ANY, 'Button 1' ), ( 0, 0 ), ( 1, 2 ), wxEXPAND )
      self.sizer.Add ( wxButton ( self.panel, wxID_ANY, 'Button 3' ), ( 1, 0 ), ( 1, 1 ) )
      self.sizer.Add ( wxButton ( self.panel, wxID_ANY, 'Button 7' ), ( 1, 1 ), ( 1, 1 ) )
      self.panel.SetSizerAndFit ( self.sizer )
      self.SetClientSize ( self.panel.GetSize() )

      self.Show ( True )

application = wxPySimpleApp()
window = SizerFrame()
application.MainLoop()

Notice that every cell in the grid had the same size. We can change that using a flexible grid sizer:

from wxPython.wx import *

class SizerFrame ( wxFrame ):

   def __init__ ( self ):

      wxFrame.__init__ ( self, None, -1, 'Sizers' )

      self.panel = wxPanel ( self, -1 )
      self.sizer = wxFlexGridSizer ( 2, 2, 2, 2 ) # rows, columns, vertical gap, horizontal gap
      self.sizer.Add ( wxButton ( self.panel, wxID_ANY, 'Button 1', size = ( 50, 50 ) ), 0, wxEXPAND )
      self.sizer.Add ( wxButton ( self.panel, wxID_ANY, 'Button 2' ), 0, wxEXPAND )
      self.sizer.Add ( wxButton ( self.panel, wxID_ANY, 'Button 3' ), 0, wxEXPAND )
      self.sizer.Add ( wxButton ( self.panel, wxID_ANY, 'Button 7' ), 0, wxEXPAND )
      self.sizer.AddGrowableRow ( 0 )
      self.sizer.AddGrowableCol ( 0 )
      self.panel.SetSizerAndFit ( self.sizer )
      self.SetClientSize ( self.panel.GetSize() )

      self.Show ( True )

application = wxPySimpleApp()
window = SizerFrame()
application.MainLoop()

Conclusion

That's it. You should now know enough about sizers to create organized applications instead of applications featuring controls thrown around the frame. Sizers make a lot more sense than absolute positioning and eliminate a lot of the work involved in the absolute positioning of controls. You also know a few more controls, enough to do a few small applications.



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