在某些情况下,用 lambda 连接信号不起作用?

发布于 2024-11-07 00:18:20 字数 1391 浏览 0 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北陌 2024-11-14 00:18:20

您的 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() 本身就可以完美完成一些事情。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文