import dbus
def screensaver_status():
session_bus = dbus.SessionBus()
screensaver_list = ['org.gnome.ScreenSaver',
'org.cinnamon.ScreenSaver',
'org.kde.screensaver',
'org.freedesktop.ScreenSaver']
for each in screensaver_list:
try:
object_path = '/{0}'.format(each.replace('.', '/'))
get_object = session_bus.get_object(each, object_path)
get_interface = dbus.Interface(get_object, each)
return bool(get_interface.GetActive())
except dbus.exceptions.DBusException:
pass
status = screensaver_status()
print(status)
这会捕获所有屏幕保护程序,而不仅仅是 Gnome。它也不会通过使用类似的东西来阻止
*-screensaver-command