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'
CodePudding user response:
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()