I cannot figure out how to handle the notify::active
signal of a Gtk.Switch
. I'm working with the MVC architecture (pattern) suggested here.
The error I got is this:
TypeError: _on_switch_serial_toggled() missing 1 required positional argument: 'state'
Here's my minimal working example (without a model):
import sys
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, Gio, GObject
class Application(Gtk.Application):
def __init__(self):
app_id = "org.iea.etc"
flags = Gio.ApplicationFlags.FLAGS_NONE
super(Application, self).__init__(application_id=app_id, flags=flags)
def do_activate(self):
# c.Controller(m.Model(), v.View(application=self))
Controller(None, View(application=self))
def do_startup(self):
Gtk.Application.do_startup(self)
class Controller(object):
def __init__(self, model, view):
self._model = model
self._view = view
self._view.connect('switch_serial_toggled',
self._on_switch_serial_toggled)
self._view.show_all()
def _on_switch_serial_toggled(self, switch, state):
if switch.get_active():
print('Switch ON')
else:
print('Switch OFF')
class View(Gtk.ApplicationWindow):
__gsignals__ = {
'switch_serial_toggled': (GObject.SIGNAL_RUN_FIRST, None, ())
}
def __init__(self, **kw):
super(View, self).__init__(**kw)
self._switch_serial = Gtk.Switch()
self._switch_serial.connect("notify::active",
self.on_switch_serial_toggled)
self.add(self._switch_serial)
def on_switch_serial_toggled(self, switch, state):
self.emit('switch_serial_toggled')
if __name__ == '__main__':
app = Application()
exit_status = app.run(sys.argv)
sys.exit(exit_status)