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  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Actuate Whitepapers 
VeriSign Whitepapers 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
SunQuest
 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? 
PYTHON

Organization in wxPython
By: Peyton McCullough
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 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:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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

    AT&T devCentral & BlackBerry(r) Webcast Series: BlackBerry and GPS -Build Location Awareness into your BlackBerry Applications, July 10th -1:00PM EST. Register Today!

    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.


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

     

       

    PYTHON ARTICLES

    - SSH with Twisted
    - Mobile Programming in Python using PyS60: UI...
    - Python: Count on It
    - Python Strings: Spinning Yarns
    - Python: More Fun with Strings
    - Python: Stringing You Along
    - Python Operators
    - Bluetooth Programming in Python: Network Pro...
    - Python Sets
    - Python Conditionals, Lists, Dictionaries, an...
    - Python: Input and Variables
    - Introduction to Python Programming
    - Mobile Programming in Python using PyS60: Ge...
    - Bluetooth Programming using Python
    - Finishing the PyMailGUI Client: User Help To...

    Click Here




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway