Sample Code 393: How to change the stock image of a button on the fly - Part 1 - stock image and label? |
|
Written by kksou
|
|
Wednesday, 19 December 2007 |
|
Problem Suppose you're developing an mp3 player with a 'Play' button as shown below.

When the user clicks the 'Play' button, you would like the button to change to 'Stop' button as shown below.

When the user clicks the 'Stop' button, the button will change back to 'Play' button:

Solution
- To change the label of a button, use GtkButton::set_label().
- To change the image of a button, use GtkButton::set_image().
- For windows users, in order to see the stock images, look for a file called
gtkrc. If you have used Gnope Installer to set up php-gtk2, it should be in <dir>\etc\gtk-2.0, where <dir> is the directory where you have installed php-gtk2. Open the file with an editor, on line 5, you should see a line gtk-button-images = 0. Change the 0 to 1 and save the file. Run the script again. You should be able to see the stock images now.
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 36 37 38
| <?php $window = new GtkWindow(); $window->set_title($argv[0]); $window->set_size_request(400, 200); $window->connect_simple('destroy', array('Gtk','main_quit')); $window->add($vbox = new GtkVBox());
// display title
$title = new GtkLabel("Change the stock image of a button\n". " Part 1 - stock image + label"); $title->modify_font(new PangoFontDescription("Times New Roman Italic 10")); $title->modify_fg(Gtk::STATE_NORMAL, GdkColor::parse("#0000ff")); $title->set_size_request(-1, 40); $title->set_justify(Gtk::JUSTIFY_CENTER); $alignment = new GtkAlignment(0.5, 0, 0, 0); $alignment->add($title); $vbox->pack_start($alignment, 0); $vbox->pack_start(new GtkLabel(), 0);
$vbox->pack_start(new GtkLabel("Click the play button below.\n". "The button will change to stop button.\n". "Click the button again. The stop button will change to play button."), 0); $vbox->pack_start(new GtkLabel(), 0);
// setup button
$vbox->pack_start($hbox = new GtkHBox(), 0); $button = new GtkButton('Play'); $img = GtkImage::new_from_stock(Gtk::STOCK_MEDIA_PLAY, Gtk::ICON_SIZE_BUTTON); // note 1
$button->set_image($img); // note 1
$button->set_size_request(80, 32); $button->connect('clicked', 'on_click'); $button_status = 'play';
$hbox->pack_start($button, 0);
$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
- Set the initial stock image to
Gtk::STOCK_MEDIA_PLAY.
- Change the button image from Play to Stop.
- Change the button image from Stop to Play.
- Change the corresponding button label.
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. |