我正在尝试制作一个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'
在这种情况下,我可以查看所有与筛选条件匹配的消息。有人能帮助我理解我哪里出错了吗?谢谢。
print
信息了,尽管获取消息的方式对我来说似乎是正确的。 - syntaxerror