Python 和 d-bus:如何设置主循环?

发布于 2024-10-01 08:18:43 字数 1487 浏览 0 评论 0原文

我遇到了 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清欢 2024-10-08 08:18:43

import gobject 放在代码顶部,实例化对象后,执行 gobject.MainLoop().run()。我认为必须在创建DBusGMainLoop之后创建MainLoop

Put import gobject at the top of your code, and after instantiating your object, do gobject.MainLoop().run(). I think that the MainLoop has to be created after the DBusGMainLoop is created.

风渺 2024-10-08 08:18:43

我也有同样的问题。在让我的代码工作后,我遇到了这个问题。

丹的回答部分正确。您首先导入 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文