【发布时间】:2020-10-27 08:40:46
【问题描述】:
我正在尝试制作一个脚本,它可以帮助我跟踪我在计算机上花费了多长时间。这个脚本应该跟踪我何时开始、停止以及我在每个“任务”上花费了多长时间。经过一番搜索,我找到了一个名为xdotool 的终端实用程序,它将返回当前聚焦的窗口,并且在运行时它的标题如下:xdotool getwindowfocus getwindowna me。例如。当关注此窗口时,它会返回:
linux - Monitering time spent on computer w/ Python and xorg-server - Stack Overflow — Firefox Developer Edition
这正是我想要的。我的第一个想法是检测焦点窗口何时更改,然后获取发生这种情况的时间,但是我找不到任何结果,所以我使用了一个 while 循环,它每 5 秒运行一次这个命令,但这是相当hack-y 并且已经证明很麻烦,我会高度更喜欢 on-focus-change 方法,但这是我现在的代码:
#!/usr/bin/env python3
from subprocess import run
from time import time, sleep
log = []
prevwindow = ""
while True:
currentwindow = run(['xdotool', 'getwindowfocus', 'getwindowname'],
capture_output=True, text=True).stdout
if currentwindow != prevwindow:
for entry in log:
if currentwindow in entry:
pass # Calculate time spent
print(f"{time()}:\t{currentwindow}")
log.append((time(), currentwindow))
prevwindow = currentwindow
sleep(5)
我在 Arch linux 上使用 dwm 应该有什么关系
【问题讨论】:
标签: python linux monitoring xorg