+ -
当前位置:首页 → 问答吧 → 关于lambda 在Button中的使用

关于lambda 在Button中的使用

时间:2010-12-08

来源:互联网

我在GUI应用中,很不明白这个lambda的作用,请帮忙解释一下:
def fetch(entries):
     print 'input =>"%s"' % entries.get()

   
popup=Toplevel()
Button(popup,text='OK',command=(lambda: fetch(ents))).pack()
popup.mainloop()
如果这样写程序运行没问题。能够打印出输入的值。
但如果改成Button(popup,text='OK',command=fetch).pack(),输出就为空了,也就是Button 的command 调用函数不起作用。
为什么呢?这里lambda:的作用是什么,请指教。

作者: vividvivion   发布时间: 2010-12-08

  1. lambda: fetch(ents)
复制代码
相当于
  1. def func:
  2.     return fetch(ents)
复制代码
而你直接command=fetch……fetch就没参数了……

作者: Kabie   发布时间: 2010-12-08

热门下载

更多