简体   繁体   中英

How to show and hide password in Tkinter with the help of button?

I want to make a show and hide button in Tkinter which on click make the password visible and on clicking hide it will hide the password. First of all, I have used an Entry widget to enter the password and with the help of (show="*"),I have made the password to be entered in * format which hides the password. But if I want to check what I have entered for that I need a show button and then again hide it after confirming.

You can use show='' to show the password and show='*' to hide the password:

import tkinter as tk

def toggle_password():
    if passwd_entry.cget('show') == '':
        passwd_entry.config(show='*')
        toggle_btn.config(text='Show Password')
    else:
        passwd_entry.config(show='')
        toggle_btn.config(text='Hide Password')

root = tk.Tk()

passwd_entry = tk.Entry(root, show='*', width=20)
passwd_entry.pack(side=tk.LEFT)

toggle_btn = tk.Button(root, text='Show Password', width=15, command=toggle_password)
toggle_btn.pack(side=tk.LEFT)

root.mainloop()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM