Click here to Skip to main content
15,886,795 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
This is my code:

#include <gtk/gtk.h>
#include <gdk-pixbuf/gdk-pixbuf.h>

void destroy(void) {
  gtk_main_quit();
}


int main()
{
    GdkPixbuf* buf;
    GdkPixbuf* buf2;
    GError* err = NULL;
    int a=0, i=0, j=0;

    GtkWidget* window;
    GtkWidget *image =0;


    gtk_init (NULL,NULL);
    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    buf = gdk_pixbuf_new_from_file("1.jpg", &err);
    buf2 = gdk_pixbuf_new_from_file("2.jpg", &err);


   for(i=0; i<10; i++)
   {


         if((i%2)==0)
     {

       gtk_image_set_from_pixbuf(GTK_IMAGE(image), buf);

            for(j=0; j<10000; j++)
            {}
          }else
          {

         gtk_image_set_from_pixbuf(GTK_IMAGE(image), buf2);
            for(j=0; j<10000; j++)
            {}
    }

  g_signal_connect(G_OBJECT (window), "destroy",
  G_CALLBACK (destroy), NULL);
  gtk_container_add(GTK_CONTAINER (window), image);
  gtk_widget_show_all(window);
  gtk_main();

    }

return 0;
}



And this is the result:
(gtk:4783): Gtk-CRITICAL **: gtk_image_set_from_pixbuf: assertion 'GTK_IS_IMAGE (image)' failed

(gtk:4783): Gtk-CRITICAL **: gtk_container_add: assertion 'GTK_IS_WIDGET (widget)' failed
^C


What I have tried:

What I am trying to do is : I have 2 images, and a loop that I want to show one image for odd numbers and other image for even numbers. But this simple task seems very hard to do! please help me to correcting my code...thank you!
Posted
Updated 13-Dec-17 3:16am
Comments
Richard MacCutchan 12-Dec-17 13:59pm    
Where does this message occur and what are the parameters that are causing it?

1 solution

I have not used GDK so far but the errors are quite clear.

Both are sourced by your image being NULL. You have to create a new empty GtkImage widget first:
C++
image = gtk_image_new();


Note also that you should check if loading from the files was successful:
C++
buf = gdk_pixbuf_new_from_file("1.jpg", &err);
if (NULL == buf)
{
    fprintf(stderr, "Loading image 1 failed with error %s\n", err->message);
    return 1;
}
buf2 = gdk_pixbuf_new_from_file("2.jpg", &err);
if (NULL == buf2)
{
    fprintf(stderr, "Loading image 2 failed with error %s\n", err->message);
    return 1;
}
You have not specified a full path to the image files. Then the current working directory is used. If that does not contain the image files, loading will fail.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900