0
votes

As I am new to glade and C, I am trying to build a very simple GUI which has an entry box a label and a button. When the button is pressed whatever values present in the text box will we show it in label. This is my glade,

<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.22.1 -->
<interface>
  <requires lib="gtk+" version="3.20"/>
  <object class="GtkWindow" id="windows1">
    <property name="can_focus">False</property>
    <child>
      <placeholder/>
    </child>
    <child>
      <object class="GtkFixed">
        <property name="visible">True</property>
        <property name="can_focus">False</property>
        <child>
          <object class="GtkButton" id="button1">
            <property name="label" translatable="yes">button</property>
            <property name="width_request">100</property>
            <property name="height_request">80</property>
            <property name="visible">True</property>
            <property name="can_focus">True</property>
            <property name="receives_default">True</property>
            <signal name="clicked" handler="on_clicked" swapped="no"/>
          </object>
          <packing>
            <property name="x">182</property>
            <property name="y">146</property>
          </packing>
        </child>
        <child>
          <object class="GtkEntry" id="entry1">
            <property name="width_request">100</property>
            <property name="height_request">80</property>
            <property name="visible">True</property>
            <property name="can_focus">True</property>
          </object>
          <packing>
            <property name="x">68</property>
            <property name="y">45</property>
          </packing>
        </child>
        <child>
          <object class="GtkLabel" id="label1">
            <property name="width_request">100</property>
            <property name="height_request">80</property>
            <property name="visible">True</property>
            <property name="can_focus">False</property>
            <property name="label" translatable="yes">label</property>
          </object>
          <packing>
            <property name="x">321</property>
            <property name="y">44</property>
          </packing>
        </child>
      </object>
    </child>
  </object>
</interface>

And this is the C logic which on button press prints clicked in terminal.

#include <gtk/gtk.h>

void on_clicked(GtkButton * b, gpointer data)
{
    (void)b;
    (void)data;
    printf("clicked");
}

int main (int argc, char *argv[])
{
    GtkBuilder      *builder;
    GtkWidget       *window;

    gtk_init(&argc, &argv);

    builder = gtk_builder_new();
    gtk_builder_add_from_file (builder, "example.glade", NULL);

    window = GTK_WIDGET(gtk_builder_get_object(builder, "window1"));

    gtk_builder_connect_signals(builder, NULL);

    g_object_unref(builder);

    gtk_widget_show(window);
    gtk_main();

    return 0;
}

// called when window is closed
void on_window_main_destroy()
{
    gtk_main_quit();
}

I compiled the c code without any error using

gcc main.c $(pkg-config --cflags --libs gtk+-3.0) -o main -export-dynamic

But when I run I get this warning but no gui

Gtk-CRITICAL **: : gtk_widget_show: assertion 'GTK_IS_WIDGET (widget)' failed

How can I solve this problem? The second question is how can I fetch values from the text box and show it in label using c.

1

1 Answers

1
votes

The error message you're getting is because your GtkWindow has id "windows1", while you're trying to fetch it using id "window1" (note that the former has an 's' in the middle, while the latter doesn't).

For your other question: you should fetch the GtkEntry widget (also using gtk_builder_get_object()) and save the resulting pointer somewhere. You can then fetch its contents once you are inside your callback.