Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add an image to a button with GTK

I am trying to add an image to a button with label, but the image doesn't show and the broken image doesn't display either.

stop_button = gtk_button_new_with_label("stop");
image = gtk_image_new_from_file ("/home/cendit/Escritorio/index.jpeg");
gtk_button_set_image (GTK_BUTTON(stop_button),image);

I tried a different path "file:///home/cendit/Escritorio/index.jpeg" but it was not successful.

like image 861
Edgar Gomez Avatar asked Oct 20 '25 01:10

Edgar Gomez


1 Answers

Images inside buttons are not visible by default, as we transitioned from GTK+ 2.x to 3.x. Sadly, the API hasn't been cleaned up to reflect this change, so it's a bit of a trap.

If you want to display a button with only an image inside it, you can use:

GtkWidget *image = gtk_image_new_from_file ("...");
GtkWidget *button = gtk_button_new ();

gtk_button_set_image (GTK_BUTTON (button), image);

On the other hand, if you want to have a button with both text and an image inside it, you can use:

GtkWidget *image = gtk_image_new_from_file ("...");
GtkWidget *button = gtk_button_new_with_label ("...");

gtk_button_set_always_show_image (GTK_BUTTON (button), TRUE);
gtk_button_set_image (GTK_BUTTON (button), image);

See the documentation of Gtk.Button.set_image for further information.

like image 177
ebassi Avatar answered Oct 21 '25 14:10

ebassi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!