python-3.x - 我想清理一个脚本,它会在 X 分钟不活动后自动锁定并关闭我的屏幕
问题描述
我正在运行 Lubuntu 20.04 LTS。
目前,要手动关闭我的显示器并锁定我的屏幕,我运行这个 bash 脚本/home/y/Scripts/Script to lock the screen but keep the computer awake using i3lock
......
i3lock -u -i /home/y/Pictures/02-Wallpaper.png &
sleep 4 &&
xtrlock &
sleep 2 &&
xset dpms force off
我希望该脚本在 X 分钟不活动后自动运行。
因此,在一段时间不活动后,我稍微修改了这个 Python 脚本锁屏,但不是在合上盖子时——我不完全理解——来创建......
import subprocess
import time
# set screen off after x seconds
off = 300
# lock screen after x seconds
lock = 3
# check idle every x seconds
res = 3
def get(cmd):
return subprocess.check_output(cmd).decode("utf-8").strip()
def test(t, threshold):
return int(t)/1000 < threshold
testoff1 = True
testlock1 = True
t1 = 0
while True:
time.sleep(res)
t2 = get("xprintidle")
testoff2 = test(t2, off); testlock2 = test(t2, lock)
if (testoff2, testoff1) == (False, True):
subprocess.run(["/bin/bash", "/home/y/Scripts/Script to lock the screen but keep the computer awake using i3lock"])
if (testlock2, testlock1) == (False, True):
subprocess.Popen(["gnome-screensaver-command", "-l"])
testoff1 = testoff2; testlock1 = testlock2
当我简单地测试它时,它起作用了。也就是说,我的屏幕正确锁定。我想删除脚本的无关部分。例如,我认为我不需要subprocess.Popen(["gnome-screensaver-command", "-l"])
. 另外,我可以删除代码的其他部分testoff1 = testoff2; testlock1 = testlock2
吗?
解决方案
推荐阅读
- android-studio - 类型不匹配:推断类型是 GroupsFragment 但 Context!预计
- r - 使用 cross() 选择变量的复杂“if”条件
- ios - Swift UITextFieldBeganEditing 选择所有文本不起作用
- c# - 我将如何编写一个解码此文本的函数?
- reactjs - Axios 不使用反应代理
- python - 使用 PyParsing 解析带有重要换行符的语言(如 Python)
- javascript - 如何在具有动态加载、不受信任的模块的应用程序中对代码进行沙箱处理?
- python - 使用 pywin32 代码制作可执行的 cx_freeze = 错误没有名为 win32 的模块
- reactjs - 将 OnClick 事件附加到动态创建的 React 组件
- ios - iOS 13 中的本地通知 didReceive