1

我试图获取用户在文本字段 ( entry) 中输入的内容,但是当我运行它时,我不断nullgtk_entry_get_text.

错误:

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

Entry contents: (null)

代码 :

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: %s\n", 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);
}

那么我如何从用户那里获得输入,而不仅仅是null每次?

4

2 回答 2

1

需要指向指针的指针:

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);
}

主要是:

CreateTextBox(&txtVoltage, vbox);
于 2013-05-24T12:28:44.440 回答
0

您永远不会显示您设置txtVoltage为有效的小部件指针。

也不清楚是什么原因entry_Submit()被调用。如果它连接到有问题的条目,请使用widget参数而不是全局。尽可能避免使用全局变量(在 GTK+ 中,这确实很常见)。

于 2013-05-24T11:30:49.807 回答