Tkinter Entry – Password

Tkinter Entry Password

In Tkinter, you can define Entry widget to read password from the user, such that only dots or asterisk symbol is displayed when user types password.

Tkinter Entry Password
Tkinter Entry widget to read password from user

To define a Tkinter Entry widget to read password from user, create Entry widget using Entry class constructor, and pass the asterisk symbol as string for show parameter.

tk.Entry(window, show="*")

This makes the Entry widget to display the input as asterisks or dots. This provides a secure way to handle password input from the user.

In this tutorial, you will learn how to use Tkinter Entry widget to read password from user, with examples.

Examples

1. Read password from user

In this example, we create a basic Tk window in which we request user to enter the password. When user types in the password, we display asterisk or dot symbols.

After entering the password, the user clicks on the Show Password button. We shall get the value entered in the Entry widget programmatically, and display it in the terminal output.

Python Program

import tkinter as tk

# Create the main window
window = tk.Tk()
window.title("PythonExamples.org")
window.geometry("300x200")

# Function to retrieve the password
def get_password():
    password = entry.get()
    print("Password:", password)

label = tk.Label(window, text="Enter Password")
label.pack()

# Create a password entry field
entry = tk.Entry(window, show="*")
entry.pack()

# Create a button to retrieve the password
button = tk.Button(window, text="Show Password", command=get_password)
button.pack()

# Run the application
window.mainloop()

Output

In Windows

In MacOS

Tkinter Entry widget to read password from user
Tkinter Entry widget to read password from user

Click on the Show Password button.

Tkinter Entry – Password
Terminal output – Prints password

Summary

In this Python Tkinter tutorial, we learned how to use Tkinter Entry widget to read Password from user, with examples.

Code copied to clipboard successfully 👍