为什么当我创建按钮时,我的按钮立即执行我的按钮,而不是在我单击它时立即执行?
我的代码是:
from Tkinter import *
admin = Tk()
def button(an):
print(an)
print('het')
b = Button(admin, text='as', command=button('hey'))
b.pack()
mainloop()
该按钮不起作用,它在没有我的命令的情况下一次打印“嘿”和“ het”,然后,当我按下按钮时,什么也不会发生。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
考虑以下代码:
它与此相同:
同样,如果您创建这样的绑定:
相同:
...与此 一种奇特的方式,说您需要将其传递给功能的名称。要传递参考,您必须仅使用名称,而无需使用括号或参数。例如:
如果要传递诸如“嘿”之类的参数,则必须使用一些额外的代码:
button
函数,lambda
创建称为匿名函数的内容。在各个方面,它是一个功能,除了没有名称。当您调用lambda
命令时,它将A 参考返回到创建的函数,这意味着它可用于命令
选项的值按钮。对我来说,
lambda
是最简单的,因为它不需要任何其他导入,例如functials.partial
确实如此,尽管有些人认为functials.partials.partial
更容易理解。要创建一个lambda函数,该函数调用您的
button
函数,您将执行这样的操作:您最终以功能上等效的函数与:
正如我之前说的那样,
lambda 返回对此无名功能的引用。由于引用是
命令
选项期望您可以直接在按钮的创建中使用lambda
:该站点上有一个问题,在lambda上有很多有趣的评论, 一般来说。请参阅问题为什么Python Lambdas有用?。同样的讨论具有一个答案将变量传递给回调。
最后,请参阅标题为 lambda 的覆盖范围非常精简,但是那里的信息仍然有用。
Consider this code:
It does exactly the same as this:
Likewise, if you create a binding like this:
... it's the same as this:
The
command
option takes a reference to a function, which is a fancy way of saying you need to pass it the name of the function. To pass a reference you must use the name only, without using parenthesis or arguments. For example:If you want to pass a parameter such as "hey" you must use a little extra code:
button
function,lambda
to create what is referred to as an anonymous function. In every way it's a function except it doesn't have a name. When you call thelambda
command it returns a reference to the created function, which means it can be used for the value of thecommand
option to the button.For me,
lambda
is the simplest since it doesn't require any additional imports likefunctools.partial
does, though some people think thatfunctools.partial
is easier to understand.To create a lambda function that calls your
button
function with an argument you would do something like this:You end up with a function that is functionally equivalent to:
As I said earlier,
lambda
returns a reference to this nameless function. Since a reference is what thecommand
option expects you can uselambda
directly in the creation of the button:There's a question on this site that has a lot of interesting comments about lambda, in general. See the question Why Python lambdas are useful?. That same discussion has an answer that shows how to use lambdas in a loop when you need to pass in a variable to the callback.
Finally, see the zone.effbot.org article titled Tkinter Callbacks for a nice tutorial. The coverage of
lambda
is pretty lean, but the information there might still be useful.您需要创建一个无参数的函数,您可以用作命令:
请参阅此文档。
You need to create a function without parameters that you can use as the command:
See the "Passing Argument to Callbacks" section of this document.
GUI示例:
假设我有GUI:
按下按钮时会发生什么,
请参见
btn
按下时,它调用其自己的函数,与非常相似button_press_handle
在下面的示例中:使用:
您可以简单地认为应将
命令
选项设置为我们要称为的方法的引用,类似于callback 在
button_press_handle
中。调用方法(a callback
)没有参数,
因此,如果我想
打印
按下按钮时,我需要设置:请密切注意 的 code>()带有
print
方法,该方法省略了: “这是我希望您在按下时要调用但不要只称其为即时。” ,但是,我没有对print
进行任何争论,因此它在没有参数的情况下打印了任何打印的内容。参数(s)
使用 使用匿名函数的使用,可以用 lambda 语句,在这种情况下,用于
print
内置方法,如以下内容:calle 多个方法按下按钮
没有参数 /strong>
您还可以使用
lambda
语句实现这一目标,但是它被认为是不良练习,因此我不会在此处包含它。良好的做法是定义一个单独的方法protive_methods
,该方法调用所需的方法,然后将其设置为“回调”按钮:with )
为了将参数传递到调用其他方法的方法,再次使用
lambda
语句,但首先:设置:
从回调中返回对象(S)
然后 进一步注意,
回调
无法真正返回
,因为它仅在button_press_handle
callback()中,而不是在button_press_handle
返回回调()
。它确实返回
,但不是该功能以外的任何地方。因此,您应该宁愿在当前范围内访问对象。完整的示例 global 以下示例对象修改(s)
将调用一个方法,该方法每次按下按钮时都会更改
btn
的文本:镜子
Example GUI:
Let's say I have the GUI:
What Happens When a Button Is Pressed
See that when
btn
is pressed it calls its own function which is very similar tobutton_press_handle
in the following example:with:
You can simply think that
command
option should be set as, the reference to the method we want to be called, similar tocallback
inbutton_press_handle
.Calling a Method (a Callback) When the Button is Pressed
Without arguments
So if I wanted to
print
something when the button is pressed I would need to set:Pay close attention to the lack of
()
with theprint
method which is omitted in the meaning that: "This is the method's name which I want you to call when pressed but don't call it just this very instant." However, I didn't pass any arguments for theprint
so it printed whatever it prints when called without arguments.With Argument(s)
Now If I wanted to also pass arguments to the method I want to be called when the button is pressed I could make use of the anonymous functions, which can be created with lambda statement, in this case for
print
built-in method, like the following:Calling Multiple Methods when the Button Is Pressed
Without Arguments
You can also achieve that using
lambda
statement but it is considered bad practice and thus I won't include it here. The good practice is to define a separate method,multiple_methods
, that calls the methods wanted and then set it as the callback to the button press:With Argument(s)
In order to pass argument(s) to method that calls other methods, again make use of
lambda
statement, but first:and then set:
Returning Object(s) From the Callback
Also further note that
callback
can't reallyreturn
because it's only called insidebutton_press_handle
withcallback()
as opposed toreturn callback()
. It doesreturn
but not anywhere outside that function. Thus you should rather modify object(s) that are accessible in the current scope.Complete Example with global Object Modification(s)
Below example will call a method that changes
btn
's text each time the button is pressed:Mirror
引擎评估函数在“ ...命令= ...”中分配值时,
“命令”期望返回函数,这就是为什么使用lambda可以完成工作的原因,因为它是在评估过程中创建一个返回“命令”的异源函数。
您也可以编码自己的功能,也可以完成工作。
这是Lambda和没有Lambda的一个例子:
The engine evaluates the result of the function when it is assigning the value at the line "... command = ..."
The "command" expects a function to be returned, that's why using a lambda can do the job because it is creating an anomymous function that is returned to the "command" during evaluation.
You can also code your own function, it will do the job also.
this is an example with lambda and without lambda:
我认为解决此问题的最佳方法是使用lambda功能。
如果您不想使用命令关键字,则可以使用.bind()方法:
使用母函数(无参数),该功能(无参数)您要调用的子函数(至少1个参数)是愚蠢的。
只是与您分享,这是我的计划之一:
就是这样。
I think the best way to solve this problem is to use a lambda function.
If you don't want to use the command keyword, you can use the .bind() method instead:
Using a mother function (no parameter) which owns the child function (at least 1 parameter) you want to call is stupid.
Just to share with you, this is one of my program:
That's it.