Python
  Home arrow Python arrow Page 2 - 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 - Box Sizers
    ( Page 2 of 4 )

    Now let's organize things. We'll take a look at sizers throughout this tutorial. Sizers are simply devices that let you organize widgets. The most basic type of sizer is wxBoxSizer. It lets you organize widgets horizontally and vertically. It is also possible to nest sizers, as I'll explain in a bit. First, let me show you the syntax. Here is how you would create  a horizontal wxBoxSizer:

    wxBoxSizer ( wxHORIZONTAL )

    Here's how you create a vertical wxBoxSizer:

    wxBoxSizer ( wxVERTICAL )

    Of course, now we have to do something with our sizer. Let's dive right in to some code:

    from wxPython.wx import *

    class SizerFrame ( wxFrame ):

       def __init__ ( self ):

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

          self.panel = wxPanel ( self, -1 )
          self.text = wxStaticText ( self.panel, -1, 'Just some text. That is all. I promise.' )
          self.button = wxButton ( self.panel, -1, 'Click Me' )
          self.sizer = wxBoxSizer ( wxVERTICAL )
          self.sizer.Add ( self.text )
          self.sizer.Add ( self.button )
          self.panel.SetSizerAndFit ( self.sizer )
          self.SetClientSize ( self.panel.GetSize() )

          self.Show ( True )

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

    All right. This should be bite-size, but let's tear it apart just in case there's anything you don't completely understand. We start our application as normal. We import everything we need and create a frame. We then add a panel and two controls. Notice that the panel is the parent of both of the controls. Next, we create a vertical wxBoxSizer. This organizes our conrols vertically. We then add our two controls and adjust the size of both the panel and the frame to match the sizes of the controls. Finally, we show our frame to the user.

    Creating a horizontal sizer is done the same way, with the exception of one variable:

    from wxPython.wx import *

    class SizerFrame ( wxFrame ):

       def __init__ ( self ):

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

          self.panel = wxPanel ( self, -1 )
          self.text = wxStaticText ( self.panel, -1, 'Just some text. That is all. I promise.' )
          self.button = wxButton ( self.panel, -1, 'Click Me' )
          self.sizer = wxBoxSizer ( wxHORIZONTAL )
          self.sizer.Add ( self.text )
          self.sizer.Add ( self.button )
          self.panel.SetSizerAndFit ( self.sizer )
          self.SetClientSize ( self.panel.GetSize() )

          self.Show ( True )

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

    Neither looks especially pretty, especially the horizontal one. Let's modify the vertical one a bit more and align the button in the center.

    from wxPython.wx import *

    class SizerFrame ( wxFrame ):

       def __init__ ( self ):

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

          self.panel = wxPanel ( self, -1 )
          self.text = wxStaticText ( self.panel, -1, 'Just some text. That is all. I promise.' )
          self.button = wxButton ( self.panel, -1, 'Click Me' )
          self.sizer = wxBoxSizer ( wxVERTICAL )
          self.sizer.Add ( self.text )
          self.sizer.Add ( self.button, 0, wxALIGN_CENTER )
          self.panel.SetSizerAndFit ( self.sizer )
          self.SetClientSize ( self.panel.GetSize() )

          self.Show ( True )

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

    Notice how we pass three arguments to the Add method. The first, of course, is the control we want to add to the sizer. There's nothing new there. The second is how the control should look in proportion to other controls. For example, if you want one control to be twice as big as another, pass 2 and 1, respectively. We pass 0, since we don't want to alter the size of our button. The third and final argument can vary. Since we pass 0, we have the choice of aligning our control any way we want it. We choose to align it in the center. Here are other options:

    wxALIGN_LEFT
    wxALIGN_RIGHT
    wxALIGN_TOP
    wxALIGN_BOTTOM
    wxALIGN_CENTER_VERTICAL
    wxALIGN_CENTER_HORIZONTAL



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