【发布时间】:2012-07-17 16:37:51
【问题描述】:
我正在尝试制作一个 python 应用程序,它可以读取通过 DBus 的消息,从而提供与 bash dbus-monitor 相同的输出。根据我从搜索中获得的信息,代码应该非常简单明了,例如:
import dbus, gobject
from dbus.mainloop.glib import DBusGMainLoop
def msg_cb(bus, msg):
args = msg.get_args_list()
print "Notification from '%s'" % args[0]
print "Summary: %s" % args[3]
print "Body: %s", args[4]
if __name__ == '__main__':
DBusGMainLoop(set_as_default=True)
bus = dbus.SessionBus()
string = "interface='org.freedesktop.Notifications',member='Notify'"
bus.add_match_string(string)
bus.add_message_filter(msg_cb)
mainloop = gobject.MainLoop ()
mainloop.run ()
但启动它时,我只收到 DBus 返回的消息,说明应用程序已连接,这与我执行 bash 命令时得到的不同:
dbus-monitor --session interface='org.freedesktop.Notifications',member='Notify'
在这种情况下,我可以查看所有符合过滤条件的消息。 有人请帮助我了解我失败的地方吗? 谢谢
【问题讨论】:
-
这可能无法回答您的问题,但安装 qt4-dev-tools 并启动 qtdbusviewer 可能值得一试。我用它来调试我的 dbus 代码。我认为如果您在命令行上运行它,它可能会在其输出中显示 dbus 消息。干杯。
-
看起来语法在短短 2 年内发生了很大变化。至少在我的 python 2.7.8 上,我不再使用“printf-C 样式”语法得到 any
print输出,尽管获取消息的方式似乎是正确的我。