gpt4 book ai didi

Python 和 d-bus : How to set up main loop?

转载 作者:太空狗 更新时间:2023-10-29 18:07:24 27 4
gpt4 key购买 nike

我对 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()。我认为必须在创建 DBusGMainLoop 之后创建 MainLoop

关于Python 和 d-bus : How to set up main loop?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4125393/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com