Computer >> Computer tutorials >  >> Programming >> Python

How to create a Password Entry Field in Tkinter?


Suppose you are creating a Login Form for a Tkinter application. In many cases, an ideal login requires a standard format of username, password, and other details of the user. Users can enter the password in the Entry field with any combination of alphanumeric characters. Generally, to establish a secure bridge between the user and the application, the password fields store the input in the form of "*" characters. In order to create a field that accepts the input in the form of "*," we have to use show="*" attribute in the Entry widget.

Example

The following example will have an Entry widget that accepts the password in hidden form. If we click "Show Password", it will show the password on the screen.

# Import the required libraries
from tkinter import *
from tkinter import ttk

# Create an instance of tkinter frame or window
win = Tk()

# Set the size of the window
win.geometry("700x350")

# Define a function to show the entered password
def show():
   p = password.get()
   ttk.Label(win, text="Your Password is: " + str(p)).pack()

password = StringVar()

# Add an Entry widget for accepting User Password
entry = Entry(win, width=25, textvariable=password, show="*")
entry.pack(pady=10)

# Add a Button to reveal the password
ttk.Button(win, text="Show Password", command=show).pack()

win.mainloop()

Output

Running the above code will display a password field and a button to reveal the password on the screen.

How to create a Password Entry Field in Tkinter?

Now, click the "Show Password" button to reveal the password on the screen.

How to create a Password Entry Field in Tkinter?