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

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    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

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





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