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

1.3.gif

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

  1. Connects the signal 'clicked' to the callback function on_click().
  2. 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.


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