In accordance with this answer no need to create a new Widget to swap the image. Just use gtk_image_set_from_image() __deprecated__ (or maybe gtk_image_set_from_file() )
I extracted the code below this another answer and I imagine he solves his problem. The following example is a array of images representing "leds"; if you click on an image, for example, the led will light up/delete with a change of the widget image img of the struct led (see the callback click_handler()
). The code is very easy to understand, take advantage :-)
#include <gtk/gtk.h>
#define ICON_WIDTH 16
#define ICON_HEIGHT 16
#define NUM_LEDS 2500
typedef enum {
ON,
OFF
} led_status;
typedef struct {
GtkWidget *img;
struct {
gint x;
gint y;
} pos;
led_status status;
} led;
static led leds[NUM_LEDS];
static GdkPixbuf *led_on;
static GdkPixbuf *led_off;
static gboolean click_handler(GtkWidget *widget,
GdkEvent *event,
gpointer user_data)
{
led *info = user_data;
if (info->status == ON) {
gtk_image_set_from_pixbuf(GTK_IMAGE(info->img), led_off);
info->status = OFF;
} else {
gtk_image_set_from_pixbuf(GTK_IMAGE(info->img), led_on);
info->status = ON;
}
return TRUE;
}
int main(int argc, char** argv)
{
GtkWidget *window, *layout;
int i = 0, x, y;
gtk_init(&argc, &argv);
/* Load our images (ignoring errors - as any good sample code would) */
led_on = gdk_pixbuf_new_from_file("led-on.png", NULL);
led_off = gdk_pixbuf_new_from_file("led-off.png", NULL);
/* Initialize our array */
for (x = 0; x < 50; x++) {
for (y = 0; y < 50; y++) {
leds[i].img = gtk_image_new();
leds[i].pos.x = x * ICON_WIDTH;
leds[i].pos.y = y * ICON_HEIGHT;
leds[i].status = OFF;
/* Initialize our image from the pixbuf we've already loaded */
gtk_image_set_from_pixbuf(GTK_IMAGE(leds[i].img), led_off);
i++;
}
}
/* Create a window */
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window), "LEDs");
gtk_signal_connect(GTK_OBJECT(window),
"destroy",
G_CALLBACK(gtk_main_quit),
NULL);
/* Create the widget */
layout = gtk_layout_new(NULL, NULL);
for (i = 0; i < NUM_LEDS; i++) {
/*
* A GtkImage doesn't have a window, so we need to put it inside
* a GtkEventBox so we can capture events.
*/
GtkWidget *eb = gtk_event_box_new();
g_signal_connect(G_OBJECT(eb),
"button_press_event",
G_CALLBACK(click_handler),
&leds[i]);
gtk_container_add(GTK_CONTAINER(eb), leds[i].img);
gtk_layout_put(GTK_LAYOUT(layout), eb, leds[i].pos.x, leds[i].pos.y);
}
gtk_container_add(GTK_CONTAINER(window), layout);
gtk_widget_show_all(window);
gtk_main();
return 0;
}
Good night Ana, post the code, code images are more difficult to reproduce, if something can be text then use text, really it is complicated to take a picture of the code right? I hope you’ll take this as a positive criticism.
– Guilherme Nascimento
Code images are also invisible to search engines, so other people may not find the solution to their similar problem.
– Pablo Almeida
Ana Cris, you speak Portuguese? ¿Ana Cris, Usted hablas Portugués? I ask this because your code is in Spanish and you used the words "add" and "atril" in the text.
– Victor Stafusa
I speak both languages :)
– Ana Cris
@Anacris , college work requires you to use GTK or you chose to use GTK ?
– Nelson Teixeira
@Anacris I would like to know if you have solved your problem - I believe you have. If so, put an answer yourself and mark it as a solution! If you have not solved the problem, could you tell me if that variable
image
is the typeGtkWidget *
? I would also like to say this: I particularly dislike the use of nonconstant global variables likeimage
,estado
,Element
and the others present in its function. I recommend - I believe many others too - that you refer everything to the function you will use and only then modify its contents.– José