Python
  Home arrow Python arrow Page 4 - Organization in wxPython
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
Google.com  
PYTHON

Organization in wxPython
By: Peyton McCullough
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 13
    2005-06-29


    Table of Contents:
  • Organization in wxPython
  • Box Sizers
  • Box Sizers Continued
  • Grid Bag Sizers

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Organization in wxPython - Grid Bag Sizers
    ( Page 4 of 4 )

    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
     

       

    PYTHON ARTICLES

    - 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
    - Sequences and Sets in Python
    - Python Expressions and Operators
    - Dictionaries, Variables and Statements in Py...
    - Data Types in Python
    - The Python Language
    - SSH with Twisted





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek