4.2. Containers with more than one child

Gtk2::Box and Gtk2::Table classes can contain more than one child. The Gtk2::Box class is further divided into Gtk2::HBox and Gtk2::VBox sub classes. In later sections we will do more advanced containers like Gtk2::TreeView and Gtk2::TextView.


A horizontal or vertical Gtk2::Box can be used to contain a row or column of widgets. Various options can be set to modify the spacing between the widgets, the alignment of the widgets in the Gtk2::Box, or the behaviors of the Gtk2::Box when the user resizes the parent widget. Boxes work only in one direction. The widgets inside a horizontal box always have the height of the box, and widgets in a vertical box always have the width of the vertical box.

You can create a Gtk2::HBox instance by calling:

 widget = Gtk2::HBox->new ($homogeneous=0, $spacing=5) 
$homogeneous is a boolean, telling Gtk+ whether the children should each have the same space in the box.

$spacing is an integer, which tells Gtk+ how much space to leave between the widgets in the box.

To add widgets to the Gtk2::Box, use the following method:

$box->pack_end ($child, $expand, $fill, $padding)  
$box->pack_start ($child, $expand, $fill, $padding) 

The illustrations below show how it will be packed in the Gtk2::HBox and Gtk2::VBox respectively.

Figure 4-2. HBox packing

Figure 4-3. VBox packing

$expand, $fill and $padding explained

When packing a widget into a Gtk2::Box class widget using the pack_end or pack_start methods, you need to specify these values.

$homogeneous explained

When creating an instance of Gtk2::HBox or Gtk2::VBox you need to specify the value of $homogeneous. $homogeneous is of type boolean. Setting this to "TRUE" will have the effect of setting the $expand value to "TRUE" on ALL the packed widgets. This will then give equal space to all widgets packed into the box.


Most of the times this is not desirable, and as a rule of thumb, should be set to FALSE

#widget = Gtk2::VBox ->new ($homogeneous=FALSE, $spacing=5) 
my $vbox = Gtk2::VBox ->new(FALSE,5); 


The Gtk2::Table widget is used to set widgets in a grid inside the parent widget. It acts like a grid with cells, in which you can hang your widgets. If the user resizes the parent widget, then the size of the grid cells changes, and the widgets will change their location and size, based on the size of the new grid cells.

Consider the following drawing:

Figure 4-6. Schematic of Table

The perl code to create it will include the following:

To create the Gtk2::Table object:

			#widget = Gtk2::Table ->new ($rows, $columns, $homogeneous=FALSE)
			my $widget = Gtk2::Table ->new (2, 2, FALSE);

To add the Gtk2::Label object:

#$table->attach_defaults ($widget, $left_attach, $right_attach, $top_attach, $bottom_attach)
$table->attach_defaults ($label, 0, 2, 0, 1);

To add the Gtk2::Button object:

$table->attach_defaults ($button, 0, 1, 1, 2);

To add the Gtk2::Entry object:

$table->attach_defaults ($entry, 1, 2, 1, 2);


To fit more than one widget onto a Gtk2::Bin sub class object, you need to use either the Gtk2::Box (be it Gtk2::HBox or Gtk2::VBox ) or Gtk2::Table class widgets. To overcome the the limitations of Gtk2::Bin, do the following:



You may notice that, even when you set the padding to "0", the widgets are not really touching each other, when packed. This is because any widgets that gets packet into Gtk2::Box subclasses will have a default spacing of 5 to start with.