python - 使用 PyGObject 获取 GDBusMenuModel 的菜单项

标签 python dbus pygobject gio

所以因为我有 unity-gtk-module安装后,所有 gtk 应用程序都通过 dbus SessionBus 导出它们的菜单。我的目标是提取所有可用菜单条目的列表。我已经在 pydbus 的帮助下实现了这个,但由于某种原因,这个解决方案非常不稳定,一些应用程序会崩溃。 unity-gtk-module 使用 Gio 的 g_dbus_connection_export_menu_model ()通过 dbus 导出其 GMenuModel 建模菜单,因此我认为尝试使用 Gio 处理导出的菜单是有意义的。 Gio 使用 GDBusMenuModel类从总线检索菜单。 Python 使用 PyGObject 包装 Gio:

from gi.repository import  Gio
connection = Gio.bus_get_sync(Gio.BusType.SESSION, None)
menuModel = Gio.DBusMenuModel.get(connection, [bus-name e.g. ":1.5"], [object-path e.g. "/com/canonical/unity/gtk/window/0"])

现在 menuModel 应该从我的应用程序中包装 GMenuModel。在这一点上,老实说,我对 GMenuModel 的工作原理有点困惑(Description 并没有真正的帮助),但似乎我必须使用 GMenuAttributeIter对象遍历条目。但是当我尝试这个时:

iter = Gio.MenuModel.iterate_item_attributes(menuModel, 0) #0 is the index of the root node

发生这种情况:

GLib-GIO-CRITICAL **: g_dbus_menu_model_get_item_attributes: assertion 'proxy->items' failed
GLib-GIO-CRITICAL **: GMenuModel implementation 'GDBusMenuModel' doesn't override iterate_item_attributes() and fails to return sane calues from get_item_attributes()

这可能是因为 GDBusMenuModel 继承了提供这些方法的 GMenuModel,但它是抽象的,因此 GDBusMenuModel 应该覆盖它们,但事实并非如此(参见上面的链接,它只提供 g_dbus_menu_model_get ())。如果是这种情况,我应该如何实际使用此类作为代理?如果不是,我做错了什么?

最佳答案

我刚刚在几年后第一次登录 SO,并记得我实际上已经找到了这个问题的解决方案(我认为)。老实说,我什至不记得这些词的一半是什么意思,但当时我写了一个脚本来完成标题中提出的任务,据我记得,最后,它成功了:https://gist.github.com/encomiastical/caa0ee955300bc2a40ef55d123b06212

关于python - 使用 PyGObject 获取 GDBusMenuModel 的菜单项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44143643/

相关文章:

python - pygobject 中无响应的拖放

python - 用 python 3 解开一个 python 2 对象

python - 切片 Pandas 数据框以获得不连续的列

python - 我的函数的性能从字典中删除作为其他键的子字符串的键

python - Django 评级错误 : "rating value must be a Rating instance, not ' 100"?

linux - 通过 dbus 接口(interface)使用 wpa_supplicant 创建访问点

linux - Bluez 5.48 - 如何强制蓝牙传输状态从事件到空闲 - 嵌入式接收器端

Python-dbus 附加参数到 add_signal_receiver

python-3.x - 如何在 PyGObject 中的 TextView 对象上添加占位符

label - 如何调整标签的宽度?