Gtk Switch activate signal not firing
Asked Answered
P

2

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()
Pyromagnetic answered 19/8, 2011 at 11:33 Comment(1)
Actually, this has nothing to do with PyGTK, but with GObject. PyGTK is for GTK 2.X.Dieback
S
18

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

Sherris answered 14/10, 2011 at 18:59 Comment(3)
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.Sard
yes, I agree. Make it switch.connect('notify::active', my_callback) for pythonZinciferous
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".Sodamide
P
-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.

Pyromagnetic answered 19/8, 2011 at 11:38 Comment(2)
Submit a patch on bugzilla.gnome.org to improve this particular lapse in the documentation! ;-)Consubstantiation
-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.Dieback

© 2022 - 2024 — McMap. All rights reserved.