pygtk 中 connect() 和 connect_after() 的区别

Difference between connect() and connect_after() in pygtk

我不知道 pygtk 中的 connect()connect_after() 有什么区别。有人可以用示例代码解释一下吗?

谢谢。

首先,这里是 g_signal_connect_after 的定义:

Connects a GCallback function to a signal for a particular object.The handler will be called after the default handler of the signal.

但是您可能会问什么是默认处理程序,好吧,GSignal description它非常具有描述性:

The basic concept of the signal system is that of the emission of a signal. Signals are introduced per-type and are identified through strings. Signals introduced for a parent type are available in derived types as well, so basically they are a per-type facility that is inherited.

A signal emission mainly involves invocation of a certain set of callbacks in precisely defined manner. There are two main categories of such callbacks, per-object ones and user provided ones. (Although signals can deal with any kind of instantiatable type, I'm referring to those types as "object types" in the following, simply because that is the context most users will encounter signals in.) The per-object callbacks are most often referred to as "object method handler" or "default (signal) handler", while user provided callbacks are usually just called "signal handler".

The object method handler is provided at signal creation time (this most frequently happens at the end of an object class' creation), while user provided handlers are frequently connected and disconnected to/from a certain signal on certain object instances.

A signal emission consists of five stages, unless prematurely stopped:

  • Invocation of the object method handler for G_SIGNAL_RUN_FIRST signals

  • Invocation of normal user-provided signal handlers (where the after flag is not set)

  • Invocation of the object method handler for G_SIGNAL_RUN_LAST signals

  • Invocation of user provided signal handlers (where the after flag is set)

  • Invocation of the object method handler for G_SIGNAL_RUN_CLEANUP signals

The user-provided signal handlers are called in the order they were connected in.

现在你知道了信号序列,下面是 answer to a similar question 但在 Gtk 邮件列表中:

g_signal_connect_after will let you run your user handler after the class's default handler; why is this usefull ?

Say I have an object that emits an "initialize" signal in which its class handler does the work, you probably want your handler to run after the class handler so that you can use the already initialized object in your function.

I think normally you dont have to use this method because signals of that nature are usually installed with G_SIGNAL_RUN_FIRST which; if I'm not mistaken means that it's default handler will be called before user handlers anyway.

在高级语言上使用它可能没有明显的需要,但是,例如,假设您想保证回调将是对 运行 的最后一个用户回调,那么您可以使用它方法。 (注意 pygtk 已弃用,请使用 pygobject)。

一个简单的例子,我们连接两个方法,on_click2on_click1(按此顺序),通过对 on_click2 使用 connect_after 我们确保它会运行 最后(用户回调):

import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk

class Button(Gtk.Box):

    def __init__(self, message):
        Gtk.Box.__init__(self, spacing=6)
        self.set_border_width(10)
        button = Gtk.Button.new_with_label(message)
        self.pack_start(button, True, True, 0)
        button.connect_after("clicked", self.on_click2)
        button.connect("clicked", self.on_click1)

    def on_click1(self, widget):
        print ("Click1 signal. connect normal");

    def on_click2(self, widget):
        print ("Click2 signal. connect after");

win = Gtk.Window()
button = Button("Test")

win.add (button)

win.connect("destroy", Gtk.main_quit)
win.show_all()

Gtk.main()

结果是 on_click2 是最后一个被调用的,尽管它是第一个连接的:

$ python <filename.py>
... (Click test button)...
Click1 signal. connect normal
Click2 signal. connect after