This chapter covers
■ Understanding sizers
■ Spacing widgets in sizers
■ Using the grid family of sizers
■ Using box sizers
■ Looking at sizers in action
Traditionally, one of the most vexing problems in programming a UI is managing the physical layout of the widgets within the window. In the beginning, there was absolute positioning, and the programmer would explicitly set the size and position of each widget on the screen. This is—to put it mildly—tedious, not to mention a pain in the neck. Even worse, absolute positioning only works if you have absolute control over the window size and number of widgets. If you allow the user to do something wild and crazy like make his text-entry widget bigger by resizing the window, you need to capture the window-resize event, then explicitly change the size and position of each widget. While you're doing that, make sure that the widgets still look good and don't overlap or fall off the edge of the window. This is even more of a pain in the neck and it gets worse if the user's interaction normally changes the number and type of widgets in the display. And, of course, if you or your client decide you want a different interface look, you have to go through the whole process all over again.
What you need is a structure that decides how to resize and move the widgets based on a predetermined pattern. There have been several solutions proposed to this problem. There is even a deprecated mechanism or two in the wxPython system. The recommended way to deal with complicated layout these days is by using a sizer. A sizer is an automated algorithm for laying out a group of widgets. A sizer is attached to a container, usually a frame or panel. Subwidgets that are created within a parent container must be separately added to the sizer. When the sizer is attached to the container, it then manages the layout of the children contained inside it.
The advantages of using a sizer are substantial. The sizer will automatically respond by recalculating the layout of its children when its container's size is changed. Similarly, if one of the children changes size, the sizer can automatically refresh the layout. Furthermore, sizers are easy to manage when you want to change the layout. The biggest downside is that the layouts sizers enforce can be somewhat restrictive. However, the most flexible sizers, the grid bag and box, will be able to do nearly everything you'll want them to.
Was this article helpful?