Skip Montanaro wrote:
>Using the Gtk 2.0 API, I have a subclass of GtkEntry to which I've attached
>three new signals: history-prev, history-next, and history-menu:
>
> class _HEntry(gtk.Entry):
> def __init__(self, *args):
> gtk.Entry.__init__(self, *args)
> self.set_name("_history_")
>
> gobject.signal_new("history-prev", _HEntry,
> gobject.SIGNAL_RUN_LAST|gobject.SIGNAL_ACTION,
> gobject.TYPE_NONE, ())
>
> gobject.signal_new("history-next", _HEntry,
> gobject.SIGNAL_RUN_LAST|gobject.SIGNAL_ACTION,
> gobject.TYPE_NONE, ())
>
> gobject.signal_new("history-menu", _HEntry,
> gobject.SIGNAL_RUN_LAST|gobject.SIGNAL_ACTION,
> gobject.TYPE_NONE, ())
>
The above code creates a python subclass of GtkEntry, but at the C level
(where all the signal handling takes place), your _HEntry is still just
a GtkEntry (take a look at _HEntry.__gtype__). At the moment, you need
to explicitly create a new GType for your subclass. Your _HEntry def
should look like:
class _HEntry(gtk.Entry):
def __init__(self):
gobject.GObject.__init__(self)
self.set_name("_history_")
gobject.type_register(_HEntry)
Note the gobject.type_register() call, and chaining to GObject.__init__ rather than
gtk.Entry.__init__. If you look at _HEntry.__gtype__, it should be for This way, the
signals will be added to your new type rather than GtkEntry. This will also change
the type name used in the gtkrc file. The type name is generated from the full class
name (module.class), with dots replaced with plus signs (because Tim doesn't want to
make dots valid in type names).
>
>
>I want to add the same signals to a subclass of GtkSpinButton:
>
> class _HSpinButton(gtk.SpinButton):
> def __init__(self, *args):
> gtk.SpinButton.__init__(self, *args)
> self.set_name("_history_")
>
> gobject.signal_new("history-prev", _HSpinButton,
> gobject.SIGNAL_RUN_LAST|gobject.SIGNAL_ACTION,
> gobject.TYPE_NONE, ())
>
> gobject.signal_new("history-next", _HSpinButton,
> gobject.SIGNAL_RUN_LAST|gobject.SIGNAL_ACTION,
> gobject.TYPE_NONE, ())
>
> gobject.signal_new("history-menu", _HSpinButton,
> gobject.SIGNAL_RUN_LAST|gobject.SIGNAL_ACTION,
> gobject.TYPE_NONE, ())
>
GtkSpinButton is a GtkEntry, and your above code added the signals to
GtkEntry (so they are visible in GtkSpinButton). Registering a new type
for _HSpinButton is probably the right option here as well.
>
>
>When I try this, the signals seem to be defined for my GtkEntry subclass
>just fine, but I get a Gtk warning and Python RuntimeError exception when
>trying to attach the signals to the GtkSpinButton subclass:
>
> History.py (pid:18736): GRuntime-WARNING **: gsignal.c:1147:g_signal_newv():
>signal "history_prev" already exists in the `GtkEntry' class ancestry
> Traceback (most recent call last):
> ...
> File "/home/skip/src/tttech/projects/SW/lib/python/TGW/Spinner.py", line 19, in
>?
> gobject.TYPE_NONE, ())
> RuntimeError: could not create signal
>
>This is a bit frustrating. I thought the restriction on signal names was
>that a direct ancestor of the class to which I was adding a signal couldn't
>already be using this signal name. It seems to me that I need to add these
>three signals rather high up in the widget hierarchy, which unfortunately
>will pollute a lot of widgets that have no use for them with these signals.
>Is there an alternative?
>
Does the above comments help? See the examples/gobject/signals.py
program for an example of registering new types.
James.
_______________________________________________
pygtk mailing list [EMAIL PROTECTED]
http://www.daa.com.au/mailman/listinfo/pygtk