您很可能忘记设置事件循环(将示例转换为普通函数):
import dbus
from dbus.mainloop.glib import DBusGMainLoop
from gi.repository import GLib
def registerSignals():
for signal, callback, bus, interface in (('InterfacesAdded', onUSBInterfaceAdded, 'org.freedesktop.UDisks2', 'org.freedesktop.DBus.ObjectManager'),
('InterfacesRemoved', onUSBInterfaceRemoved, 'org.freedesktop.UDisks2', 'org.freedesktop.DBus.ObjectManager'),
('PropertiesChanged', onUSBMountPropertiesChanged, 'org.freedesktop.UDisks2', 'org.freedesktop.DBus.ObjectManager')):
print(dbus.SystemBus().add_signal_receiver(callback,
bus_name=bus,
signal_name=signal,
dbus_interface=interface))
def onUSBInterfaceAdded(path, interfaces):
print(path)
print(interfaces)
def onUSBInterfaceRemoved(path, interfaces):
print(path)
print(interfaces)
def onUSBMountPropertiesChanged(interface, properties, invalidated, path):
print(path)
print(interfaces)
DBusGMainLoop(set_as_default=True)
bus = dbus.SystemBus()
registerSignals()
GLib.MainLoop().run()