在某些情况下,用 lambda 连接信号不起作用?
我正在尝试使用 python 2.7 进行 pygtk 程序开发,并且在使用 lambda 函数连接信号时遇到了一些问题:
- 让我们说想要连接信号的类是类 A_Class,并且信号是字符串“A_Signal”,
- 我们还处置了第二个类B_class,定义了公共函数manage_signal
- A_Class处置了一个实例B_class 的instance_b
所以,这是我的问题: 我不明白为什么:
- 如果我从 A_Class 实例 => 的方法调用
connect("A_Signal", lambda
我们甚至无法进入instance_b的manage_signal函数(例如:: instance_b.manage_signal) connect("activate", lambda menu_item : instance_b.manage_signal)
- 如果我在A_Class中定义了一个manage_signal方法,该方法又调用instance_b。管理信号并调用 connect("A_Signal", lambda
: self.manage_signal) => 这次可以使用
代码吗?
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 ...
一些更明确的
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 even 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的 lambda 表达式应该调用处理函数而不是返回它:
当然,正如您所说,这是不需要的,因为它相当于:
lambda 最终只会将信号参数中继到
manage_event()
,connect()
本身就可以完美完成一些事情。Your lambda expression should call the handler function instead of returning it:
Of course, as you said, this is unneeded because it's equivalent to:
The lambda would only end up relaying the signal arguments to
manage_event()
, something thatconnect()
can perfectly do by itself.