Boxes - GtkHBox and GtkVBox

The most simple, but most often used containers are the GtkBoxes: GtkHBox and GtkVBox. They allow to add widgets in a single row, not more, and not less. GtkHBox arranges them horizontally, while GtkVBox lines them up vertically.

You add the widgets via pack_start() or pack_end() . pack_start() adds them one after the other from the beginning (left to right resp. top to bottom), while pack_end() adds them from the end on (right to left, or bottom to top).

You can influence the layout by adjusting the three optional parameters of pack_start() and pack_end() - see their documentation.

Example 7.1. Simple dynamic layout with GtkVBox

$w = new GtkWindow();
$w->set_title('GtkBox test');
$w->connect_simple('destroy', array('gtk', 'main_quit'));

$lbl = new GtkLabel('Your name:');
$scrwnd = new GtkScrolledWindow();
$txt = new GtkTextView();
$btn = new GtkButton('Send');

$vbox = new GtkVBox();

$vbox->pack_start($lbl, false);
$vbox->pack_start($scrwnd, true, true);
$vbox->pack_start($btn, false);


Start the demo and resize the window: The multiline text widget adjusts its size to fill all the space in the window which is not needed for the label or the button. Change the third parameter from true to false at the place where scrwnd is packed - what happens?

A special version of GtkBoxes are GtkHButtonBox and GtkVButtonBox: They behave same as their box counterparts, but can contain GtkButtons only. To compensate this limitation, they have some layout functions often needed for rows of buttons.