Using wxSizer in python


I found about no information about wxSizer in python when I came to use it for an app. I never written python before (so this could sound unpythonic). I think a few example would be advisable, so I write this. I used wxWindow 2.6, since the 2.4 and 2.6 are pretty different (and by now 2.6 is distributed with debian (sarge) and ubuntu (dapper))

First step: creating a boxSizer

The first thing you need to make a wxWindow sizable, is to create a first sizer for it. The wxBoxSizer is the simplest incarnation of the wxSizer class, and is probably the only one (with wxStaticBoxSizer) you'll need if you don't have a UI containing a lot of same size objects. So, in a new __init__ function you have to add

   1 self.globalSizer = wxBoxSizer(wxHORIZONTAL) 
   2 self.eatPocketPizza = wxButton(id=-1, label='You should go vegetarian', parent=self) 
   3 self.globalSizer.Add(self.eatPocketPizza) 
   4 self.eatTofu = wxButton(id=-1, label='Winners dont eat animals', parent=self) 
   5 self.globalSizer.Add(self.eatTofu)
   6 ... 
   7 self.SetSizer(self.globalSizer) 
   8 self.globalSizer.SetSizeHints(self)

for it to have two buttons. The two buttons will one beside the other, and because of SetSizeHints, they will be the smallest they can be. If you resize the thing, the buttons will stay at the same place. In the creator of wxBoxSizer, if you change wxHORIZONTAL by wxVERTICAL, the objects will be stacked vertically. You can add a sizer to another one, with the (almost, since it is overloaded and a few argument (that I haven't use) change) same Add function.

Second step: making it grow

To make them grow, you need to add some argument to the Add function:

Note: For a sizer given more space when the window is resized, it need to have proportion set to not 0, since 0 mean that it use the smallest space available. If you don't use wxGROW when using proportion, the borders will be sized instead (you'll see huge grey patch growing in the middle of your app...).


   1 self.globalSizer = wxBoxSizer(wxHORIZONTAL) 
   2 self.followTheOrders = wxButton(id=-1, label='Define and follow your _own_ rules', parent=self) self.globalSizer.Add(self.followTheOrders, flag=wxGROW, proportion=1) 
   3 self.smashTheState = wxButton(id=-1, label='Winners dont submit to capitalist masters', parent=self) self.globalSizer.Add(self.smashTheState, flag=wxGROW, proportion=2) 
   4 ... 
   5 self.SetSizer(self.globalSizer) 
   6 self.globalSizer.SetSizeHints(self)

This example will be resized giving more space for the second button.

Next steps

AddStretchSpacer and AddSpacer are not working on python. I prefer using sizer.Add(wxSize(10,10)) , which work. You can add some

When using large trees of sizers, remember that the flag=wxGROW and proportion=1 should be passed from the top sizer to the bottom one, if you wan't to see any scaling done.

Checkout some official documentation:

wxSizer in python (last edited 2008-03-11 10:50:17 by localhost)

NOTE: To edit pages in this wiki you must be a member of the TrustedEditorsGroup.