DBus Error: Unable to append with type error saying list indices must be integers or slices, not dict

DBus Error: Unable to append with type error saying list indices must be integers or slices, not dict

我用 Python 使用包 dbus 编写了一个 dbus 程序。

@dbus.service.method("com.fsevenm.castboard.KaroWidget", in_signature='', out_signature="a{si}")
def get_screens(self):
    return [
        {
            "id": 1
        }
    ]

我确信类型和签名是正确的,但每次我调用该方法时,它都会在控制台中显示此错误:

ERROR:dbus.service:Unable to append ([{'id': 1}],) to message with signature a{si}: <class 'TypeError'>: list indices must be integers or slices, not dict

dbus的响应:

('g-io-error-quark: GDBus.Error:org.freedesktop.DBus.Python.TypeError: '
 'Traceback (most recent call last):\n'
 '  File "/usr/lib/python3/dist-packages/dbus/service.py", line 751, in '
 '_message_cb\n'
 '    _method_reply_return(connection, message, method_name, signature, '
 '*retval)\n'
 '  File "/usr/lib/python3/dist-packages/dbus/service.py", line 254, in '
 '_method_reply_return\n'
 '    reply.append(signature=signature, *retval)\n'
 'TypeError: list indices must be integers or slices, not list\n'
 ' (36)')('g-io-error-quark: GDBus.Error:org.freedesktop.DBus.Python.TypeError: '
 'Traceback (most recent call last):\n'
 '  File "/usr/lib/python3/dist-packages/dbus/service.py", line 751, in '
 '_message_cb\n'
 '    _method_reply_return(connection, message, method_name, signature, '
 '*retval)\n'
 '  File "/usr/lib/python3/dist-packages/dbus/service.py", line 254, in '
 '_method_reply_return\n'
 '    reply.append(signature=signature, *retval)\n'
 'TypeError: list indices must be integers or slices, not dict\n'
 ' (36)')

我该如何解决这个问题?

我扩展了 dbus-python example service 以包含您尝试做的词典列表。

我认为您缺少数组的 a,然后是字典的第二个 a

以下对我有用:

@dbus.service.method("com.example.SampleInterface",
                     in_signature='', out_signature='aa{si}')
def GetArrayDict(self):
    return [
            {"first": 1},
            {"second": 2}
            ]