4.4. Packing Using Tables

Let's take a look at another way of packing - Tables. These can be extremely useful in certain situations.

Using tables, we create a grid that we can place widgets in. The widgets may take up as many spaces as we specify.

The first thing to look at, of course, is the GtkTable() function:
table = GtkTable(rows, columns, homogeneous)

The first argument is the number of rows to make in the table, while the second, obviously, is the number of columns.

The homogeneous argument has to do with how the table's boxes are sized. If homogeneous is TRUE, the table boxes are resized to the size of the largest widget in the table. If homogeneous is FALSE, the size of a table boxes is dictated by the tallest widget in its same row, and the widest widget in its column.

The rows and columns are laid out from 0 to n, where n was the number specified in the call to GtkTable(). So, if you specify rows = 2 and columns = 2, the layout would look something like this:
 0          1          2
 |          |          |
 |          |          |

Note that the coordinate system starts in the upper left hand corner. To place a widget into a box, use the following method:
table.attach(child, left_attach, right_attach, top_attach, bottom_attach,
             xoptions, yoptions, xpadding, ypadding)

The table instance is the table you created with GtkTable(). The first parameter ("child") is the widget you wish to place in the table.

The left_attach,right_attach, top_attach and bottom_attach arguments specify where to place the widget, and how many boxes to use. If you want a button in the lower right table entry of our 2x2 table, and want it to fill that entry ONLY, left_attach would be = 1, right_attach = 2, top_attach = 1, bottom_attach = 2.

Now, if you wanted a widget to take up the whole top row of our 2x2 table, you'd use left_attach = 0, right_attach = 2, top_attach = 0, bottom_attach = 1.

The xoptions and yoptions are used to specify packing options and may be bitwise OR'ed together to allow multiple options.

These options are:

Padding is just like in boxes, creating a clear area around the widget specified in pixels.

attach() has a LOT of options but defaults are defined in PyGTK for the xoptions, yoptions, xpadding and ypadding parameters so the attach() method can be called as:
table.attach(widget, left_attach, right_attach, top_attach, bottom_attach)

The xoptions and yoptions default to FILL | EXPAND, and xpadding and ypadding, to 0. The rest of the arguments are identical to the previous method call.

We also have set_row_spacing() and set_col_spacing() methods. These add spacing between the rows at the specified row or column.
table.set_row_spacing(row, spacing)

table.set_col_spacing(column, spacing)

Note that for columns, the space goes to the right of the column, and for rows, the space goes below the row.

You can also set a consistent spacing of all rows and/or columns with:


Note that with these calls, the last row and last column do not get any spacing.