Home arrow Python arrow Page 3 - Organization in wxPython

Box Sizers Continued - 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

Of course, what if we do choose to size it proportinally? Let's take a look:

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.button1 = wxButton ( self.panel, -1, 'Click Me' )
      self.button2 = wxButton ( self.panel, -1, 'Me as well.' )
      self.sizer = wxBoxSizer ( wxVERTICAL )
      self.sizer.Add ( self.text, 1  )
      self.sizer.Add ( self.button1, 4 )
      self.sizer.Add ( self.button2, 6 )
      self.panel.SetSizerAndFit ( self.sizer )
      self.SetClientSize ( self.panel.GetSize() )

      self.Show ( True )

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

Now let's play around with the third argument. Execute this application:

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.button1 = wxButton ( self.panel, -1, 'Click Me' )
      self.button2 = wxButton ( self.panel, -1, 'Me as well.' )
      self.sizer = wxBoxSizer ( wxVERTICAL )
      self.sizer.Add ( self.text, 1, wxGROW  )
      self.sizer.Add ( self.button1, 4, wxGROW )
      self.sizer.Add ( self.button2, 6, wxGROW )
      self.panel.SetSizerAndFit ( self.sizer )
      self.SetClientSize ( self.panel.GetSize() )

      self.Show ( True )

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

Resize the window a bit and notice the behavior of the controls. Let's now change wxGROW to wxSHAPED:

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.button1 = wxButton ( self.panel, -1, 'Click Me' )
      self.button2 = wxButton ( self.panel, -1, 'Me as well.' )
      self.sizer = wxBoxSizer ( wxVERTICAL )
      self.sizer.Add ( self.text, 1, wxSHAPED  )
      self.sizer.Add ( self.button1, 4, wxSHAPED )
      self.sizer.Add ( self.button2, 6, wxSHAPED )
      self.panel.SetSizerAndFit ( self.sizer )
      self.SetClientSize ( self.panel.GetSize() )

      self.Show ( True )

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

Again, resize the window and notice the behavior of the controls.

It is also possible to nest box sizers within one another. A good use for this would be centering a control or another sizer in a frame. First, we would create a vertical sizer and add a space with a proportion of 1. I'll show you how to add a space in the example. It's not hard at all. Next we would add a horizontal sizer to the vertical sizer. To the horizontal sizer we would add a space, our control and another space. Both spaces would have a proportion of 1, and the control would have a proportion of 0 ( unless you really wanted to resize it ). The horizontal sizer itself would have a proportion of 0. Finally, another space would be added to the vertical sizer. This may sound complicated, but it's very simple when put into code:

from wxPython.wx import *

class SizerFrame ( wxFrame ):

   def __init__ ( self ):

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

      self.panel = wxPanel ( self, -1 )
      self.horizontal = wxBoxSizer ( wxHORIZONTAL )
      self.horizontal.Add ( ( 0, 0 ), 1, wxEXPAND )
      self.horizontal.Add ( wxStaticText ( self.panel, wxID_ANY, 'Centered.' ) )
      self.horizontal.Add ( ( 0, 0 ), 1, wxEXPAND )
      self.vertical = wxBoxSizer ( wxVERTICAL )
      self.vertical.Add ( ( 0, 0 ), 1, wxEXPAND )
      self.vertical.Add ( self.horizontal, 0, wxALIGN_CENTER )
      self.vertical.Add ( ( 0, 0 ), 1, wxEXPAND )
      self.panel.SetSizerAndFit ( self.vertical )

      self.Show ( True )

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



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