0

I'm learning Python3 and tkinter. I was trying to show password with binding <Button> and hide password with binding <ButtonRelease>, but I didn't have any solution. All I can do is to show the password, then the error occurred:

Here is my code:

import tkinter as tk

def show(e):
    passwd_entry.config(show="")
# def hide(event):
#     passwd_entry.config(show="*")
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=show)
toggle_btn.pack(side=tk.LEFT)
toggle_btn.bind("<Button>", show)
# toggle_btn.bind("<ButtonRelease>", hide)

root.mainloop()

This is the error when I click button:

TypeError: show() missing 1 required positional argument: 'e'
Cristian
  • 155
  • 8

1 Answers1

0

The tkinter instance creation needs to happen before the function definition event which you want to call.

Also use the lambda function to call the show function inside bind as mentioned in below code. It should help.

import tkinter as tk

root = tk.Tk()

def show():
    passwd_entry.config(show="")
def hide():
    passwd_entry.config(show="*")

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

toggle_btn = tk.Button(root, text='Show Password', width=15)
toggle_btn.pack(side=tk.LEFT)
toggle_btn.bind("<ButtonPress>", lambda event:show())
toggle_btn.bind("<ButtonRelease>", lambda event:hide())

root.mainloop()
Abhi
  • 1,080
  • 1
  • 7
  • 21
  • I appreciate your help, but this makes the password visible and cannot make invisible anymore. I'm trying to make the password visible when left-click and make it invisible when releasing left-click. – Cristian Mar 05 '22 at 13:59
  • Updated the code, please check, now you can show or hide the password with any click be it right click or left click :) Please accept the answer and upvote if it helps :) – Abhi Mar 05 '22 at 14:27