PHP-GTK2 Newsletter
PHP-GTK2 Tips & Techniques
FREE Newsletter
by kksou
Sample Code 391: How to create a flashing button - Part 1? |
|
Written by kksou
|
|
Sunday, 16 December 2007 |
|
Problem You would like to create a flashing background color as shown below. The 'Click Me' button will keep flashing until the user clicks on the button.

Solution
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
| <?php $window = new GtkWindow(); $window->connect_simple('destroy', array( 'Gtk', 'main_quit')); $window->set_size_request(400,150);
$window->add($vbox = new GtkVBox());
// display title
$title = new GtkLabel("Create a Flashing Button - Part 1"); $title->modify_font(new PangoFontDescription("Times New Roman Italic 10")); $title->modify_fg(Gtk::STATE_NORMAL, GdkColor::parse("#0000ff")); $title->set_size_request(-1, 60); $vbox->pack_start($title, 0, 0);
$vbox->pack_start($hbox=new GtkHBox(), 0, 0); $button = create_button($hbox, 'Click Me!', '#FFCC66'); $timeout_handle = Gtk::timeout_add(500, 'flash', $button, '#FFCC66'); // note 1
$window->realize(); $org_bg = $window->get_style()->bg[Gtk::STATE_NORMAL]; // note 2
$window->show_all(); Gtk::main();
function create_button($hbox, $button_label, $bg_color) { $button = new GtkButton($button_label); $button->set_size_request(80, 32); $button->modify_bg(Gtk::STATE_NORMAL, GdkColor::parse($bg_color)); $hbox->pack_start($button, 1, 0); $button->connect('clicked', "on_button", $button_label); return $button; }
function on_button($button, $button_label) {
|
- 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 The above code is based on How to set the background color of GtkButton?
What's new here:
- Call the function
flash() once every 500ms (i.e. half a second).
- Get the default gray color of the button.
- Alternate the background color of the button to create the flashing effect.
- Stops the flashing!
Related Links
User reviews There are no user reviews yet. Note: You have to be a registered member to leave a comment. Free registration here. |
|