我如何制作一个简单的指示程序来记录时间戳?

我如何制作一个简单的指示程序来记录时间戳?

我想制作一个按钮,放在声音/无线/等指示器旁边,当切换时,它会改变颜色并将系统时间与单词“开始”一起写入文件,然后,当再次切换时,它会返回到其原始颜色并使用单词“结束”记录系统时间。理想情况下,即使系统重新启动,按钮也会保持其切换/取消切换状态。

我无法想象这会是一个难以制作的程序,但是我对 Ubuntu 有点陌生,我不确定如何让程序作为指示器工作,或者如何让某些东西在系统重启时保持其状态。

答案1

我很感兴趣,所以这里是我的 Python 代码:

#!/usr/bin/python

from time import time, ctime
from gi.repository import Gtk
from gi.repository import AppIndicator3 as Indicator


MY_FILENAME = '/home/your-username/myfile.log'
MY_INDICATOR_ID = 'my-indicator-example'
STOP_ICON_NAME = 'indicator-messages'
START_ICON_NAME = 'indicator-messages-new'

class MyIndicator(object):

    def __init__(self, filename):
        self.indicator = Indicator.Indicator.new(
                                MY_INDICATOR_ID,
                                STOP_ICON_NAME,
                                Indicator.IndicatorCategory.APPLICATION_STATUS)

        self.indicator.set_status(Indicator.IndicatorStatus.ACTIVE)
        self.indicator.set_attention_icon(START_ICON_NAME)

        self.has_started = False

        self.menu = Gtk.Menu()

        start_item = Gtk.MenuItem('Start')
        start_item.connect('activate', self.start_activated, filename)
        self.menu.append(start_item)
        start_item.show()

        stop_item = Gtk.MenuItem('Stop')
        stop_item.connect('activate', self.stop_activated, filename)
        self.menu.append(stop_item)
        stop_item.show()

        self.indicator.set_menu(self.menu)

    def main(self):
        Gtk.main()

    def start_activated (self, menu_item, filename):
        if not self.has_started:
            myfile = open(filename, 'a')
            myfile.write('Start  <%s>\n' % ctime(time()))
            self.indicator.set_status(Indicator.IndicatorStatus.ATTENTION)
            self.has_started = True

    def stop_activated (self, menu_item, filename):
        if self.has_started:
            myfile = open(filename, 'a')
            myfile.write('End    <%s>\n' % ctime(time()))
            self.indicator.set_status(Indicator.IndicatorStatus.ACTIVE)
            self.has_started = False


if __name__ == '__main__':
    indicator = MyIndicator(MY_FILENAME)
    indicator.main()

我使用了gpaste 指示器Ubuntu 维基。我不确定“在重启之间保持状态”功能,最简单的方法是阅读最后一行myfile.log并进行self.has_started相应设置。祝你好运。

答案2

我能够修改和改进 edwin 非常有帮助的答案中的代码,以便应用程序在重启之间保持状态!我现在一直在使用它。

对于其他想要使用它的人,这里是(也可用在 pastebin 上):

#!/usr/bin/python

from time import time, ctime
from gi.repository import Gtk
from gi.repository import AppIndicator3 as Indicator
from datetime import datetime, date


MY_FILENAME = '/home/anotherghost/workhours.log'
MY_INDICATOR_ID = 'work-hours-indicator'
STOP_ICON_NAME = 'indicator-messages'
START_ICON_NAME = 'indicator-messages-new'
FMT = "%a %b %d %H:%M:%S %Y"

class MyIndicator(object):

    def __init__(self, filename):
        self.indicator = Indicator.Indicator.new(
                                MY_INDICATOR_ID,
                                STOP_ICON_NAME,
                                Indicator.IndicatorCategory.APPLICATION_STATUS)

        self.menu = Gtk.Menu()

        start_item = Gtk.MenuItem('Start')
        start_item.connect('activate', self.start_activated, filename)
        self.menu.append(start_item)
        start_item.show()

        stop_item = Gtk.MenuItem('Stop')
        stop_item.connect('activate', self.stop_activated, filename)
        self.menu.append(stop_item)
        stop_item.show()

        showall_item = Gtk.MenuItem('Show all')
        showall_item.connect('activate', self.showall, filename)
        self.menu.append(showall_item)
        showall_item.show()

        self.indicator.set_menu(self.menu)
        self.indicator.set_attention_icon(START_ICON_NAME)

        with open(MY_FILENAME,'r') as fn:
            try:
                fn.seek(-70, 2)
            except (IndexError, IOError):
                fn.seek(max(-len(fn.read().decode()),-69),2)
            self.last = fn.readlines()[-1].decode()
            if len(self.last) > 1 and len(self.last) < 30:
                self.last = self.last.split(" ")
                self.last[-1] = self.last[-1][:4]
                self.last = " ".join(self.last)
                self.start_time = datetime.strptime(self.last, FMT)
                self.indicator.set_status(Indicator.IndicatorStatus.ATTENTION)
                self.has_started = True
                self.end_time = datetime.now()
            else:
                self.indicator.set_status(Indicator.IndicatorStatus.ACTIVE)
                self.has_started = False
                self.start_time = datetime.now()
                self.end_time = datetime.now()


    def main(self):
        Gtk.main()

    def start_activated (self, menu_item, filename):
        if not self.has_started:
            myfile = open(filename, 'a')
            self.start_time = datetime.now()
            myfile.write('%s' % self.start_time.strftime(FMT))
            self.indicator.set_status(Indicator.IndicatorStatus.ATTENTION)
            self.has_started = True
            myfile.close()

    def stop_activated (self, menu_item, filename):
        if self.has_started:
            myfile = open(filename, 'r+')
            myfile.seek(-1, 2)
            if myfile.read(1) == '\n':
                myfile.seek(-1, 2)
            self.end_time = datetime.now()
            self.elapsed_time = self.end_time - self.start_time
            myfile.write('\t%s\t' % self.end_time.strftime(FMT))
            myfile.write('%s\n' % round(float(self.elapsed_time.seconds)/3600,3) )
            self.indicator.set_status(Indicator.IndicatorStatus.ACTIVE)
            self.has_started = False
            myfile.close()

    def showall (self, menu_item, filename):
        myfile = open('op.log', 'a')
        myfile.write('has_started: %s\n' % self.has_started)
        myfile.write('start_time: %s\n' % self.start_time.strftime(FMT))
        myfile.write('end_time: %s\n' % self.end_time.strftime(FMT))


if __name__ == '__main__':
    indicator = MyIndicator(MY_FILENAME)
    indicator.main()

相关内容