Objective
In the previous example, we have displayed a form with three fields. However, the layout is not right, and the button is too wide.
We'll fix this in this example.
Overview
We use exactly the same technique as described in the previous example:
Sample Output
Notice that we have fixed the layout, and the button size is correct. However, the label is centered. It should be left or right-justified. We'll fix this in the next article.

Sample Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| <?php $window = new GtkWindow(); $window->connect_simple('destroy',array('Gtk','main_quit')); $window->set_size_request(400, 200); $vbox = new GtkVBox(); $window->add($vbox); spacer($vbox, 4); // add a 4-pixel top margin
$input_field_def = array( 'Item Code'=>120, 'Item Description'=>240, 'Price'=>80);
foreach($input_field_def as $label=>$field_width) { $hbox = new GtkHBox(); spacer($hbox, 3); // add a 3-pixel left margin
// the label
$label = new GtkLabel("$label: "); $label->set_size_request(100, -1); // note 1
$hbox->pack_start($label, false);
// the entry field
$input = new GtkEntry(); $input->set_size_request($field_width, -1); $hbox->pack_start($input, false);
$vbox->pack_start($hbox, false); }
// the submit button
$hbox = new GtkHBox(); // note 2
$button = new GtkButton('Submit'); // note 2
$button->set_size_request(60,24); // note 2
$button->connect('clicked', 'on_submit', $input); spacer($hbox, 3); spacer($hbox, 100); // note 3
$hbox->pack_start($button, false);
spacer($vbox, 6); //add a 6-pixel gap
$vbox->pack_start($hbox, false);
$window->show_all(); Gtk::main();
function on_submit($button, $input) {
|
- Note that this is only 70% of the sample code. You have to be a registered member to see the entire sample code. Please login or register.
- Registration is free and immediate.
- Have some doubt about the registration? Please read this forum article.
Explanation
We make use of the code in the previous example.
What's new here:
- Set the size of label to the same width.
- Set the Submit button to the right size by packing it inside a hbox with
expand set to false.
- Note that we add a 100-pixel spacer here so that the Submit button appears right below the input fields.
Note
Note that all the labels are centered. This is the default setting of a GtkLabel.
|