从GTK条目获取文本

时间:2013-05-24 11:12:31

标签: c linux gtk

我正在尝试获取用户在文本字段中输入的内容(entry),但是当我运行它时,我会从null获得gtk_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

2 个答案:

答案 0 :(得分: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);

答案 1 :(得分:0)

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

同样也不清楚导致entry_Submit()被调用的原因。如果它连接到相关条目,请使用widget参数而不是全局参数。尽可能避免使用全局变量(在GTK +中,这通常确实如此)。