我试图获取用户在文本字段 ( 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
每次?