10.2. The Alignment widget

The Alignment widget allows you to place a widget within its window at a position and size relative to the size of the Alignment widget itself. For example, it can be very useful for centering a widget within the window.

There are only two calls associated with the Alignment widget:

  alignment = gtk.Alignment(xalign=0.0, yalign=0.0, xscale=0.0, yscale=0.0)

  alignment.set(xalign, yalign, xscale, yscale)

The gtk.Alignment() function creates a new Alignment widget with the specified parameters. The set() method allows the alignment parameters of an existing Alignment widget to be altered.

All four alignment parameters are floating point numbers which can range from 0.0 to 1.0. The xalign and yalign arguments affect the position of the widget placed within the gtk.Alignment widget. The align properties specify the fraction of free space that will be placed above or to the left of the child widget. The values range from 0.0 (no free space above or to the left of the child) to 1.0 (all free space above or to the left of the child). Of course, if the scale properties are both set to 1.0, the alignment properties have no effect since the child widget will expand to fill the available space.

The xscale and yscale arguments specify the fraction of free space absorbed by the child widget. The values can range from 0.0 (meaning the child absorbs none) to 1.0 (meaning the child absorbs all of the free space).

A child widget can be added to this Alignment widget using:


For an example of using an Alignment widget, refer to the progressbar.py example for the Progress Bar widget.