2.13 A simple form with only one entry field |
|
Written by kksou
|
|
Tuesday, 01 April 2008 |
Objective
A form is very common in php-gtk applications. Each field usually has a label plus an entry box for user to enter inputs. We will now learn how to display such entry fields. For this example, we will start with only one field.
Overview
Until now we have only used GtkLabel and GtkButton. In this example, we will use one more widget, the GtkEntry, for data input.
- Create a GtkLabel.
- Create a GtkEntry.
- Create a GtkButton as the submit button.
Sample Output

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
| <?php $window = new GtkWindow(); $window->connect_simple('destroy',array('Gtk','main_quit')); $window->set_size_request(400, 100); $vbox = new GtkVBox(); $window->add($vbox);
$hbox = new GtkHBox();
spacer($hbox, 3); // add a 3-pixel left margin
// the label
$hbox->pack_start(new GtkLabel('Item code: '), false);
// the entry field
$input = new GtkEntry(); $input->set_size_request(200, -1); $hbox->pack_start($input, false);
// the submit button
$button = new GtkButton('Submit'); $button->set_size_request(60,24); $button->connect('clicked', 'on_submit', $input); // note 3
spacer($hbox, 6); // note 1
$hbox->pack_start($button, false);
spacer($vbox, 4); // add a 4-pixel top margin
$vbox->pack_start($hbox, false);
$window->show_all(); Gtk::main();
function on_submit($button, $input) { // note 3
$str = $input->get_text(); // note 2
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
- Try commenting this line. You will find that without the spacer, the submit button will stick to the input field as follows:

- This is how we get the user input from an entry field with the use of the method GtkEntry::get_text().
- Note how the pointer to the GtkEntry
$input is passed along with the signal handler. We need this to retrieve the value input by the user. We will explain this in more detail in the next Chapter on Signal Handling.
|