Search code examples
ctextuser-inputgtk3

How to get 2 textentry in gtk3


I'v been seaching google for 2 weeks now. 2 textentry in a gride. (ex: userid, password) Glade let me design that no problem...

For one textentry widget, it working.

I'm compiling/linking with mysql so I would like to call functions, stored procedures with entry1 and entry2.

Please help thanks

:EDIT: 15:18 4 may 2019

I found a conclusive solution. (But it's throwing seg fault) bit of code following :

this is some kind of a transcription of the video into text (C code).

https://www.youtube.com/watch?v=_yTmW1QG3uk

obviously according to my goal really...

it's kind of working but let say you want to use the same instance a second time, "seg fault"

I'm sure I will find the problem (gtk_main_quit) or something, but the textentry multiples lines is solved :P

here's the code :

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


GtkEntry *userid, *password;

static void Button_Pressed(GtkWidget *w, gpointer *data){

/*   char *userid, *password;*/  //seg fault

/*   userid[0]='\0';
   password[0]='\0';*/

   userid=gtk_entry_get_text(userid);
   password=gtk_entry_get_text(password);


   g_print("%s\n\r%s\n\r",userid, password);

}

static void CreateWindow(GtkApplication *myapp, gpointer *user_data){

   GtkWidget *window;

   window=gtk_application_window_new(myapp);
   gtk_window_set_title(GTK_WIDGET(window), "Double Entry Solution");
   gtk_window_set_default_size(GTK_WINDOW(window),400,400);

   GtkWidget *vbox=gtk_vbox_new(FALSE,0);
   gtk_container_add(GTK_CONTAINER(window), vbox);
   gtk_widget_show(vbox);


   // userid pack
   userid=gtk_entry_new();
   gtk_box_pack_start(GTK_CONTAINER(vbox), userid, TRUE, TRUE,0);
   gtk_widget_show(userid);


   GtkWidget *hbox=gtk_hbox_new(TRUE,0);
   gtk_box_pack_start(GTK_BOX(vbox), hbox, TRUE, TRUE, 0);

   // password pack
   password=gtk_entry_new();
   gtk_box_pack_start(GTK_BOX(hbox), password, TRUE, TRUE,0);
   gtk_widget_show(password);

   GtkWidget *submit=gtk_button_box_new(GTK_ORIENTATION_HORIZONTAL);
   gtk_box_pack_start(GTK_BOX(vbox), submit, TRUE, TRUE,0);

   GtkWidget *button=gtk_button_new_with_label("Login");
   g_signal_connect(button, "clicked", G_CALLBACK(Button_Pressed), NULL);
   gtk_container_add(GTK_CONTAINER(submit), button);
   gtk_widget_show(button);



   gtk_widget_show_all(window);

}


int main(int argc, char** argv){

   GtkApplication *doubleentry;


   doubleentry=gtk_application_new("smdelectro.business.site.doubleentry", G_APPLICATION_FLAGS_NONE);

   g_signal_connect(doubleentry, "activate", G_CALLBACK(CreateWindow), NULL);


   g_application_run(G_APPLICATION(doubleentry), argc, argv);
   g_object_unref(doubleentry);




   return (EXIT_SUCCESS);
}


Solution

  • The code below work :

    #include <gtk/gtk.h>
    
    int main(int argc, char **argv)
    {
        // Init
        gtk_init(&argc,&argv);
        // Create widgets
        GtkWidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
        GtkGrid *grid     = GTK_GRID(gtk_grid_new());
        // Attach entries
        gtk_grid_attach(grid,gtk_entry_new(),0,0,1,1);
        gtk_grid_attach(grid,gtk_entry_new(),0,1,1,1);
        // Add the grid in the window
        gtk_container_add(GTK_CONTAINER(window),GTK_WIDGET(grid));
        // Dirty way to force clean termination when window is closed
        g_signal_connect(window,"delete-event",G_CALLBACK(gtk_main_quit),NULL);
        // Show **everythings** (not only the window)
        gtk_widget_show_all(window);
        // Main loop
        gtk_main();
        return 0;
    }
    

    Without any source code I can't provide more help, could you post it please.

    A common trap in GTK+3 is creating widget without showing them. If you replace gtk_widget_show_all with gtk_widget_show you will see a window without widgets, they are here but not displayed, because by default the visible property is set to FALSE.