Connecting signal with lambda does not work in some cases?
i'm experimenting pygtk programms development, with python 2.7, and i have a little problem when connecting signals using a lambda function :
- let us say that the class which wants to connect a signal is the class A_Class, and the signal is the string "A_Signal"
- we also dispose of a second class B_class defining public function manage_signal
- A_Class dispose of an instance instance_b of B_class
So, here my problem: I don't understand why :
- if i call
connect("A_Signal", lambda <signal_parameters> : instance_b.manage_signal)
from a method of A_Class instance => we can't ev开发者_C百科en enter in function manage_signal of instance_b (for example :connect("activate", lambda menu_item : instance_b.manage_signal)
- if i define in A_Class a method manage_signal, which in turn call instance_b.manage_signal and call
connect("A_Signal", lambda <signal_parameters> : self.manage_signal)
=> this time it works ?
Here some code to be more explicit
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 ...
Regards
Your lambda expression should call the handler function instead of returning it:
self.connect("delete-event", lambda the_widget, the_event: \
self.__instance_b.manage_event(the_widget, the_event))
Of course, as you said, this is unneeded because it's equivalent to:
self.connect("delete-event", self.__instance_b.manage_event)
The lambda would only end up relaying the signal arguments to manage_event()
, something that connect()
can perfectly do by itself.
精彩评论