【发布时间】:2022-01-21 03:50:48
【问题描述】:
我想了解按钮是如何使用 lambda 工作的。 我有以下 Python 代码:
from tkinter import *
def comando_click(mensagem):
print(mensagem)
menu_inicial = Tk()
menu_inicial.geometry("500x250+200+200")
botao = Button(menu_inicial, text = "Executar", command=comando_click("Nova_Mensagem"))
botao.pack()
menu_inicial.mainloop()
但是当我点击它时我的按钮不起作用,当我运行代码时它只在控制台中显示一次打印,我在问题中添加了一些打印:
好吧,当我在按钮中使用 Lambda 函数时,它似乎可以工作,我真的很想知道为什么。
Lambda working button Picture one
我刚刚在按钮上添加了 lambda:
botao = Button(menu_inicial, text = "Executar", command=lambda:comando_click("Nova_Mensagem"))
Lambda working button Picture two
为什么使用 lambda 它可以工作? 既然 lambda 基本上是一个匿名函数,它也不应该没有 lambda 工作?
我非常想知道它为什么会起作用,谢谢大家的帮助:)
编辑:我要感谢你们,现在我终于明白发生了什么以及 Python 是如何工作的。非常感谢大家:D
【问题讨论】:
-
你可能会发现helpful
标签: python python-3.x tkinter tk tkinter-button