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
知道该怎么办吗? 提前致谢。 菲尼亚斯     
已邀请:
import gobject
放在代码的顶部,在实例化对象后,执行
gobject.MainLoop().run()
。我认为
MainLoop
必须在创建
DBusGMainLoop
之后创建。     
我有同样的问题。让我的代码工作后,我遇到了这个问题。 丹的回答是部分正确的。您首先导入gobject,但您也可以在创建DBusGMainLoop之前实例化MainLoop。您应该在创建DBusGMainLoop后运行它。     

要回复问题请先登录注册