Gtk-critical **: gtk_widget_show statement GTK_IS_WIDGET (WIDGET)

I start working with Gtk +, I follow this guide on how to make the first application, but when I try to run the executable, I get this error:

Gtk-Critical **: gtk_widget_show assertion GTK_IS_WIDGET(WIDGET) 

I see a lot of people on the google with the same error, but I don’t see the answer how to fix it.

My C code:

 #include <stdlib.h> #include <gtk/gtk.h> GtkBuilder *builder; GtkWidget *app; G_MODULE_EXPORT void on_app_destroy (void) { gtk_main_quit (); } G_MODULE_EXPORT void on_menu_quit_activate (void) { gtk_main_quit (); exit(EXIT_SUCCESS); } int main (int argc, char *argv[]) { /* Initialize GTK+ */ g_log_set_handler ("Gtk", G_LOG_LEVEL_WARNING, (GLogFunc) gtk_false, NULL); gtk_init (&argc, &argv); g_log_set_handler ("Gtk", G_LOG_LEVEL_WARNING, g_log_default_handler, NULL); builder = gtk_builder_new (); gtk_builder_add_from_file (builder, "tut.glade", NULL); app = GTK_WIDGET (gtk_builder_get_object (builder, "app")); gtk_builder_connect_signals (builder, NULL); g_object_unref (G_OBJECT (builder)); /* Enter the main loop */ gtk_widget_show (app); gtk_main (); return 0; } 

joyful file:

 <?xml version="1.0"?> <glade-interface> <!-- interface-requires gtk+ 2.16 --> <!-- interface-naming-policy project-wide --> <widget class="GtkWindow" id="window1"> <child> <widget class="GtkFixed" id="fixed1"> <property name="visible">True</property> <child> <widget class="GtkButton" id="button1"> <property name="label" translatable="yes">button</property> <property name="width_request">113</property> <property name="height_request">42</property> <property name="visible">True</property> <property name="can_focus">True</property> <property name="receives_default">True</property> </widget> <packing> <property name="x">149</property> <property name="y">69</property> </packing> </child> </widget> </child> </widget> <widget class="GtkWindow" id="window2"> <child> <placeholder/> </child> </widget> </glade-interface> 

compilation options:

 `pkg-config --cflags --libs gtk+-2.0` -export-dynamic 

Linker Settings:

 -export-dynamic 

My IDE: Code :: Blocks, compiler: GNUC C compiler

How to fix it? Thanks in advance.

+6
source share
1 answer

The problem is that there is no object with app identifier in your glade file. If you want to display the window, you must pass the identifier associated with the window ie window1 . Thus, the code can be changed to:

 app = GTK_WIDGET (gtk_builder_get_object (builder, "window1")); if (NULL == app) { /* Print out the error. You can use GLib message logging */ fprintf(stderr, "Unable to file object with id \"window1\" \n"); /* Your error handling code goes here */ } 

There are several other suggestions:
1. It is better to use the GError ** parameter in the GError ** call. So instead of gtk_builder_add_from_file use the following code for reference:

 GError *err = NULL; /* It is mandatory to initialize to NULL */ ... if(0 == gtk_builder_add_from_file (builder, "tut.glade", &err)) { /* Print out the error. You can use GLib message logging */ fprintf(stderr, "Error adding build from file. Error: %s\n", err->message); /* Your error handling code goes here */ } ... 

2. The root element of the glade-interface indicates that you are using the libglade format to save the glade file instead of the GtkBuilder format. Thus, you may need to link libglade in the assembly. But from the glade file, you indicate that the version of Gtk is 2.16 or higher, you may need to use the GtkBuilder format. In this case, you will need to convert to the GtkBuilder format, which can be done using the gtk-builder-convert script or open the file in the Glade application and save it in the GtkBuilder format (this option will depend on your version of Glade).
Hope this helps!

+8
source

Source: https://habr.com/ru/post/906619/


All Articles