我正在使用python 2.7试验pygtk程序开发,我在使用lambda函数连接信号时遇到一些问题:
所以,这是我的问题: 我不明白为什么:
connect("A_Signal", lambda <signal_parameters> : instance_b.manage_signal)
我们甚至无法输入instance_b的函数manage_signal(例如:connect("activate", lambda menu_item : instance_b.manage_signal)
connect("A_Signal", lambda <signal_parameters> : self.manage_signal)
=&gt;这次有效吗?这里有些代码更明确
class A_Class(gtk.Window):
def __init__(self):
super(gtk.Window, self).__init__()
self.__instance_b = B_Class()
# this does not work and i don't understand why
self.connect("delete-event", lambda the_widget,the_event: self.__instance_b.manage_event)
# this works
self.connect("delete-event", self.__manage_event)
def __manage_event(self):
self.__instance_b.manage_event
class B_Class(object):
def manage_event(self):
# do some stuffs ...
此致
答案 0 :(得分:1)
你的lambda表达式应该调用处理函数而不是返回它:
self.connect("delete-event", lambda the_widget, the_event: \
self.__instance_b.manage_event(the_widget, the_event))
当然,正如你所说,这是不必要的,因为它等同于:
self.connect("delete-event", self.__instance_b.manage_event)
lambda最终只会将信号参数转发给manage_event()
,connect()
可以完全自行完成。