gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 16:01:53 26 4
gpt4 key购买 nike

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

我得到的错误是这样的:

TypeError:_on_switch_serial_toggled() 缺少 1 个必需的位置参数:'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 信号适本地。问题在于处理您已添加到 View 中的自定义信号。

了解您在做什么很重要:您已经创建了一个具有属性的 View 类,该属性是 Gtk.Switch。您还创建了一个与此类关联的信号,名为 switch_serial_toggled。在类内部,您希望在内部 Gtk.Switch 更改状态时,View 触发其自己的“切换”信号。

话虽如此,让我们修复您的代码:

1 - 让我们将切换状态作为 bool 值添加到 View 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 是随信号发射传递的 bool 值。

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

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)

结果是这样的:

enter image description here

PS:请注意,在第 2 步中,obj 是您连接信号处理程序的对象,类似于 self._switch_serial。这意味着您可以改用 obj.get_active ():

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

关于python - 如何处理来自 Gtk.Switch 的 "notify::active"信号? (MVC架构),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50258880/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com