Python和d-bus:如何设置主循环?
我有python和dbus的问题。我查看了开发人员文档和规范,但我不明白如何设置主循环。我想听通知事件。
看到
http://dbus.freedesktop.org/doc/dbus-python/doc/
和
http://www.galago-project.org/specs/notification/0.9/index.html
我的示例脚本:
import dbus
from dbus.mainloop.glib import DBusGMainLoop
class MessageListener:
def __init__(self):
DBusGMainLoop(set_as_default=True)
self.bus = dbus.SessionBus()
self.proxy = self.bus.get_object('org.freedesktop.Notifications',
'/org/freedesktop/Notifications')
self.proxy.connect_to_signal('NotificationClosed',
self.handle_notification)
def handle_notification(self, *args, **kwargs):
print args, kwargs
if __name__ == '__main__':
MessageListener()
DBusGMainLoop没有像run()这样的其他方法。
如果我使用gobject的循环并更改源代码:
import gobject
loop = gobject.MainLoop()
dbus.set_default_main_loop(loop)
...
loop.run()
我收到以下错误消息:
Traceback (most recent call last):
File "dbus_example.py", line 40, in <module>
MessageListener()
File "dbus_example.py", line 9, in __init__
dbus.set_default_main_loop(loop)
TypeError: A dbus.mainloop.NativeMainLoop instance is required
知道该怎么办吗?
提前致谢。
菲尼亚斯
没有找到相关结果
已邀请:
2 个回复
讼乐
放在代码的顶部,在实例化对象后,执行
。我认为
必须在创建
之后创建。
泻伴墓荒