python - 使用python接收dbus信号

标签 python dbus dbus-python

我正在尝试编写一些Python来监听信号。

使用dbus-monitor,如下所示,我可以过滤我想要的信号。

dbus-monitor "type='signal',sender='org.kde.KWin',path='/ColorCorrect',interface='org.freedesktop.DBus.Properties',member='PropertiesChanged'"

signal time=1653997355.732016 sender=:1.4 -> destination=(null destination) serial=13165 path=/ColorCorrect; interface=org.freedesktop.DBus.Properties; member=PropertiesChanged
   string "org.kde.kwin.ColorCorrect"
   array [
      dict entry(
         string "enabled"
         variant             boolean false
      )
   ]
   array [
   ]

但是当我用 python 尝试同样的事情时,请参见下文,没有打印任何内容。

import dbus
from gi.repository import GLib
from dbus.mainloop.glib import DBusGMainLoop


def signal_handler(*args, **kwargs):
    for i, arg in enumerate(args):
        print("arg:%d        %s" % (i, str(arg)))
    print('kwargs:')
    print(kwargs)
    print('---end----')


DBusGMainLoop(set_as_default=True)
bus = dbus.SystemBus()

# register your signal callback
bus.add_signal_receiver(signal_handler,
                        bus_name='org.kde.KWin',
                        interface_keyword='org.freedesktop.DBus.Properties',
                        member_keyword='PropertiesChanged',
                        path_keyword='/ColorCorrect'
                        # message_keyword='msg')
                        )

loop = GLib.MainLoop()
loop.run()

最佳答案

您可能感兴趣:NightColor.py for Setting Temp From Terminal其中解释了如何使用 pydbus 来传达 KWin 使用代号“ColorCorrect”完成的夜间色彩效果。

解决方案

请参阅 pydbus, Connecting to the Bus 的文档:

There are generally two bus daemons you may be interested in. Each user login session should have a session bus, which is local to that session. It's used to communicate between desktop applications. Connect to the session bus by creating a SessionBus object:

from pydbus import SessionBus

session_bus = SessionBus()

KDE 中的 session 总线

默认: session 总线

来自dbus-monitor manpage :

Options --system Monitor the system message bus. --session Monitor the session message bus. (This is the default.)

因此,您在输出中看到的消息显然是来自默认 session 总线。

KDE 开发者指南中解释了相同的默认总线:Accessing D-Bus Interfaces :

A QDBusInterface object represents a given D-Bus interface. The constructor accepts as parameters (in order) a service name, an object path, an optional interface and optionally which bus (e.g. system or session) to use. If no bus is explicitly defined, it defaults to the session bus. If no interface is given, the returned object will be used to call all interfaces on the bus.

注意

KDE Plasma Desktop 窗口管理器的给定 DBus 服务名称 KWin (org.kde.KWin) 也可以有后缀,请参阅环境变量 KWIN_DBUS_SERVICE_SUFFIX

Suffix for the DBus-Service name "org.kde.KWin". If set, the name will be changed to "org.kde.KWin.${KWIN_DBUS_SERVICE_SUFFIX}".

关于python - 使用python接收dbus信号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72447284/

相关文章:

c++ - 如何在 Qt DBus 调用中从 QDBusMessage 中提取返回的数据?

python - dbus-python 如何返回字典数组

python - Django 将功能添加到模板中的按钮

python - sdist 错误 : option --manifest-only not recognized

python - 使用 pymongo 保持连续的 mongo 连接处于事件状态

python - 如果未找到参数,则在 __init__ 中引发异常

bash - 如何启动具有多个运行各种程序的选项卡的 KDE 控制台?

python - 如何通过脚本在 Ubuntu 中禁用 media_automount_open