0

I have a problem in that I need to emit data based on data I receive from a hardware thread. Ideally, I'd like to emit a signal with a data package.

I don't know what to fill in for the ???? below.

Do I need to make my own event class?

Something like this:

import gtk.gdk as gdk
import gtk.glade as glade

class ApplicationFrame(object):
    def __init__(self, *args, **kwargs):
        ...
        self.glade = glade.XML(ui_filepath)
        self.window = self.glade.get_widget(self.widget_name)
        # for keystrokes:
        self.window.add_events(gtk.gdk.KEY_PRESS_MASK)
        self.window.connect("key-press-event", self.default_handler)
        # for my special event ????
        self.window.add_events(gtk.gdk.????)
        self.window.connect("????", self.default_handler)
        ...

    def emit_signal(self, name, data):
        event = gdk.Event(gdk.????)
        event.name = name
        event.data = data
        self.window.emit(event)

    def default_handler(self, widget, event):
        name, data = self.extract_data_from(event)
        # do something special with the information...

Thanks in advance!

Edit: Final implementation (only showing new functions/additions)...

class ApplicationFrame(Ui_ApplicationFrame):
    """Standard Application Frame for Application"""
    __gsignals__ = {
        'data-received': (gobject.SIGNAL_RUN_FIRST, 
                          gobject.TYPE_NONE, 
                          (gobject.TYPE_PYOBJECT,)),
    }

    ...

    def handle_new_data(self, data):
    """Callback routine when data arrives on bus"""
        self.window.emit('data-received', data)

    def do_data_received(self, data):
    """Callback routine for emitted 'data-received' signal"""
    ...

...
gobject.type_register(ApplicationFrame)
Brian Bruggeman
  • 5,008
  • 2
  • 36
  • 55

1 Answers1

1

Don't define another gdk.Event. Those are only used to represent X11 events or their equivalents on other platforms. Instead, you need to have your class inherit from gobject.GObject, and then define a dictionary member with the magic name __gsignals__.

How to do it is described here.

Like this:

class ApplicationFrame(gobject.GObject):
    __gsignals__ = {
        'data-received': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE, (gobject.TYPE_PYOBJECT,))
    }

    def do_data_recieved(self, data):
        # default handler
ptomato
  • 56,175
  • 13
  • 112
  • 165
  • PS. This is how to do it in PyGTK, which is on its way out. Consider porting your program to GTK 3 and PyGObject. – ptomato Aug 10 '12 at 03:50
  • I would love to, but I'm currently stuck with Python 2.5 and an older version of PyGTK. Thank you for the insight, though! I will look into this right now. – Brian Bruggeman Aug 10 '12 at 05:27