Reputation: 29
I have a simple program that makes two password boxes and two buttons that should show and unshow the text in their respective entry boxes. This is my current code:
import tkinter as tk
def show_stars(event):
password.config(show="*")
def show_chars(event):
password.config(show="")
root = tk.Tk()
root.title("Password")
password = tk.Entry(root, show="*")
password2 = tk.Entry(root, show="*")
password.grid(row=0, column=1)
password2.grid(row=1, column=1)
password_label = tk.Label(text="Confirm Password:")
password_label.grid(row = 1, column=0)
password_label = tk.Label(text="Password:")
password_label.grid(row = 0, column=0)
button = tk.Button(text = '.')
button.bind("<ButtonPress-1>", show_chars)
button.bind("<ButtonRelease-1>", show_stars)
button.grid(row=0, column=2)
button2 = tk.Button(text = '.')
button2.bind("<ButtonPress-1>", show_chars)
button2.bind("<ButtonRelease-1>", show_stars)
button2.grid(row=1, column=2)
I want to make it so that I can pass an argument to the show_chars
and show_stars
function like this:
def show_stars(entry_box):
entry_box.config(show="*")
def show_chars(entry_box):
entry_box.config(show="")
button.bind("<ButtonPress-1>", show_chars(password))
button.bind("<ButtonRelease-1>", show_stars(password))
button2.bind("<ButtonPress-1>", show_chars(password2))
button2.bind("<ButtonRelease-1>", show_stars(password2))
So that it does not show and unshow the same entry box. Is there any way I can do this?
Upvotes: 1
Views: 43
Reputation: 7710
Try this:
import tkinter as tk
def show_stars(entry_box):
entry_box.config(show="*")
def show_chars(entry_box):
entry_box.config(show="")
root = tk.Tk()
password = tk.Entry(root, show="*")
button = tk.Button(root, text="Show password")
password2 = tk.Entry(root, show="*")
button2 = tk.Button(root, text="Show password")
password.grid(row=1, column=1)
button.grid(row=1, column=2)
password2.grid(row=2, column=1)
button2.grid(row=2, column=2)
button.bind("<ButtonPress-1>", lambda event: show_chars(password))
button.bind("<ButtonRelease-1>", lambda event: show_stars(password))
button2.bind("<ButtonPress-1>", lambda event: show_chars(password2))
button2.bind("<ButtonRelease-1>", lambda event: show_stars(password2))
root.mainloop()
For more info on how to use lambda
in python read this
You can also use functools.partial
:
import tkinter as tk
from functools import partial
def show_stars(entry_box, event):
entry_box.config(show="*")
def show_chars(entry_box, event):
entry_box.config(show="")
root = tk.Tk()
password = tk.Entry(root, show="*")
button = tk.Button(root, text="Show password")
password2 = tk.Entry(root, show="*")
button2 = tk.Button(root, text="Show password")
password.grid(row=1, column=1)
button.grid(row=1, column=2)
password2.grid(row=2, column=1)
button2.grid(row=2, column=2)
command = partial(show_chars, password)
button.bind("<ButtonPress-1>", command)
command = partial(show_stars, password)
button.bind("<ButtonRelease-1>", command)
command = partial(show_chars, password2)
button2.bind("<ButtonPress-1>", command)
command = partial(show_stars, password2)
button2.bind("<ButtonRelease-1>", command)
root.mainloop()
For more info on functools.partial
read this.
Upvotes: 1