如何处理来自 Gtk.Switch 的 "notify::active" 信号? (MVC架构)

How to handle a "notify::active" signal from a Gtk.Switch? (MVC architecture)

我不知道如何处理 Gtk.Switchnotify::active 信号。我正在使用 MVC 架构(模式)suggested here

我得到的错误是这样的:

TypeError: _on_switch_serial_toggled() missing 1 required positional argument: 'state'

这是我的最小工作示例(没有模型):

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)

首先,您正在正确处理 Gtk.Switch almostactive 属性 的 notify 信号。问题在于处理您已添加到视图中的自定义信号。

了解您在做什么很重要:您创建了一个视图 class,它有一个 属性,它是一个 Gtk.Switch。您还创建了一个与此 class 关联的信号,名为 switch_serial_toggled。在 class 内部,您希望当内部 Gtk.Switch 改变状态时,View 触发它自己的 "toggled" 信号。

话虽这么说,让我们修复您的代码:

1 - 让我们将切换状态作为布尔值 switch_serial_toggled 添加到视图

class View(Gtk.ApplicationWindow):
    __gsignals__ = {
        'switch_serial_toggled': (GObject.SIGNAL_RUN_FIRST, None, (bool,))
    }

2 - 让我们为 View 内部信号处理程序的参数赋予适当的名称,并将状态添加到发出的信号:

def on_switch_serial_toggled(self, obj, pspec):
    self.emit('switch_serial_toggled', self._switch_serial.get_active ())

GObject.Object 通知信号处理程序是 described here

3 - 现在,让我们转到 Controller 并正确处理 View 信号:

def _on_switch_serial_toggled(self, widget, active):
        if active is True:
            print('Switch ON')
        else:
            print('Switch OFF')

widget参数是 Controller 实例中的 View 实例,state 是随信号发射传递的布尔值。

包含上述修复的完整代码应如下所示:

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, widget, active):
            if active is True:
                print('Switch ON')
            else:
                print('Switch OFF')


class View(Gtk.ApplicationWindow):
    __gsignals__ = {
        'switch_serial_toggled': (GObject.SIGNAL_RUN_FIRST, None, (bool,))
    }

    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, obj, pspec):
        self.emit('switch_serial_toggled', self._switch_serial.get_active ())


if __name__ == '__main__':
    app = Application()
    exit_status = app.run(sys.argv)
    sys.exit(exit_status)

结果是这样的:

PS:请注意,在第 2 步中,obj 是您连接信号处理程序的对象,类似于 [=23] =].这意味着您可以使用 obj.get_active () 代替:

def on_switch_serial_toggled(self, obj, pspec):
    self.emit('switch_serial_toggled', obj.get_active ())