GTK Entry – User Input

Advertisement

GTK Entry is a single-line text entry field.

Create New Entry

GtkWidget * gtk_entry_new (void);

The gtk_entry_new() function returns a GtkWidget and creates a new entry field.

Set GTK Entry Placeholder

void gtk_entry_set_placeholder_text (GtkEntry *entry, const gchar *text);

This sets the text to be displayed in entry the field when it is empty and unfocused.

Note: The gtk_entry_new() returns a GtkWidget, and the gtk_entry_set_placeholder_text() function accepts the GtkEntry type as the first parameter, so casting is required.

Example:

GtkWidget *emailLabel,*emailEntry;
emailLabel = gtk_label_new("Email:");
emailEntry = gtk_entry_new();
gtk_entry_set_placeholder_text(GTK_ENTRY(emailEntry),"Email");
Gtk Entry Field Placeholder
GTK Entry Placeholder

Set GTK Entry Icon

void gtk_entry_set_icon_from_gicon (GtkEntry *entry, GtkEntryIconPosition icon_pos, GIcon *icon);

This sets the icon shown in the entry at the specified position from the current icon theme.

The gtk_entry_set_icon_from_gicon() function takes three parameters:

  • Entry widget
  • Icon Position – GTK_ENTRY_ICON_PRIMARY & GTK_ENTRY_ICON_SECONDARY
  • GIcon

Example:

emailLabel = gtk_label_new("Email:");
emailEntry = gtk_entry_new();
gtk_entry_set_placeholder_text(GTK_ENTRY(emailEntry),"Email");
GIcon *icon;
GFile *path; 
path = g_file_new_for_path("D:/path/emailicon.png"); 
icon = g_file_icon_new(path); 
gtk_entry_set_icon_from_gicon(GTK_ENTRY(emailEntry),GTK_ENTRY_ICON_PRIMARY,icon);
GTK Entry Icon
GTK Entry Icon

Entry Visibility

void gtk_entry_set_visibility (GtkEntry *entry, gboolean visible);

This sets whether the contents of the entry are visible or not.

The first parameter of the gtk_entry_set_visibility() function is the GtkEntry widget, next is a gboolean value, when set to FALSE, characters are displayed as the invisible char.

Output:

GTK Entry Visibility
GTK Entry Visibility

Get Text from Entry Field

const gchar * gtk_entry_get_text (GtkEntry *entry);

This function retrieves the contents of the entry widget.

Example:

GtkWidget *emailEntry;
emailEntry = gtk_entry_new(); 
gtk_entry_set_placeholder_text(GTK_ENTRY(emailEntry),"Email");

const gchar *emailData = gtk_entry_get_text(GTK_ENTRY(emailEntry));

GTK Entry Application

Let’s bring all this together.

Read GTK Button to understand how to connect the button signal to a function.

#include<gtk/gtk.h>

GtkWidget *emailLabel, *emailEntry, *passwordLabel, *passwordEntry, *signupBtn, *grid;

void signup_button_clicked(GtkWidget *wid,gpointer data)
 {
      const gchar *emailData = gtk_entry_get_text(GTK_ENTRY(emailEntry)); 
      gtk_label_set_text(GTK_LABEL(data),emailData); 
      gtk_entry_set_text(GTK_ENTRY(emailEntry),""); 
      gtk_entry_set_text(GTK_ENTRY(PasswordEntry),"");
 } 

static void activate (GtkApplication* app, gpointer user_data)
 {
     GtkWidget *window;
     window = gtk_application_window_new (app);
     gtk_window_set_title (GTK_WINDOW (window), "User Input");
     gtk_window_set_default_size (GTK_WINDOW (window), 500, 400);

     GtkWidget *showEmail; 
     emailLabel = gtk_label_new("Email:"); 
     emailEntry = gtk_entry_new(); 
     gtk_entry_set_placeholder_text(GTK_ENTRY(emailEntry),"Email");
     GIcon *icon; 
     GFile *path; 
     path = g_file_new_for_path("D:/path/emailicon.png"); 
     icon = g_file_icon_new(path); 
     gtk_entry_set_icon_from_gicon(GTK_ENTRY(emailEntry),GTK_ENTRY_ICON_PRIMARY,icon); 

     PasswordLabel = gtk_label_new("Password:");
     PasswordEntry = gtk_entry_new();
     gtk_entry_set_placeholder_text(GTK_ENTRY(PasswordEntry),"Password");
     gtk_entry_set_visibility(GTK_ENTRY(PasswordEntry),FALSE);
     signupBtn = gtk_button_new_with_label("Sign Up");
     showEmail = gtk_label_new("");

     g_signal_connect(signupBtn,"clicked",G_CALLBACK(signup_button_clicked),showEmail);

     GtkWidget *box; box = gtk_box_new(GTK_ORIENTATION_VERTICAL,20);
     gtk_box_pack_start(GTK_BOX(box),emailLabel,FALSE,FALSE,0);
     gtk_box_pack_start(GTK_BOX(box),emailEntry,FALSE,FALSE,0);
     gtk_box_pack_start(GTK_BOX(box),PasswordLabel,FALSE,FALSE,0);
     gtk_box_pack_start(GTK_BOX(box),PasswordEntry,FALSE,FALSE,0); 
     gtk_box_pack_start(GTK_BOX(box),signupBtn,FALSE,FALSE,0); 
     gtk_box_pack_start(GTK_BOX(box),showEmail,FALSE,FALSE,0); 

     gtk_container_add(GTK_CONTAINER(window),box); 
     gtk_widget_show_all (window);
 } 


 int main(int argc,char **argv)
 {
     GtkApplication *app;
     int status;
     app = gtk_application_new ("com.hackthedeveloper", G_APPLICATION_FLAGS_NONE);
     g_signal_connect (app, "activate", G_CALLBACK(activate), NULL);
     status = g_application_run(G_APPLICATION(app), argc, argv);
     g_object_unref (app);
     return status;
 }

There are two entry fields in the above code, email, and password. The password entry field’s visibility is set to FALSE.

When the user will fill the details and click the sign-up button, the email will be displayed below the sign-up button, and both the entry fields text will be set to NULL.

Output:

GTK Entry - User Input
GTK Entry – User Input

Source:

About Author
3.7 3 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
Scroll to Top