'How to make "view passwords"?

I'm making password recognition system, and I want to make "View Passwords" function, which can switch hide to show passwords. I have no idea how to make this function.

Here is my brief code:

from tkinter import *

root=Tk()

#Entry box to get password from users
getPassword = Entry(root, show='*').pack()

I am stuck here. I don't even know whether I should use Button() or other function such as checkbox to make "View Passwords" function.

Do you have any good idea to fix this?



Solution 1:[1]

You can use the universal widget methods cget() and config() to get and set the current show option of your Entry widgets to define a function to toggle the display of contents of the Entry widget(s). You can then make the function the call-back command of a Button. You could also use it with Radiobutton or ttk.Checkbutton widgets in a similar manner.

Here's a runnable example to illustrate what I'm saying:

import tkinter as tk

HIDE_CHAR = '*'

def toggle_password_display():
    show = HIDE_CHAR if not password_entry.cget('show') else ''
    password_entry.config(show=show)

root = tk.Tk()

# Entry box to get password from users.
password_entry = tk.Entry(root, show=HIDE_CHAR)
password_entry.pack()

toggle_btn = tk.Button(root, text='Toggle password display', command=toggle_password_display)
toggle_btn.pack()

root.mainloop()

Before and after clicking the Button:

screenshot of code running

Solution 2:[2]

usage

    Password_entry.config('*')

to make the password invisible
And

    Password_entry.config('')

to make the password visible

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Sytze