简体   繁体   中英

pass string from inputfield to function fails - GTK c-language

I have one issue with gtk in c . I tried to follow the tutorial but I am not able to pass a text-entry to a function when one clicks on the button in the widget.

The code compiles fine but when I press the button I get several warnings and the string from text-entry that was supposed to be printed are null

What did I do wrong?

 #include <stdio.h>
 #include <stdlib.h>
 #include <gtk/gtk.h>


 static GtkWidget *asset_label;
 static GtkWidget *frame;
 static GtkWidget *entry;

 static void entry_Submit(GtkWidget *widget, GtkWidget *entry)
 {
    const gchar *text = gtk_entry_get_text(GTK_ENTRY (entry));
    printf ("Result: %s\n", text);

    gtk_widget_destroy(GTK_WIDGET(asset_label));
    asset_label = gtk_label_new (text);
    gtk_container_add (GTK_CONTAINER (frame), asset_label);

    gtk_widget_show_all(frame);

 }



 static void destroy(GtkWidget *widget, gpointer data)
 {
     gtk_main_quit ();
 }



 static void initialize_window(GtkWidget* window)
 {
   gtk_window_set_title(GTK_WINDOW(window),"My Window"); //Set window title
   gtk_window_set_default_size (GTK_WINDOW (window), 400, 200); //Set default size for the window
   g_signal_connect (window, "destroy", G_CALLBACK (destroy), NULL); //End application when close button clicked

 }

 int main (int argc, char *argv[])
 {
   GtkWidget *window,*table,*label, *button;
   gtk_init(&argc, &argv);


   //Create the main window
   window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
   initialize_window(window);


    /* Create a 1x2 table */
    table = gtk_table_new (3, 3, TRUE);
    gtk_container_add (GTK_CONTAINER (window), table);


   /* create a new label. */
   label = gtk_label_new ("Enter some text:" );
   //gtk_misc_set_alignment (GTK_MISC (label), 0, 0);
   gtk_table_set_homogeneous(GTK_TABLE (table), TRUE);
   gtk_table_attach_defaults (GTK_TABLE (table), label, 1, 2, 0, 1);


   //create a text box
   entry = gtk_entry_new ();
   //gtk_entry_set_text (GTK_ENTRY (entry), "");
   gtk_entry_set_max_length (GTK_ENTRY (entry),0);
   gtk_table_attach_defaults (GTK_TABLE (table), entry, 0, 1, 0, 1);



   button = gtk_button_new_with_label("Calculate");
   g_signal_connect_swapped (button, "clicked", G_CALLBACK (entry_Submit), entry);
   gtk_table_attach_defaults (GTK_TABLE (table), button, 0, 2, 1, 2);



   //gtk_widget_show (button);

    gtk_widget_show_all(window);

   gtk_main ();
   return 0;
 }

And when the button is clicked I get this result:

 Result: (null)

There is a problem in the callback or the way you are registering the callback (as fixing either one of them should fix your problem).
By default the clicked callback takes GtkButton as the first parameter and gpointer data as the second. By using g_signal_connect_swapped you are saying that in the callback function, the parameters will be swapped ie, the first parameter will be gpointer data ( GtkEntry in your code) and second GtkButton . But in your callback function you are treating second parameter entry which is in fact GtkButton as GtkEntry . Either use g_signal_connect instead of g_signal_connect_swapped or use the first parameter widget as GtkEntry in your callback function.
Side note: Regarding the warning, if the code which you have posted is the full code then in the callback function entry_Submit during first execution asset_label is null and thus gtk_widget_destroy(GTK_WIDGET(asset_label)); will throw a warning. Also, frame is unassigned before use in the callback function.

Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM