When typing can you change what the text is displayed as?

Question:

So i am making a program to test a password and was wondering, When the user enters their password can you stop their password from showing as they type it in to the console/GUI?

Asked By: 0llie_C

||

Answers:

Python has you covered with a built-in library:

>>> import getpass
>>> pw = getpass.getpass()

Edit: As mentioned by @MattH, this won’t echo asterisks (*) while user types the password.

Answered By: santosh-patil
import tkinter as tk 

root = tk.Tk()

def store():
    pw = my_box.get()

my_box = tk.Entry(root, show='*')
my_box.pack()
Button(text='Submit', command=store).pack()

root.mainloop()

This will work in a GUI. You can then call pw = my_box.get() where necessary.


You can hide user input in the console using the getpass module- but this only hides and does not display asterisks.

>>> import getpass
>>> pw = getpass.getpass()

But displaying asterisks in the console is a little more complicated; you would need to use sys and msvcrt– Google it or ask a separate question asking how to use these modules for that purpose; I don’t know.

Answered By: anon582847382
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.