I'm getting started with Gtk+, I'm following this tutorial how to make 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 can see on google a lot of people with same error, but I can't see the reply with how to fix this..
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;
}
glad 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>
compile 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 this? Thanks in advance.
The problem is that there is no object whose id is app
in your glade file. If you want to display the window, you have to pass the id associated with the window i.e. window1
. Thus you 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's message logging */
fprintf(stderr, "Unable to file object with id \"window1\" \n");
/* Your error handling code goes here */
}
There are few other suggestions:
1. It is better to use GError **
parameter in gtk_builder_add_from_file
call. So instead of just gtk_builder_add_from_file
use the following piece of code for your 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's message logging */
fprintf(stderr, "Error adding build from file. Error: %s\n", err->message);
/* Your error handling code goes here */
}
...
2. From your glade file, the root element glade-interface
indicates that you are using libglade
format for saving you glade file instead of GtkBuilder
format. Thus you may need to link libglade
in the build. But from the glade file you are indicating that Gtk version is 2.16 or above, you may need to use GtkBuilder
format. In that case you will need to convert to GtkBuilder
format which can be done using gtk-builder-convert
script or opening the glade file in Glade application & saving in the GtkBuilder
format (This option will depend on your Glade version).
Hope this helps!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With