Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to create a password entry field using Tkinter?
When creating user interfaces with Tkinter, you often need a secure password entry field that hides the user's input. Tkinter's Entry widget provides a simple solution using the show parameter to mask password characters.
Creating a Basic Password Field
The key to creating a password field is using the show parameter in the Entry widget. This parameter specifies which character to display instead of the actual input ?
from tkinter import *
# Create an instance of tkinter frame
win = Tk()
# Set the geometry of frame
win.geometry("600x250")
win.title("Password Entry Example")
def close_win():
win.destroy()
# Create a text label
Label(win, text="Enter the Password", font=('Helvetica', 20)).pack(pady=20)
# Create Entry Widget for password
password = Entry(win, show="*", width=20)
password.pack()
# Create a button to close the window
Button(win, text="Quit", font=('Helvetica bold', 10), command=close_win).pack(pady=20)
win.mainloop()
How It Works
The show="*" parameter tells the Entry widget to display an asterisk (*) for each character typed. You can use any character instead of "*" ?
from tkinter import *
win = Tk()
win.geometry("400x300")
# Different masking characters
Label(win, text="Asterisk Password:").pack(pady=5)
Entry(win, show="*", width=20).pack(pady=5)
Label(win, text="Dot Password:").pack(pady=5)
Entry(win, show="?", width=20).pack(pady=5)
Label(win, text="Hash Password:").pack(pady=5)
Entry(win, show="#", width=20).pack(pady=5)
win.mainloop()
Getting Password Value
To retrieve the password value, use the get() method on the Entry widget ?
from tkinter import *
win = Tk()
win.geometry("400x200")
def show_password():
password_value = password_entry.get()
result_label.config(text=f"Password: {password_value}")
Label(win, text="Enter Password:").pack(pady=10)
password_entry = Entry(win, show="*", width=25)
password_entry.pack(pady=5)
Button(win, text="Show Password", command=show_password).pack(pady=10)
result_label = Label(win, text="")
result_label.pack()
win.mainloop()
Key Parameters
| Parameter | Description | Example |
|---|---|---|
show |
Character to display instead of input | show="*" |
width |
Width of the entry field | width=20 |
font |
Font style and size | font=('Arial', 12) |
Conclusion
Use the show parameter in Tkinter's Entry widget to create secure password fields. The get() method retrieves the actual password value while keeping the display masked for security.
