有时候,当我需要去某个地方时,我会把计算机保持未悬浮状态,但是我将它留给我绝对信任的人,并且我很快就会回来,所以没有真正的需要暂停(例如,如果是家庭,我只是跳到那是一间厕所)距离我所处的地方只有一小段距离,在这样的情况下始终挂起我的机器并不方便),或者可能有其他原因导致我想要这样做,例如想知道是否应该跑到我的机器上摆动鼠标进入睡眠状态,或者如果我可以较慢的速度进入计算机。
无论如何,要讲到这一点,我希望能够在屏幕右上角所有内容的顶部启动倒计时(最好是在透明窗口(不是透明窗口位))顶部栏下方)显示,它将向我显示下一个暂停时间的倒计时。
这可能会在我每次与机器交互时重置计时器,或者以某种方式直接与暂停系统交互以告知下一次暂停的时间(因为我假设存在某种倒计时)。如何实现这样的效果(当然,我不希望窗口始终处于打开状态,而是让我能够在启动窗口的任何时间运行命令,或者甚至可以是终端窗口中的倒数计时如果GUI位完全有问题,我会只是移动还是会自动位于右上角)?
我正在运行带有GNOME 3.18的Ubuntu GNOME 15.10,但是该解决方案也应该与带有GNOME 3.20的Ubuntu GNOME 16.04一起使用,因为我打算尽快进行升级。
在下面的原始答案中,倒计时窗口在任意空闲时间之后出现。重新阅读您的问题,您可能会永久想要它。永久版本在下面(更简单),原始答案则更进一步。
解决方案是一个后台脚本,显示一个半透明的倒计时窗口。该窗口的行为类似于通知:它始终在顶部可见,但是(当然)您可以照常在其他窗口中工作:
初始时间是应激活挂起之前的空闲时间。鼠标键盘事件会重置时间。
如图所示,脚本具有不同的预设颜色选项(请参见下文)。
该脚本需要xprintidle
:
sudo apt-get install xprintidle
countdown.py
以空闲时间为参数运行它:
python3 /path/to/countdown.py <idle_time>
例如
python3 /path/to/countdown.py 300
5分钟后进入暂停状态。
如果一切正常,请将其添加到启动应用程序:Dash>启动应用程序>添加。添加命令:
/path/to/runner.py <idle_time>
#!/usr/bin/env python3
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, Gdk, GObject, Pango
from threading import Thread
import subprocess
import time
import signal
import sys
import os
# --- set the color (index) below (1 is the first)
color = 1
# ---
textcolors = ["grey", "orange", "green", "blue", "white"]
# --- don't change anything below
txtcolor = textcolors[color-1]
countdown = int(sys.argv[1])
susp = os.path.dirname(os.path.realpath(__file__))+"/susp.sh"
class CountDown(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self)
maingrid = Gtk.Grid()
self.add(maingrid)
maingrid.set_border_width(40)
# set initial text for the spash window
self.label = Gtk.Label(convert_seconds(countdown))
self.label.modify_font(Pango.FontDescription('Ubuntu 22'))
self.label.set_width_chars(10)
maingrid.attach(self.label, 0, 0, 1, 1)
self.update = Thread(target=self.start_countdown, args=[countdown])
# daemonize the thread
self.update.setDaemon(True)
self.update.start()
def start_countdown(self, countdown):
idle1 = idletime()
t = countdown
while True:
time.sleep(1)
idle2 = idletime()
if idle2 < idle1:
t = countdown
else:
t -= 1
if t <= 0:
subprocess.Popen(["systemctl", "suspend"])
GObject.idle_add(self.label.set_text, convert_seconds(t),
priority=GObject.PRIORITY_DEFAULT)
idle1 = idle2
def stop(self):
Gtk.main_quit()
def get_screen():
scr = [s.split("x") for s in subprocess.check_output([
"xrandr"]).decode("utf-8").split() if "+0+0" in s][0]
return int(scr[0]) - 300
def convert_seconds(sec):
timedisplay = [
str(int(sec/3600)),
str(int((sec % 3600)/60)),
str(int(sec % 60)),
]
for i, n in enumerate(timedisplay):
if len(n) == 1:
timedisplay[i] = "0"+n
return ":".join(timedisplay)
def idletime():
return int(subprocess.check_output(
"xprintidle"
).decode("utf-8").strip())/1000
def splashwindow():
window = CountDown()
window.set_decorated(False)
window.set_resizable(False)
window.override_background_color(Gtk.StateType.NORMAL, Gdk.RGBA(0,0,0,1))
window.modify_fg(Gtk.StateFlags.NORMAL, Gdk.color_parse(txtcolor))
window.set_opacity(0.6)
window.move(get_screen(), 80)
window.set_keep_above(True)
window.show_all()
Gtk.main()
GObject.threads_init()
splashwindow()
文字的颜色可以更改,如答案的第二个版本的最底部所述。
#!/usr/bin/env python3
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, Gdk, GObject, Pango
from threading import Thread
import subprocess
import time
import signal
import sys
import os
# --- set the color (index) below (1 is the first)
color = 1
# ---
textcolors = ["grey", "orange", "green", "blue", "white", "yellow", "red"]
# --- don't change anything below
txtcolor = textcolors[color-1]
al_cl1 = textcolors[5]; al_cl2 = textcolors[6]
countdown = int(sys.argv[1])
alarm1 = int(countdown/2)
alarm2 = 30
class CountDown(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self)
maingrid = Gtk.Grid()
self.add(maingrid)
maingrid.set_border_width(40)
# set initial text for the spash window
self.label = Gtk.Label(convert_seconds(countdown))
self.label.modify_font(Pango.FontDescription('Ubuntu 22'))
self.label.modify_fg(Gtk.StateFlags.NORMAL, Gdk.color_parse(txtcolor))
self.label.set_width_chars(10)
maingrid.attach(self.label, 0, 0, 1, 1)
self.update = Thread(target=self.start_countdown, args=[countdown])
# daemonize the thread
self.update.setDaemon(True)
self.update.start()
def mod_color(self, color):
self.label.modify_fg(Gtk.StateFlags.NORMAL, Gdk.color_parse(color))
def start_countdown(self, countdown):
idle1 = idletime()
t1 = countdown
t2 = countdown
while True:
time.sleep(1)
idle2 = idletime()
if idle2 < idle1:
t2 = countdown
if t1 <= alarm1:
# print("change textcolor default")
GObject.idle_add(self.mod_color, txtcolor,
priority=GObject.PRIORITY_DEFAULT)
else:
t2 -= 1
if all([t2 <= alarm2, t1 > alarm2]):
# print("change textcolor red")
GObject.idle_add(self.mod_color, al_cl2,
priority=GObject.PRIORITY_DEFAULT)
elif all([t2 <= alarm1, t1 > alarm1]):
# print("change textcolor yellow")
GObject.idle_add(self.mod_color, al_cl1,
priority=GObject.PRIORITY_DEFAULT)
if t2 <= 0:
subprocess.Popen(["systemctl", "suspend"])
GObject.idle_add(self.label.set_text, convert_seconds(t2),
priority=GObject.PRIORITY_DEFAULT)
idle1 = idle2
t1 = t2
def stop(self):
Gtk.main_quit()
def get_screen():
scr = [s.split("x") for s in subprocess.check_output([
"xrandr"]).decode("utf-8").split() if "+0+0" in s][0]
return int(scr[0]) - 300
def convert_seconds(sec):
timedisplay = [
str(int(sec/3600)),
str(int((sec % 3600)/60)),
str(int(sec % 60)),
]
for i, n in enumerate(timedisplay):
if len(n) == 1:
timedisplay[i] = "0"+n
return ":".join(timedisplay)
def idletime():
return int(subprocess.check_output(
"xprintidle"
).decode("utf-8").strip())/1000
def splashwindow():
window = CountDown()
window.set_decorated(False)
window.set_resizable(False)
window.override_background_color(Gtk.StateType.NORMAL, Gdk.RGBA(0,0,0,1))
window.set_opacity(0.6)
window.move(get_screen(), 80)
window.set_keep_above(True)
window.show_all()
Gtk.main()
GObject.threads_init()
splashwindow()
下面的设置将显示倒计时(在任意时间长度内)到下一个暂停:
该窗口始终位于所有其他窗口的顶部,就像通知气泡一样。
该安装程序将替换“常规”挂起设置,这意味着您需要从系统设置中禁用挂起。
在脚本中挂起的命令是:
systemctl suspend
不需要sudo
。结果是您至少 15.04
需要使用此解决方案。
该脚本是在Ubuntu
(Unity)上编写和测试的15.10
,但是其中没有特定于Unity的特定代码。我认为它可以在所有默认的Ubuntu版本上正常工作>15.04
要进行设置(详细版本,请参见下面的详细信息),只需将涉及的三个脚本复制到一个相同的目录中即可,该目录的名称与所示的名称完全相同。要运行,只需运行主脚本(运行时间检查)。
该脚本需要xprintidle
:
sudo apt-get install xprintidle
将以下三个脚本复制到单独的空文件中,将分别保存在一个和相同的目录下,并按照指示正确命名:
A.保存(完全)为win.py
:
#!/usr/bin/env python3
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, Gdk, GObject, Pango
from threading import Thread
import subprocess
import time
import signal
import sys
import os
# --- set the color (index) below (1 is the first)
color = 1
# ---
textcolors = ["grey", "orange", "green", "blue", "white"]
# --- don't change anything below
txtcolor = textcolors[color-1]
countdown = int(sys.argv[1])
susp = os.path.dirname(os.path.realpath(__file__))+"/susp.sh"
class CountDown(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self)
maingrid = Gtk.Grid()
self.add(maingrid)
maingrid.set_border_width(40)
# set initial text for the spash window
self.label = Gtk.Label(convert_seconds(countdown))
self.label.modify_font(Pango.FontDescription('Ubuntu 22'))
self.label.set_width_chars(10)
maingrid.attach(self.label, 0, 0, 1, 1)
self.update = Thread(target=self.start_countdown, args=[countdown])
# daemonize the thread
self.update.setDaemon(True)
self.update.start()
def start_countdown(self, countdown):
t = countdown
while t > 0:
time.sleep(1)
t -= 1
GObject.idle_add(self.label.set_text, convert_seconds(t),
priority=GObject.PRIORITY_DEFAULT)
print(t)
subprocess.Popen(["/bin/bash", susp])
self.stop()
def stop(self):
Gtk.main_quit()
def get_screen():
scr = [s.split("x") for s in subprocess.check_output([
"xrandr"]).decode("utf-8").split() if "+0+0" in s][0]
return int(scr[0]) - 300
def convert_seconds(sec):
timedisplay = [
str(int(sec/3600)),
str(int((sec % 3600)/60)),
str(int(sec % 60)),
]
for i, n in enumerate(timedisplay):
if len(n) == 1:
timedisplay[i] = "0"+n
return ":".join(timedisplay)
def splashwindow():
window = CountDown()
window.set_decorated(False)
window.set_resizable(False)
window.override_background_color(Gtk.StateType.NORMAL, Gdk.RGBA(0,0,0,1))
window.modify_fg(Gtk.StateFlags.NORMAL, Gdk.color_parse(txtcolor))
window.set_opacity(0.6)
window.move(get_screen(), 80)
window.set_keep_above(True)
window.show_all()
Gtk.main()
GObject.threads_init()
splashwindow()
B.另存为runner.py
:
#!/usr/bin/env python3
import subprocess
import time
import os
import sys
window_mod = os.path.dirname(os.path.realpath(__file__))+"/win.py"
suspend = int(sys.argv[1])
countdown = int(sys.argv[2])
w = False
while True:
time.sleep(1)
idletime = int(subprocess.check_output(
"xprintidle"
).decode("utf-8").strip())/1000
if all([idletime > suspend-countdown, w == False]):
subprocess.Popen(["python3", window_mod, str(countdown)])
w = True
elif all([idletime < suspend-countdown, w == True]):
try:
procdata = subprocess.check_output([
"pgrep", "-f", window_mod
]).decode("utf-8").strip()
procs = procdata.splitlines()
except subprocess.CalledProcessError:
pass
else:
for p in procs:
subprocess.Popen(["kill", p])
w = False
C.保存(完全)为susp.sh
:
#!/bin/bash
sleep 3
systemctl suspend
使所有三个脚本都可执行,并再次确保它们位于一个目录中。
您实际上已经完成了。
测试脚本的挂起时间(应应用挂起之前的空闲时间),以及倒计时时间(以秒为单位)作为参数,例如:
/path/to/runner.py 600 300
要将空闲时间设置为10分钟,计数器会在暂停前5分钟启动。
如果一切正常,请将其添加到启动应用程序:Dash>启动应用程序>添加。添加命令:
/path/to/runner.py <idle_time> <countdown_time>
在的头部win.py
,您可以为显示的文本设置不同的颜色:
# --- set the color (index) below (1 is the first)
color = 1
# ---
textcolors = ["grey", "orange", "green", "blue", "white"]
播放各行中的值:
maingrid.set_border_width(10)
和
return int(scr[0]) - 200
(从函数开始get_screen
,其中200是窗口左侧到屏幕右侧的距离),以及
window.move(get_screen(), 35)
(其中35是窗口与屏幕顶部之间的距离),您可以轻松更改窗口的几何形状,例如:
玩得开心 :)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句