【问题标题】:Emitting Signals on dbus using Python-dbus使用 Python-dbus 在 dbus 上发射信号
【发布时间】:2012-10-14 22:56:08
【问题描述】:

我希望能够首先调用一个简单的脚本来启用或禁用我上网本的外接显示器。我正在以 XFCE 作为桌面运行 Fedora 17。我看到我应该能够使用 python 和 python-dbus 来打开和关闭活动切换。我的问题是我不知道如何发出信号以激活新设置。不幸的是,Python 不是我经常使用的语言。我的代码是:

import dbus
item = 'org.xfce.Xfconf'
path = '/org/xfce/Xfconf'
channel = 'displays'
base = '/'
setting = '/Default/VGA1/Active'

bus = dbus.SessionBus()
remote_object = bus.get_object(item, path)
remote_interface = dbus.Interface(remote_object, "org.xfce.Xfconf")

if remote_interface.GetProperty(channel, setting):
  remote_interface.SetProperty(channel, setting, '0')
  remote_object.PropertyChanged(channel, setting, '0')
else:
  remote_interface.SetProperty(channel, setting, '1')
  remote_object.PropertyChanged(channel, setting, '0')

它失败并被踢出:

Traceback (most recent call last):   File "./vgaToggle", line 31, in <module>
remote_object.PropertyChanged(channel, setting, '0')   
File "/usr/lib/python2.7/site-packages/dbus/proxies.py", line 140, in __call__
**keywords)   
File "/usr/lib/python2.7/site-packages/dbus/connection.py", line 630, in call_blocking
message, timeout) dbus.exceptions.DBusException: 
org.freedesktop.DBus.Error.UnknownMethod: Method "PropertyChanged"
with signature "sss" on interface "(null)" doesn't exist

我花了一些时间进行搜索,但没有找到很多类似的 Python 示例。提前致谢。

【问题讨论】:

    标签: python dbus xfce


    【解决方案1】:

    PropertyChanged 是一个信号,而不是一个方法。您正在与之通信的服务负责发出信号。在这种情况下,只要相应对象或接口上的属性值发生更改,PropertyChanged 就会隐式触发。

    这应该在您调用 remote_interface.SetProperty(...) 时隐式发生,并且您不需要像方法一样显式“调用”信号。

    如果您对接收信号感兴趣,您需要设置一个 glib 主循环并在您的代理对象上调用 connect_to_signal,并传递一个回调方法来调用。

    【讨论】:

      猜你喜欢
      • 2016-09-16
      • 1970-01-01
      • 2016-07-16
      • 2011-01-08
      • 2012-06-11
      • 2012-09-11
      • 1970-01-01
      • 2019-05-16
      • 2015-03-16
      相关资源
      最近更新 更多