I wanted to use this from my mail client's calendar as an alarm that will get my attention. I also wanted to display a message so I would know why the clock poped up. One problem with xclock is that it's strftime format does not handle the newline characther '%n', so I couldn't fit a message properly in the display. So I created two small Python scripts that add those features I wanted:
1) Launch the digital clock with a custom message specified on the command line
2) If the clock was already launched, running the script again will terminate the existing clock
Launching script (~/bin/my_alarm):
Code:
#!/usr/bin/python
# :mode=python: jEdit modeline
import os
import sys
import subprocess
import time
if __name__ == "__main__":
not_active = True
# Check if the alarm gui is up
p = subprocess.Popen(['ps','u'], stdout = subprocess.PIPE)
stdout, stderr = p.communicate()
lines = stdout.split("\n")
for line in lines:
if "my_alarm_gui.py" in line and "/usr/bin/python" in line:
not_active = False
tokens = line.split()
pid = tokens[1]
os.system("kill -TERM %s" % pid)
if not_active:
os.system("~/bin/my_alarm_gui.py %s &" % " ".join(sys.argv[1:]))
time.sleep(0.5)
os.system('wmctrl -r "My Alarm Gui" -b add,fullscreen,above')
my_alarm script launches the digital clock implemented in Python and TK (~/bin/my_alarm_gui.py):
Code:
#!/usr/bin/python
from Tkinter import *
import sys
import time
def tick(title):
global time1
# get the current local time from the PC
time2 = time.strftime(title + '%n%F %H:%M:%S')
# if time string has changed, update it
if time2 != time1:
time1 = time2
clock.config(text=time2)
# calls itself every 200 milliseconds
# to update the time display as needed
# could use >200 ms, but display gets jerky
clock.after(200, tick, title)
if __name__ == "__main__":
root = Tk()
root.title("My Alarm Gui")
time1 = ''
clock = Label(root, font=('courier', 96, 'bold'), bg='black', fg="white")
clock.pack(fill=BOTH, expand=1)
title = " ".join(sys.argv[1:])
tick(title)
root.mainloop( )
Example usage:
$ my_alarm Some kind of message
# The message is displayed and becomes full screen
# Switch to another desktop screen, open a shell
$ my_alarm
# The fullscreen digital clock will disappear.
Bookmarks