如何在将函数绑定到按钮时将参数传递给函数

马纳夫

我有一个简单的程序,它制作两个密码框和两个按钮,它们应该在各自的输入框中显示和取消显示文本。这是我当前的代码:

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_charsandshow_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))

这样它就不会显示和取消显示相同的输入框。有什么办法可以做到这一点吗?

蜥蜴

试试这个:

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()

有关如何lambda在 python 中使用的更多信息,请阅读本文

您还可以使用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()

有关functools.partial阅读此内容的更多信息

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章