调用带有“command"参数的函数和“绑定" [英] Calling functions with arguments on "command" and "bind"
问题描述
我想指出的是,我从很短的时间就开始学习 Python.问题将是初学者之一.
i want to point, that i am learning python since short time. The question is going be to beginner one.
我需要在程序顶部的菜单中添加命令,该命令会调用函数color_picker("red").
I need to add command to menu at top of program, which would call function "color_picker("red").
kolory.add_command(label="Czerwony", command=color_picker('red'))
当我使用它时,它不知何故错了,因为一旦程序启动它就会被调用,而不是等待我单击菜单按钮.(我确信这一点,因为我向该函数添加了showinfo",并且在我执行任何操作之前它会显示消息)kolory.add_command(label="Czerwony", command=lambda: color_picker('red'))
那个有点用,但我不知道lambda"在这里是什么意思.它是在菜单选项下使用参数调用函数的唯一方法吗?
kolory.add_command(label="Czerwony", command=color_picker('red'))
When i am using that, its somehow wrong, cuz its called once the program started, its not waiting for me to click the menu button. (i am sure of it, as i added "showinfo" to that function, and it shows the message before i do anything)
kolory.add_command(label="Czerwony", command=lambda: color_picker('red'))
That one kinda works, but i don't know what does "lambda" mean here. Is it only way to call functions with arguments under menu options?
同样的问题涉及绑定键盘快捷键.okno.bind("1", color_picker)
- 将调用函数但没有参数,参数应该是颜色.我该怎么做?
Same question goes to binding keyboard shortcuts.
okno.bind("1", color_picker)
- that will call the function but does not have the argument, which should be a color. How can i do that?
那么,如何使用 add_command 将带参数的函数分配给键盘快捷键和菜单?
So, how to assign functions WITH arguments, to keyboard shortcuts and to menu using add_command?
附注.正在通过谷歌搜索,但似乎 python 没有像 c# 那样好的文档.或者我太笨了找不到.
PS. Was searching trough google, but it seems python does not have so good documentation like c# does for example. Or i am too stupid to find it.
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python27\lib\lib-tk\Tkinter.py", line 1410, in __call__
return self.func(*args)
TypeError: color_picker() takes at most 1 argument (2 given)
这是错误信息,当我尝试在 okno.bind 中使用pick_red"
Thats the error message, when i try to use "pick_red" in okno.bind
推荐答案
我不确定我是否理解这个问题,但在这里;
I'm not sure if I understand the question, but here goes;
问题在于您正在调用color_picker
函数(通过在函数名称后添加()
).
The problem is that you are calling the color_picker
function (by adding ()
after the function name).
你想要做的是传递实际的函数,而不是函数调用的结果作为command
关键字参数,例如add_command(label="Czerwony", command=color_picker)
What you want to do is pass the actual function, not the result of the function call as the command
keyword argument, e.g. add_command(label="Czerwony", command=color_picker)
然而,因为你想给它一个固定的参数 'red'
,你必须使用 partial 来自 functools
,类似;
However, since you want to give it a fixed argument 'red'
, you must use partial from functools
, something like;
from functools import partial
pick_red = partial(color_picker, "red")
kolory.add_command(label="Czerwony", command=pick_red)
编辑:
既然您的错误消息显示您正在使用 Tkinter
,我们可以根据 documentation 给 bind()
的函数总是传递一个 event
参数,所以你需要一个可以接受的函数;
Now that your error message shows that you are using Tkinter
, we can see that according to documentation the function that is given to bind()
is always passed an event
parameter, so you need a function that can accept it;
def pick_red_with_event(event):
# We really do nothing with event for now but we always get it...
color_picker("red")
okno.bind("1", pick_red_with_event)
<打击>okno.bind
同样适用,如果您已经定义了 pick_red
如上所述,只需执行:
Same thing works for okno.bind
, if you have defined pick_red
as above, just do:
okno.bind("1", pick_red)
这篇关于调用带有“command"参数的函数和“绑定"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!