我可以在倒数计时窗口中显示下一次暂停之前的时间吗?

用户名

有时候,当我需要去某个地方时,我会把计算机保持未悬浮状态,但是我将它留给我绝对信任的人,并且我很快就会回来,所以没有真正的需要暂停(例如,如果是家庭,我只是跳到那是一间厕所)距离我所处的地方只有一小段距离,在这样的情况下始终挂起我的机器并不方便),或者可能有其他原因导致我想要这样做,例如想知道是否应该跑到我的机器上摆动鼠标进入睡眠状态,或者如果我可以较慢的速度进入计算机。

无论如何,要讲到这一点,我希望能够在屏幕右上角所有内容的顶部启动倒计时(最好是在透明窗口(不是透明窗口位))顶部栏下方)显示,它将向我显示下一个暂停时间的倒计时。

这可能会在我每次与机器交互时重置计时器,或者以某种方式直接与暂停系统交互以告知下一次暂停的时间(因为我假设存在某种倒计时)。如何实现这样的效果(当然,我不希望窗口始终处于打开状态,而是让我能够在启动窗口的任何时间运行命令,或者甚至可以是终端窗口中的倒数计时如果GUI位完全有问题,我会只是移动还是会自动位于右上角)?

我正在运行带有GNOME 3.18的Ubuntu GNOME 15.10,但是该解决方案也应该与带有GNOME 3.20的Ubuntu GNOME 16.04一起使用,因为我打算尽快进行升级。

雅各布·弗利姆(Jacob Vlijm)

编辑

在下面的原始答案中,倒计时窗口在任意空闲时间之后出现重新阅读您的问题,您可能会永久想要它。永久版本在下面(更简单),原始答案则更进一步。


1a。版本,永久显示倒计时时间

解决方案是一个后台脚本,显示一个半透明的倒计时窗口。该窗口的行为类似于通知:它始终在顶部可见,但是(当然)您可以照常在其他窗口中工作:

在此处输入图片说明 在此处输入图片说明 在此处输入图片说明 在此处输入图片说明

初始时间是应激活挂起之前的空闲时间。鼠标键盘事件会重置时间。

如图所示,脚本具有不同的预设颜色选项(请参见下文)。

如何设定

  1. 该脚本需要xprintidle

    sudo apt-get install xprintidle
    
  2. 将下面的脚本复制到一个空文件中,另存为 countdown.py
  3. 以空闲时间为参数运行它:

    python3 /path/to/countdown.py <idle_time>
    

    例如

    python3 /path/to/countdown.py 300
    

    5分钟后进入暂停状态。

  4. 如果一切正常,请将其添加到启动应用程序: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()

笔记

文字的颜色可以更改,如答案的第二个版本的最底部所述。

1b。根据注释中的要求:相同脚本的豪华版本:如果经过一半时间,文本颜色将变为黄色,而在暂停前30秒变为红色。

在此处输入图片说明> 在此处输入图片说明>使用它完全一样在此处输入图片说明

1a

剧本

#!/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()




2.原始答案:版本,显示x空闲时间后的倒数时间

下面的设置将显示倒计时(在任意时间长度内)到下一个暂停:

在此处输入图片说明

该窗口始终位于所有其他窗口的顶部,就像通知气泡一样。

该安装程序将替换“常规”挂起设置,这意味着您需要从系统设置中禁用挂起。

关于解决方案

在脚本中挂起的命令是:

 systemctl suspend

不需要sudo结果是您至少 15.04需要使用此解决方案。
该脚本是在Ubuntu(Unity)上编写和测试的15.10,但是其中没有特定于Unity的特定代码。我认为它可以在所有默认的Ubuntu版本上正常工作>15.04

怎么运行的

要进行设置(详细版本,请参见下面的详细信息),只需将涉及的三个脚本复制到一个相同的目录中即可,该目录的名称与所示的名称完全相同。要运行,只需运行主脚本(运行时间检查)。

  • 如果空闲时间超过某个限制,则会调用倒数计时窗口。
  • 如果在倒计时期间计算机变得空闲(鼠标或键盘事件),则窗口将关闭(其pid被终止)。
  • 如果计时器结束倒计时,它将运行一个简单的脚本来挂起

如何设定

  1. 该脚本需要xprintidle

    sudo apt-get install xprintidle
    
  2. 将以下三个脚本复制到单独的空文件中,将分别保存在一个和相同的目录下按照指示正确命名:

    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
    
  3. 使所有三个脚本都可执行,并再次确保它们位于一个目录中。

  4. 您实际上已经完成了。

    • 禁用“常规”暂停设置
    • 测试脚本的挂起时间(应应用挂起之前的空闲时间),以及倒计时时间(以秒为单位)作为参数,例如:

      /path/to/runner.py 600 300
      

      要将空闲时间设置为10分钟,计数器会在暂停前5分钟启动。

  5. 如果一切正常,请将其添加到启动应用程序:Dash>启动应用程序>添加。添加命令:

     /path/to/runner.py <idle_time> <countdown_time>
    

笔记

  1. 在的头部win.py,您可以为显示的文本设置不同的颜色:

    # --- set the color (index) below (1 is the first)
    color = 1
    # ---
    textcolors = ["grey", "orange", "green", "blue", "white"]
    
  2. 播放各行中的值:

    maingrid.set_border_width(10)
    

    return int(scr[0]) - 200 
    

    (从函数开始get_screen,其中200是窗口左侧到屏幕右侧的距离),以及

    window.move(get_screen(), 35)
    

    (其中35是窗口与屏幕顶部之间的距离),您可以轻松更改窗口的几何形状,例如:

    在此处输入图片说明

玩得开心 :)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Dart : 显示到下一次的时间

在下一次迭代之前使用暂停来遍历JSON对象

我如何看待使用Workrave之前的下一次休息还剩下多少时间?

我可以在不阻塞 UI 线程的情况下一次更新我的 TableView 项目吗?

我可以确定下一次React渲染时将使用最新状态值吗?

在进行下一次迭代之前,如何使我的for循环完全执行任务?

对于下一次迭代之前的循环延迟

查找下一次出现的时间,例如TemporalAdjuster

选择用户的下一次登录时间

下一次运行剩余时间

当非阻塞send()仅传输部分数据时,我们可以假定它在下一次调用时将返回EWOULDBLOCK吗?

是否可以确保在下一次迭代之前调用循环内局部对象的析构函数?

当我在 tkinter 中一次又一次按下开始和暂停按钮时,倒数计时器的速度会增加

2的下一次幂

js onkeypress 在下一次输入之前不起作用

如何在下一次迭代之前在JavaScript循环中添加延迟?

git:如何在提交之前获取下一次提交的树形哈希?

$ .get在下一次执行代码之前不起作用

如何在for循环中的下一次迭代之前等待承诺解决

单击以启动倒数计时器,再单击一次以在同一按钮中暂停计时器

入睡直到下一次出现特定时间

RxJS :(时间)在下一次发射后开始的缓冲区

计算当前时间与熊猫下一次失败之间的时差

CloudWatch速率表达的下一次运行时间

如果foreach循环花费的时间太长,则继续下一次迭代

设置backoffCoefficient时,如何为持久功能计算下一次重试时间?

SQL在X天数内找到下一次跟进(加入比较日期时间)

计算下一次并四舍五入日期时间

jQuery keydown事件只能每秒钟按下一次吗?