PyGTK connect_signals
Ok so I have a little test program here:
This is in my file that I load through gtk.Builder
<object class="GtkWindow" id="mainWindow">
<property name="default_width">500</property>
<property name="default_height">250</property>
<signal name="delete_event" handler="endProgram" />
</object>
I then use this:
def endProgram ():
print "end";
rofl = gtk.Builder();
rofl.add_from_file("mainwindow.ui");
win = rofl.get_object("mainWindow");
rofl.connect_signals("mainWindow");
win.show_all();
gtk.main();
Yet when I go to run that it complains that I am missing a handler for the mainWindow object. I have tried doing rofl.connect_signals(wi开发者_开发技巧n); as well
Per the docs, connect_signals
takes as the argument a mapping or instance, and
uses Python's introspective features to look at the keys (if object is a mapping) or attributes (if object is an instance) and tries to match them with the signal handler names given in the interface description. The callbacks referenced by each matched key or attribute are connected to their matching signals.
So for example when you pass "mainwindow"
, which is an instance of str
, the attributes are e.g. such method names as upper
, lower
, isalpha
, and the like -- nothing to do with anything at all that you might be remotely interested about. And why would you want the attributes of win
to handle signals, either? IOW, what do you expect connect_signals
to do?
A more typical example use can be found e.g. in this SO question and this tutorial, which offers among others the following Python example:
class TutorialTextEditor:
def on_window_destroy(self, widget, data=None):
gtk.main_quit()
def __init__(self):
builder = gtk.Builder()
builder.add_from_file("tutorial.xml")
self.window = builder.get_object("window")
builder.connect_signals(self)
as you see, here connect_signals
is used in the typical way -- i.e., passing an object (self
) with an on_window_destroy
method that (by introspection) will be used as the handler for the signal raised on window destruction.
精彩评论