Python GTK +: create custom signals?

Is it possible to create new signals in Python GTK +?

I need an example skeleton code.

+5
source share
2 answers

Exposure:

Creating Your Own Signals

Another thing you probably want to use when defining a GObject subclass of custom signals. You can create your own signals, which may be users of your class can connect to them.

. . - ( ), ( ) , .

, . , .

, , . , , : , - . , .

, http://www.pygtk.org/articles/subclassing-gobject/sub-classing-gobject-in-python.htm, , . , :

import pygtk
pygtk.require('2.0')
import gobject

class Car(gobject.GObject):
    __gproperties__ = {
        'fuel' : (gobject.TYPE_FLOAT, 'fuel of the car',
                  'amount of fuel that remains in the tank',
                  0, 60, 50, gobject.PARAM_READWRITE)
        }

    __gsignals__ = {
        'engine-started' : (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE,
                            (gobject.TYPE_FLOAT,))
        }

    def __init__(self):
        gobject.GObject.__init__(self)
        self.fuel = 50

    def do_get_property(self, property):
        if property.name == 'fuel':
            return self.fuel
        else:
            raise AttributeError, 'unknown property %s' % property.name

    def do_set_property(self, property, value):
        if property.name == 'fuel':
            self.fuel = value
        else:
            raise AttributeError, 'unknown property %s' % property.name

    def do_engine_started(self, remaining_fuel):
        print '***** Beginning of class closure *****'
        print 'The engine is ready and we still have %f of fuel' % self.fuel
        print '***** End of class closure *****'

    def start(self):
        self.emit('engine-started', self.get_property('fuel'))

gobject.type_register(Car)
+6

, , :

import gobject

from pygtkhelpers.utils import gsignal

class Car(gobject.GObject):

    gsignal('engine-started', float) # yeah baby
+3