【问题标题】:How to pass argument to function while binding it to a button如何在将参数绑定到按钮时将参数传递给函数
【发布时间】: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_charsshow_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("&lt;ButtonPress-1&gt;", lambda event: show_chars(password))

标签: python-3.x tkinter button


【解决方案1】:

试试这个:

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

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

你也可以使用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 的更多信息,请阅读this

【讨论】:

  • 你为什么把event放在lambda后面?在对this question 的回答中,他们没有在 lambda 之后添加任何内容。
  • 因为 tkinter 在调用函数时总是传递一个事件参数。 'lambda's 基本上是函数,: 之前的东西是参数。
  • @Manav 你可以避免这种情况,但你必须定义一个参数event 或其他东西。
猜你喜欢
  • 2021-02-14
  • 1970-01-01
  • 1970-01-01
  • 2018-06-21
  • 2019-04-01
  • 2022-01-19
  • 2019-09-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多