Show password button on entry in gtk

Viewed 156

Is there a way in gtk to make an eye icon on the right of an entry to show a password? I know there is a way to make a checkbox under an entry to show the password, but I want the button to be inside the entry, not outside. To put it simply:

What i'm looking for is this: show password icon

...and not this: show password checkbox

(I'm sorry that I cant embed the image. The site says I need 10 reputation first. I actually used to have almost 300 untill I got banned from a dislike attack. Please think before you dislike because it literally only takes ~20 to permenantly ban a year-long user. If anyone has enough reputation to edit the question and add the images in, please do)

1 Answers

You can put an icon to Gtk.Entry with the set_icon_from_ functions, for example set_icon_from_name.

So you need to set the icon to for example to the view-reveal-symbolic.symbolic icon, make it clickable with set_icon_activatable and then in the signal handler for the icon-press event you need to set_visibility to either hide or show the text (and also change the icon to something like view-conceal-symbolic.symbolic).

So the Gtk.Entry code could look like this

GtkWidget *entry = gtk_entry_new();
gtk_entry_set_visibility(GTK_ENTRY(entry), FALSE);
gtk_entry_set_icon_from_icon_name(GTK_ENTRY(entry), 
                                  GTK_ENTRY_ICON_SECONDARY, 
                                  "view-reveal-symbolic.symbolic");
gtk_entry_set_icon_activatable(GTK_ENTRY(entry), GTK_ENTRY_ICON_SECONDARY, TRUE);
g_signal_connect(entry, "icon-press", G_CALLBACK (on_icon_press), NULL);

and the signal handler

void on_icon_press(GtkWidget *widget, gpointer data) {   
    gboolean visible = gtk_entry_get_visibility(GTK_ENTRY(widget));
    
    if (visible) {
        gtk_entry_set_visibility(GTK_ENTRY(widget), FALSE);
        gtk_entry_set_icon_from_icon_name(GTK_ENTRY(widget), 
                                          GTK_ENTRY_ICON_SECONDARY, 
                                          "view-reveal-symbolic.symbolic");
    } else {
        gtk_entry_set_visibility(GTK_ENTRY(widget), TRUE);
        gtk_entry_set_icon_from_icon_name(GTK_ENTRY(widget), 
                                          GTK_ENTRY_ICON_SECONDARY, 
                                          "view-conceal-symbolic.symbolic");
    }
}

And you'll get something like this:

enter image description here enter image description here

Related