• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

python - Why is the command bound to a Button or event executed when declared?

[复制链接]
菜鸟教程小白 发表于 2022-4-10 16:03:35 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

My code is:

from Tkinter import *

admin = Tk()
def button(an):
    print an
    print 'het'

b = Button(admin, text='as', command=button('hey'))
b.pack()
mainloop()

The button doesn't work, it prints 'hey' and 'het' once without my command, and then, when I press the button nothing happens.



Best Answer-推荐答案


Consider this code:

b = Button(admin, text='as', command=button('hey'))

It does exactly the same as this:

result = button('hey')
b = button(admin, text='as', command=result)

Likewise, if you create a binding like this:

listbox.bind("<<ListboxSelect>>", some_function())

... it's the same as this:

result = some_function()
listbox.bind("<<ListboxSelect>>", result)

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:

b = Button(... command = button)

If you want to pass a parameter such as "hey" you must use a little extra code:

  • You can create an intermediate function that can be called without your argument and which then calls your button function,
  • You can use 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 the lambda command it returns a reference to the created function, which means it can be used for the value of the command option to the button.
  • You can use functools.partial

For me, lambda is the simplest since it doesn't require any additional imports like functools.partial does, though some people think that functools.partial is easier to understand.

To create a lambda function that calls your button function with an argument you would do something like this:

lambda: button('hey')

You end up with a function that is functionally equivalent to:

def some_name():
    return button('hey')

As I said earlier, lambda returns a reference to this nameless function. Since a reference is what the command option expects you can use lambda directly in the creation of the button:

b = Button(... command = lambda: button('hey'))

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.

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap