Organization in wxPython - Box Sizers Continued
(Page 3 of 4 )
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()
Next: Grid Bag Sizers >>
More Python Articles
More By Peyton McCullough