Python 和 d-bus:如何设置主循环?
我遇到了 python 和 dbus 的问题。我查看了开发人员文档和规范,但我不明白如何设置主循环。我想监听通知事件。 请参阅
http://dbus.freedesktop.org/doc/dbus-python/doc/< /a>
和
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
知道该怎么办吗? 提前致谢。 菲尼亚斯
I have a problem with python and dbus. I checked out the developer docs and specifications, but I don't understand how to set up a main loop. I want to listen for notification events.
See
http://dbus.freedesktop.org/doc/dbus-python/doc/
and
http://www.galago-project.org/specs/notification/0.9/index.html
My example script:
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 has no further methods like run().
If I use a loop from gobject and change the sourcecode:
import gobject
loop = gobject.MainLoop()
dbus.set_default_main_loop(loop)
...
loop.run()
I get following error message:
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
Any idea what to do about it?
Thanks in advance.
phineas
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将
import gobject
放在代码顶部,实例化对象后,执行gobject.MainLoop().run()
。我认为必须在创建DBusGMainLoop
之后创建MainLoop
。Put
import gobject
at the top of your code, and after instantiating your object, dogobject.MainLoop().run()
. I think that theMainLoop
has to be created after theDBusGMainLoop
is created.我也有同样的问题。在让我的代码工作后,我遇到了这个问题。
丹的回答部分正确。您首先导入 gobject,但您也可以在创建 DBusGMainLoop 之前实例化 MainLoop。您应该在创建 DBusGMainLoop 之后运行它。
I had the same problem. After getting my code to work, I came across this question.
Dan's answer is partially correct. You import gobject first, but you can also instantiate your MainLoop before the DBusGMainLoop is created. You should run it after the DBusGMainLoop is created.