1.3 Responding to button click |
|
Written by kksou
|
|
Wednesday, 12 March 2008 |
Objective
We've displayed a button in the previous example. Now we will respond to button clicks.
Overview
- When a user presses the button, a signal clicked is generated. Think of signal as something php-gtk generates to inform you that something has occurred, in this example, the user has clicked the button.
- By connecting the signal to a callback function, every time there is a 'clicked' signal, php-gtk will automatically call the callback function that you have connected.
Sample Output

Sample Code
1 2 3 4 5 6 7 8 9 10
| <?php $window = new GtkWindow(); $window->connect_simple('destroy',array('Gtk','main_quit'));
$button = new GtkButton("click me!"); $button->connect('clicked', 'on_click'); // note 1
$window->add($button); $window->show_all(); Gtk::main();
|
- 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
- Connects the signal 'clicked' to the callback function
on_click().
- This is the callback function that is called when the user clicks on the button. Here we simply echo the words "button clicked!" to the command window.
Note
- Different signals are generated by a widget for different types of events. Each signal has a unique name assigned to it. E.g. for GtkButton:
- the signal clicked is emitted when the user clicks the button.
- the signal pressed is emitted when the button is being pressed
- the signal released is emitted when the button is being released.
- You will sometimes see people referring callback functions as signal handlers, since these callback functions are used to handle signals.
|