#!/usr/bin/env python # A progress bar that represents how much time awake you have spent and is remaining, and reminds you using notify-send to go to sleep # By Evan Boldt for the EchoWarp blog - GPL v2 import pygtk pygtk.require('2.0') import gtk, gobject import time import os #execute commands to use notify-send #Fill in based on 24 hour system wakehour=8 #what hour do you usually wake up? This is used to accurately represent how much time you have in your day wakeminute=30 #what minute do you usually wake up? bedhour=22 #what hour do you want to go to sleep? Cannot be after midnight bedminute=30 #what minute? reminderfrequency=5 #How often do you want to be reminded to go to bed, in minutes minutesinday= (bedhour * 60) - (wakehour * 60) + bedminute - wakeminute # Update the value of the progress bar so that we get # some movement def progress_timeout(pbobj): if pbobj.activity_check.get_active(): pbobj.pbar.pulse() else: # Calculate the value of the progress bar using the # value range set in the adjustment object hour= float(time.strftime("%H")) - wakehour minute= float(time.strftime("%M")) - wakeminute minutes = (hour * 60) + minute timepercent= minutes / minutesinday new_val = timepercent if (((minutes - minutesinday % reminderfrequency == 0) and (minutes > minutesinday)) ) and (time.strftime("%s" == "0")) : os.system('DISPLAY=:0 notify-send -t 10000 "Go to sleep" "Current Time: `date +"%l"`:`date +"%M"` `date +"%p"`" ') if new_val >= 1.0 or new_val < 0.0: pbobj.pbar.pulse() pbobj.pbar.set_text("Go to sleep!") else: # Set the new value pbobj.pbar.set_fraction(new_val) minutesleft = int(minutesinday - minutes) if minutesleft % 60 >= 10 : minutesleftThishour = str(minutesleft % 60) else : minutesleftThishour = "0" + str(minutesleft % 60) pbobj.pbar.set_text("(%.1f" % (new_val * 100) + "%) - " + str(minutesleft/ 60) + ":" + str(minutesleftThishour) + " hours remaining" ) # As this is a timeout function, return TRUE so that it # continues to get called return True class ProgressBar: # Callback that toggles the text display within the progress # bar trough # Callback that toggles the activity mode of the progress # bar def toggle_activity_mode(self, widget, data=None): self.pbar.set_fraction(0.0) # Callback that toggles the orientation of the progress bar def toggle_orientation(self, widget, data=None): if self.pbar.get_orientation() == gtk.PROGRESS_LEFT_TO_RIGHT: self.pbar.set_orientation(gtk.PROGRESS_RIGHT_TO_LEFT) elif self.pbar.get_orientation() == gtk.PROGRESS_RIGHT_TO_LEFT: self.pbar.set_orientation(gtk.PROGRESS_LEFT_TO_RIGHT) # Clean up allocated memory and remove the timer def destroy_progress(self, widget, data=None): gobject.source_remove(self.timer) self.timer = 0 gtk.main_quit() def __init__(self): self.window = gtk.Window(gtk.WINDOW_TOPLEVEL) self.window.set_resizable(True) self.window.connect("destroy", self.destroy_progress) self.window.set_title("Time Left in Your Day") self.window.set_border_width(0) vbox = gtk.VBox(False, 5) vbox.set_border_width(1) self.window.add(vbox) vbox.show() # Create a centering alignment object align = gtk.Alignment(0, 0, 0, 0) vbox.pack_start(align, False, False, 10) align.show() # Create the ProgressBar self.pbar = gtk.ProgressBar() self.pbar.set_size_request(400, 25) align.add(self.pbar) self.pbar.show() # Add a timer callback to update the value of the progress bar self.timer = gobject.timeout_add (100, progress_timeout, self) # Add a check button to select displaying of the trough text # Add a check button to toggle activity mode self.activity_check = check = gtk.CheckButton("Activity mode") check.connect("clicked", self.toggle_activity_mode) check.show() self.window.set_resizable(False) #this window is not resizable #self.window.set_decorated(False) #remove the title bar #self.window.set_opacity(0.8) #make the window semi transparent self.window.stick() #make the winodw visible on all desktops #self.window.set_keep_above(True) #always on top #self.window.set_keep_below(True) #always on bottom self.window.show() def main(): gtk.main() return 0 if __name__ == "__main__": ProgressBar() main()