How to create a password entry field using Tkinter

PythonTkinter

Python Problem Overview


I am trying to code a login window using Tkinter but I'm not able to hide the password text in asterisk format. This means the password entry is plain text, which has to be avoided. Any idea how to do it?

Python Solutions


Solution 1 - Python

A quick google search yielded this

widget = Entry(parent, show="*", width=15)

where widget is the text field, parent is the parent widget (a window, a frame, whatever), show is the character to echo (that is the character shown in the Entry) and width is the widget's width.

Solution 2 - Python

If you don't want to create a brand new Entry widget, you can do this:

myEntry.config(show="*");

To make it back to normal again, do this:

myEntry.config(show="");

I discovered this by examining the previous answer, and using the help function in the Python interpreter (e.g. help(tkinter.Entry) after importing (from scanning the documentation there). I admit I just guessed to figure out how to make it normal again.

Solution 3 - Python

widget-name = Entry(parent,show="*")

You can also use a bullet symbol:

bullet = "\u2022" #specifies bullet character
widget-name = Entry(parent,show=bullet)#shows the character bullet

Solution 4 - Python

Here's a small, extremely simple demo app hiding and fetching the password using Tkinter.

#Python 3.4 (For 2.7 change tkinter to Tkinter)
    
from tkinter import * 
        
def show():
    p = password.get() #get password from entry
    print(p)
        
    
app = Tk()   
password = StringVar() #Password variable
passEntry = Entry(app, textvariable=password, show='*')
submit = Button(app, text='Show Console',command=show)

passEntry.pack() 
submit.pack()      

app.mainloop() 

Hope that helps!

Solution 5 - Python

I was looking for this possibility myself. But the immediate "hiding" of the entry did not satisfy me. The solution I found in the modification of a tk.Entry, whereby the delayed hiding of the input is possible:

PassEntry

Basically the input with delay is deleted and replaced

        def hide(index: int, lchar: int):
            i = self.index(INSERT)
            for j in range(lchar):
                self._delete(index + j, index + 1 + j)
                self._insert(index + j, self.show)
            self.icursor(i)

and the keystrokes are written into a separate variable.

    def _char(self, event) -> str:
        def del_mkey():
            i = self.index(INSERT)
            self._delete(i - 1, i)

        if event.keysym in ('Delete', 'BackSpace'):
            return ""
        elif event.keysym == "Multi_key" and len(event.char) == 2:  # windows stuff
            if event.char[0] == event.char[1]:
                self.after(10, del_mkey)
                return event.char[0]
            return event.char
        elif event.char != '\\' and '\\' in f"{event.char=}":
            return ""
        elif event.num in (1, 2, 3):
            return ""
        elif event.state in self._states:
            return event.char
        return ""

Look for PassEntry.py if this method suits you.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionHickView Question on Stackoverflow
Solution 1 - PythonFederico klez CullocaView Answer on Stackoverflow
Solution 2 - PythonBrōtsyorfuzthrāxView Answer on Stackoverflow
Solution 3 - PythonNathan GreenView Answer on Stackoverflow
Solution 4 - PythonkarlzafirisView Answer on Stackoverflow
Solution 5 - PythonsrccircumflexView Answer on Stackoverflow