我不知道 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_click2
和 on_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
关于gtk - pygtk 中 connect() 和 connect_after() 的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45899746/