我对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
你知道怎么办吗? 提前谢谢。 菲尼亚斯
我也有同样的问题。在我的代码开始工作后,我遇到了这个问题。
丹的回答部分正确。首先导入gobject,但也可以在创建DBusGMainLoop之前实例化主循环。应该在创建DBusGMainLoop之后运行它。
将
import gobject
放在代码的顶部,然后在实例化对象之后,执行gobject.MainLoop().run()
。我认为MainLoop
必须在创建DBusGMainLoop
之后创建。相关问题 更多 >
编程相关推荐