Skip to content
Advertisement

Get text from GTK entry

I’m trying to get what a user has inputed in a text field (entry) but when i run it i keep getting null from gtk_entry_get_text.

Errors :

Gtk-CRITICAL **: IA__gtk_entry_get_text: assertion `GTK_IS_ENTRY (entry)' failed

Entry contents: (null)

Code :

static GtkWidget *txtVoltage;
static GtkWidget *window;
static GtkWidget *vbox, *hbox;
static void entry_Submit(GtkWidget *widget, GtkWidget *entry)
{
    const gchar *SVolt = gtk_entry_get_text(GTK_ENTRY (txtVoltage));
    printf ("Entry contents: %sn", SVolt);
}
static void CreateTextBox(GtkWidget *entry, GtkWidget *vbox)
{
    entry = gtk_entry_new();
    gtk_entry_set_text (GTK_ENTRY (entry), "");
    gtk_box_pack_start (GTK_BOX (vbox), entry, TRUE, TRUE, 0);
}
int main(int argc, char *argv[] )
{
    GtkWidget *button;
    gtk_init (&argc, &argv);

    window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    gtk_widget_set_size_request (GTK_WIDGET (window), 300, 300);
    gtk_window_set_title (GTK_WINDOW (window), "Elektro GTK");
    g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL);
    g_signal_connect_swapped (window, "delete-event", G_CALLBACK (gtk_widget_destroy), window);

    vbox = gtk_vbox_new (FALSE, 0);
    gtk_container_add (GTK_CONTAINER (window), vbox);
    gtk_widget_show (vbox);

    hbox = gtk_hbox_new (FALSE, 0);
    gtk_container_add (GTK_CONTAINER (vbox), hbox);
    gtk_widget_show(hbox);

    CreateTextBox(txtVoltage, vbox);

    button = gtk_button_new_with_label("Submit");
    g_signal_connect_swapped (button, "clicked", G_CALLBACK (entry_Submit), window);
    gtk_box_pack_start (GTK_BOX (vbox), button, TRUE, TRUE, 0);
    gtk_widget_set_can_default (button, TRUE);
    gtk_widget_grab_default (button);
}

So how do i get the input from the user and not just null every time?

Advertisement

Answer

Pointer to pointer is needed:

static void CreateTextBox(GtkWidget **entry, GtkWidget *vbox)
{
    *entry = gtk_entry_new();
    gtk_entry_set_text (GTK_ENTRY (*entry), "");
    gtk_box_pack_start (GTK_BOX (vbox), *entry, TRUE, TRUE, 0);
}

And in main:

CreateTextBox(&txtVoltage, vbox);
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement