Whats a sizer

A wxPython sizer is an object whose sole purpose is to manage the layout of a set of widgets within a container. The sizer is not a container or a widget itself. It is just the representation of an algorithm for laying out a screen. All sizers are instances of a subclass of the abstract class wx.Sizer. There are five sizers provided with wxPython, defined in table 11.1. Remember, sizers can be placed inside other sizers to give you even more flexibility.

Table 11.1 The predefined sizers in wxPython

Sizer Type



A very basic grid layout. Best used when the widgets you are placing are all exactly the same size and neatly fall into a regular grid.

Flex grid

A slight change from the grid sizer, allowing better results when the widgets are different sizes.

Grid bag

The most flexible member of the grid sizer family, allowing for more arbitrary placement of widgets in the grid. Useful for layouts where the display can be thought of as an irregular grid, with perhaps some items that take up more than one grid square.


Either a horizontal or vertical box with widgets laid out in a line. Very flexible in controlling widget behavior when resized. Generally used in a nested fashion. Useful for nearly any kind of layout, although figuring out exactly how to nest the boxes can be tricky.

Static box

A standard box sizer with a line and a title around it.

If you want your layout to be grid- or box-like, wxPython can definitely accommodate; in practice almost any useful layout can be imagined as either a grid or a set of boxes.

All sizers know the minimal size of each of their children. Typically, the sizer also allows for additional information about the layout, such as how much space to place between widgets, how much it can increase the size of a widget to fill space, and how to align widgets when they are smaller than their allotted space. From these bits of information the sizer uses its layout algorithm to determine the size and position of each child. Each kind of sizer in wxPython will produce a different final layout from the same group of child widgets. You can see this throughout this chapter, as we use very similar layouts to demonstrate each sizer type.

There are three basic steps you take to use a sizer:

1 Add the sizer to a container. Connect the sizer to the widget whose children it is managing. A sizer is added to the container widget using the method SetSizer(sizer) of wx.Window. Since this is a method of wx.Window, this means that any wxPython widget can have a sizer, although a sizer is only meaningful for widgets which are containers.

2 Add each child widget to the sizer. All of the child widgets need to be separately added to the sizer. Merely creating child widgets with the container as a parent is not enough. The primary method for adding a widget to a sizer is, appropriately enough, Add(). The Add() method has a couple of different signatures, which will be discussed in the next section.

3 (optional) Enable the sizer to calculate its size. Tell the sizer to calculate its size based on its children by calling either the wx.Window method Fit() on the parent window object or the Fit(window) method of the sizer. (The window method redirects to the sizer method.) In either case, the Fit() method asks the sizer to calculate its size based on what it knows about its children, and it resizes the parent widget to fit. There is a related method, FitInside(), which does not change the display size of the parent widget, but does change its virtual size—meaning that if the widget is inside a scrolled panel, wxPython would recalculate whether or not scroll bars are needed.

At this point we need to discuss both the behavior of specific sizers as well as the behavior common to all sizers. There's always a question whether to present the general or specific information first. Our solution is to start by presenting the grid sizer, which is the easiest to understand. After that, we'll discuss behavior common to all sizers, using the grid sizer as an example. (It helps that most of the common behaviors are very easy to visualize using the grid sizer as an example.) After that, we'll show the rest of the specific sizer types.

Was this article helpful?

0 0

Post a comment