【发布时间】:2021-03-07 13:15:47
【问题描述】:
我有一个简单的程序,它制作了两个密码框和两个按钮,它们应该在各自的输入框中显示和取消显示文本。 这是我当前的代码:
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)
我想这样做,以便我可以像这样将参数传递给 show_chars 和 show_stars 函数:
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))
这样它就不会显示和取消显示相同的输入框。有什么办法可以做到吗?
【问题讨论】:
-
试试:
button.bind("<ButtonPress-1>", lambda event: show_chars(password))
标签: python-3.x tkinter button