2.14 A form with three fields - Part 1
Written by kksou   
Wednesday, 02 April 2008

Objective

In this example, we will expand the form to include three fields. Take a look at the sample output. Yes, I know the layout is not right, and the button is too wide. And you may say that why not do this with GtkTable?

Well, be patient. I just want to show you that with simple widgets like GtkHBox and GtkVBox, we can accomplish the same effect as provided by some other widgets such as GtkTable and GtkIconView – with the added benefit that you have a lot more control over the positioning and sizing.

Go through these couple of exercises with me. Many of the techniques used here will be applicable to other widgets such as GtkTable.

Overview

We use exactly the same technique as described in the previous example:

  • Each field will occupy one row. So we create a new hbox for each field.
  • For each field, we create a GtkLabeland a GtkEntry, set the desired width, and pack them into the hbox – all with expand=false.

Sample Output

2.14.gif

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   
<?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( // note 1
    'Item Code'=>120,
    'Item Description'=>240,
    'Price'=>80);

foreach($input_field_def as $label=>$field_width) {
    $hbox = new GtkHBox(); // note 2
    spacer($hbox, 3); // add a 3-pixel left margin

    // the label
    $hbox->pack_start(new GtkLabel("$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
$button = new GtkButton('Submit');
$button->set_size_request(60,24);
$button->connect('clicked', 'on_submit', $input);
spacer($vbox, 6); //add a 6-pixel gap
$vbox->pack_start($button, false);  // note 3

$window->show_all();
Gtk::main();

function on_submit($button, $input) {
    $str = $input->get_text();
    echo "you have entered: $str\n";
  • 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.

  1. This array defines the fields. Each associative array comprises "label_of_field" => width_of_field.
  2. Create a new row for each field.
  3. Add a submit button after all the fields are displayed.


User reviews

There are no user reviews yet.

Note: You have to be a registered member to leave a comment. Free registration here.

 
< Prev   Next >

Copyright © 2006-2008. kksou.com. All Rights Reserved