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