在python上使用gnome-screensaver-command [英] Use gnome-screensaver-command on python

查看:175
本文介绍了在python上使用gnome-screensaver-command的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码来检查屏幕是否被锁定(使用gnome-screensaver)

I have the following code to check whether the screen is locked or not (using gnome-screensaver)

gnome-screensaver-command -q | grep "is active"

通过此链接,> https://askubuntu.com/questions/17679/how-can-i-put-the-display-to-sleep-on-screen-lock 上有一段关于在shell脚本上使用它的代码.但是如何使用python中的代码?并将其存储在变量中,无论它是否处于活动状态.

From this link, https://askubuntu.com/questions/17679/how-can-i-put-the-display-to-sleep-on-screen-lock there is a code on using it on a shell script. But how do I use the code in python? And store it in a varaiable whether if it is active or not.

推荐答案

您还可以通过 D总线:

import dbus

def screensaver_active():
    bus = dbus.SessionBus()
    screensaver = bus.get_object('org.gnome.ScreenSaver', '/')
    return bool(screensaver.GetActive())

variable = screensaver_active()

这篇关于在python上使用gnome-screensaver-command的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆