我正在尝试向类中添加自定义信号 -
class TaskBrowser(gobject.GObject):
__list_signal__ = (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE, (<List datatype>,))
__gsignals__ = {'tasks-deleted': __list_signal__}
...
def on_delete_tasks(self, widget=None, tid=None):
...
gobject.idle_add(self.emit, "tasks-deleted", deleted_tasks) #deleted_tasks is of type 'list'
...
...
在 __gsignals__
字典中,当我将 list
声明为参数类型时,我得到以下错误回溯 -
File "/home/manhattan/GTG/Hamster_in_hands/GTG/gtk/browser/browser.py", line 61, in <module>
class TaskBrowser(gobject.GObject):
File "/usr/lib/python2.7/site-packages/gobject/__init__.py", line 60, in __init__
cls._type_register(cls.__dict__)
File "/usr/lib/python2.7/site-packages/gobject/__init__.py", line 115, in _type_register
type_register(cls, namespace.get('__gtype_name__'))
TypeError: Error when calling the metaclass bases
could not get typecode from object
我看到了 possible parameter types 的列表,并且 list
有没有一种方法可以将列表作为信号参数传递?
最佳答案
C 库需要知道参数的 C 类型,对于 Gtk、Gdk、Gio 和 GLib 对象,包装器中的类型将起作用,因为它们反射(reflect)了 Gtk 和系列库中的 C 类型。
但是,对于任何其他类型,您需要传递 object
或 gobject.TYPE_PYOBJECT
。这意味着在 C 端传递了一个“python 对象”类型。从 python 脚本访问的每个对象都是这种类型,这几乎意味着您可以通过 python 脚本传递的任何对象都适合 object
参数。
当然,这也意味着这个特性在 python 中不起作用! Python 依赖于 duck typing,这意味着我们在处理某个对象时确定它是否属于某个类型并且它可以工作。传递参数的类型对于 C 来说是一种确保传递的对象是程序需要的类型的方法,但是在 python 中每个对象在 C 端都是相同的类型所以这个特性在 python 端变得完全无用。
但这并不意味着它总体上完全没有用。例如,在 python 中 int
是一个 object
。但不是在 C 中。如果您正在使用在 Gtk 库的 C 端编码的属性绑定(bind),您将需要指定适当的类型,因为不同属性类型的绑定(bind)不起作用。
使用带有object
参数类型的 C 端包装信号处理程序也必然不起作用,因为 C 端需要特定类型才能运行。
关于python - PyGTK 自定义信号发送一个列表作为参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19199944/