我试图得到什么用户input在文本字段( entry
),但是当我运行它,我不断从gtk_entry_get_text
null
。
错误:
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); }
那么我如何从用户那里得到input,而不是每次都是null
?
指针指针是需要的:
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);
你永远不会显示你设置txtVoltage
有效的小部件指针。
什么原因导致了entry_Submit()
被调用,也不是很清楚。 如果它连接到有问题的条目,则使用widget
参数而不是全局。 尽可能避免使用全局变量(在GTK +中,这确实很常见)。