20.8. Responding to User Actions20.8.1. ProblemYou want to do something when a user clicks a button, chooses an item from a dropdown list, or otherwise interacts with a GUI widget. 20.8.2. SolutionWrite a callback function and then associate the callback function with a signal using the connect( ) method:
20.8.3. DiscussionThe code in the Solution displays a window with a button in it. On the button is the time, rendered by strftime('%c'). When the button is clicked, its label is updated with the current time. The update_time( ) function is called each time the button is clicked because $button->connect('clicked','update_time') makes update_time( ) the callback function associated with the button's clicked signal. The first argument to the callback function is the widget whose signal triggered the call as its first argument. In this case, that means that $button is passed to update_time( ). You tell connect( ) to pass additional arguments to the callback by passing them to connect( ) after the callback function name. This example displays a window with a button and a separate label. The time is printed in the label and updated when the button is clicked:
Because $label is on the list of arguments passed to $button->connect( ), $label is passed to update_time( ). Calling set_text( ) on $label updates the text displayed in the label. 20.8.4. See AlsoDocumentation on signals and callbacks at http://gtk.php.net/manual/en/gtk.signals.php, on GtkObject::connect( ) at http://gtk.php.net/manual/en/gtk.gtkobject.method.connect.php, and on GtkButton's clicked signal at http://gtk.php.net/manual/en/gtk.gtkbutton.signal.clicked.php.
Copyright © 2003 O'Reilly & Associates. All rights reserved. |
|