2012-10-24 96 views
2

我希望能夠在第一調用一個簡單的腳本來啓用或我的上網本禁用外部顯示器。我使用XFCE作爲桌面運行Fedora 17。我看到我應該可以使用python和python-dbus來反覆啓用和關閉活動。我的問題是,我無法弄清楚如何發出一個信號讓新的設置激活。不幸的是,Python不是我經常使用的語言。我有到位的代碼是:發射信號使用Python-DBUS

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 

我花了一點時間尋找,我沒有找到許多蟒​​蛇的例子做任何接近這個。提前致謝。

回答

0

PropertyChanged是一個信號,而不是一個方法。您與之通信的服務負責發射信號。在這種情況下,應該PropertyChanged隱含火,只要在相應的對象或接口屬性的值發生了變化。

,當你調用remote_interface.SetProperty(...)這應該含蓄地發生,你不應該需要明確「呼」的方法等的信號。

如果您有興趣接收信號,您需要設置一個glib主循環,並在您的代理對象上調用connect_to_signal,並將其傳遞給調用的回調方法。