问题描述
当我想添加一个按钮时
b = Button(app_name,text="..." , command = any_function)
是否可以使用带有类似参数的函数
is it possible to use function with a parameter like
b = Button(app_name, text="..." , command = any_function(7))
我试过了
(...)
def cash(price):
global total
total.set(total.get() + price)
total = IntVar()
total.set(0)
b1 = Button(app,text = "one ",width = 10,command = cash(1))
b1.pack()
b2 = Button(app,text = "two ",width = 10,command = cash(10))
b2.pack()
b3 = Button(app,text = "three ",width = 10,command = cash(100))
b3.pack()
b4 = Button(app,text = "four ",width = 10,command = cash(1000))
b4.pack()
l_total = Label(app,textvariable = total)
l_total.pack()
(...)
但是 l_total
在程序运行时(1000+100+10+1)已经为 1111,就像我按下了四个按钮一样,而且按钮也没有向 l_total
.我只想知道为什么它不起作用,因为我知道一个解决方案.
But l_total
already as 1111 when the program run ( 1000+100+10+1) like I had pressed the four buttons and also the buttons doesnt add values to l_total
. I just want to know why it doesnt work as I know a solution .
推荐答案
您所写的内容:
b = Button(app_name, text="..." , command = any_function(7))
...不是传递函数作为command
,而是调用一个函数,然后传递它的结果 作为 command
.
… isn't passing a function as the command
, it's calling a function, then passing its result as the command
.
您需要做的是定义一个新函数,该函数在不带参数的情况下调用时会调用any_function(7)
.像这样:
What you need to do is to define a new function that, when called with no arguments, calls any_function(7)
. Like this:
def any_function_7():
return any_function(7)
b = Button(app_name, text="..." , command = any_function_7)
或者,使用较短的lambda
定义函数的语法:
Or, using the shorter lambda
syntax for defining functions:
b = Button(app_name, text="..." , command = lambda: any_function(7))
将表达式包装在这样的函数中,然后传递该函数,是一种将函数推迟到以后执行的方法.可能很难理解,但一旦理解,它就会在所有地方都非常有用.
Wrapping an expression up in a function like this, then passing the function around, is a way of deferring the function until later. It can be hard to get your head around, but once you do, it's incredibly useful all over the place.
另一种看待它的方式是部分应用该功能,它为您提供了一个您可以在以后完成应用的东西.有一个 partial
函数在为您执行此操作的标准库:
Another way to look at it is partially applying the function, which gives you a thing that you can finish applying later on. There's a partial
function in the standard library that does this for you:
from functools import partial
b = Button(app_name, text="..." , command = partial(any_function, 7))
但你也可以自己做:
def make_any_function_partial(i):
def wrapped():
return any_function(i)
return wrapped
b = Button(app_name, text="..." , command = make_any_function_partial(7))
然后你可以看到这与使用 def
或 lambda
的技巧是一样的,再加上另一个技巧:它是一个返回一个功能.因此,虽然您可能正在调用 make_any_function_partial
并将其结果传递给 command
,就像您最初的尝试一样,不同之处在于结果本身就是一个函数.
And then you can see how this is really just the same trick as using def
or lambda
, plus another trick on top of it: it's a function that returns a function. So, while you may be calling make_any_function_partial
and passing its result to command
, just as in your original attempt, the difference is that the result is itself a function.
这篇关于tkinter , python , 按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!