|
Objective
In the previous example, we have one button, and one callback function to handle its button clicks.
Suppose now we have three buttons. Of course, we could connect the button clicks to three separate callback functions. However, php-gtk also allows us to handle all the button clicks with just one signal handler.
Overview
- connect the signal 'clicked' for all the three buttons to the same callback function.
- In the callback function, you will find that the first argument $button gives you a pointer to the button that is being clicked on.
- To retrieve the label of the button, we use the method GtkButton::get_label()
Sample Output

Sample Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| <?php $window = new GtkWindow(); $window->connect_simple('destroy',array('Gtk','main_quit')); $window->set_size_request(200, 100); $vbox = new GtkVBox(); $window->add($vbox);
$hbox = new GtkHBox(); for ($i=1; $i<=3; ++$i) { $button = new GtkButton('button'.$i); $button->connect('clicked', 'on_click'); // note 1
$hbox->pack_start($button, false); $hbox->pack_start(new GtkLabel(' '), false); }
$vbox->pack_start($hbox, false);
|
- 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
- Connect the signal 'clicked' to the function
on_click().
- Note the first argument $button. This is passed along by php-gtk when calling your signal handler.
- Get the label of the button that is being clicked on, and echo it in the command window.
Data Automatically Passed to Callback Functions
When php-gtk invokes your callback function, most of the time it will automatically pass along some useful data in the arguments. Take a look at some of these signals in the php-gtk manual:
- signal 'clicked':
void callback(GtkButton button)
- signal 'key-press-event':
bool callback(GtkWidget widget, GdkEvent event)
- signal 'set_cell_data_func':
void callback(GtkCellLayout cell_layout, GtkCellRenderer cell, GtkTreeModel tree_model, GtkTreeIter iter [, user_data])
Note that:
- For each signal, different data are passed along by php-gtk.
- The first argument is usually the widget that emitted the signal.
Differentiating the Source of Signals
For the signal 'clicked', the first argument we receive in the callback function is a pointer to the GtkButton that is being clicked on. With this pointer, we make a call to the method GtkButton::get_label() to get the corresponding label of the button. Therefore, even though we have used just one callback function, we're able to differentiate exactly which is the button that triggered the signal.
User reviews There are no user reviews yet. Note: You have to be a registered member to leave a comment. Free registration here. |