8

I'm trying to use a Gtk.Switch widget in an app but "activate" signal is not firing by clicks. It works fine when using the widget with keyboard by hitting reture/space key on it but clicks don't fire the "activate" event.

Any Idea what is to be done in order to register signals for clicks on Gtk.Switch

from gi.repository impoty Gtk, GObject

def my_callback(widget, data=None):
    print 'Event Fired'

switch = Gtk.Switch()
window = Gtk.Window()
window.add(switch)
switch.connect('activate', my_callback)
window.show_all()
GObject.MainLoop().run()
Alba Mendez
  • 4,432
  • 1
  • 39
  • 55

2 Answers2

17

Actually, a better way is connect to the notify::active event.

Kknd
  • 3,033
  • 26
  • 29
  • 1
    The benefit of using this rather than the "button-press-event" is it will work correctly if the switch is toggled via another method e.g. key press / accessibility framework. – Rob Bradford Oct 14 '11 at 19:15
  • 1
    yes, I agree. Make it switch.connect('notify::active', my_callback) for python – narnie Mar 09 '12 at 23:55
  • 5
    Note to glade user: I spent a long time figuring how to register a handler for "listen::active", so this might be useful for other glade users. If you're using glade, you'll notice that there does not seem to be "notify::active" in the signals list, this is because it's a GObject signal "listen", use the "Detail" column to add the second parameter "active" to register a handler for "listen::active". – Lie Ryan Feb 10 '13 at 16:20
-1

Ok, after looking around for a couple of days I asked the question here and found the answer 5 minutes later.

To register mouse click, 'button-press-event' signal has to be used instead of 'activate' signal.

Might help someone with a similar problem.

GTK needs better documentation.

  • 1
    Submit a patch on bugzilla.gnome.org to improve this particular lapse in the documentation! ;-) – ptomato Aug 19 '11 at 15:58
  • 2
    -1 Usually, you shouldn't rely on concrete input methods unless you're making your own component. I think what you want to do is being notified when the switch **changes its state** (via the keyboard, dragging, ...), not when the user clicks with his mouse. **See Kknd's answer instead.** – Alba Mendez May 19 '12 at 11:14