The Artima Developer Community
Sponsored Link

Python Buzz Forum
Counting down in Python

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Ben Last

Posts: 247
Nickname: benlast
Registered: May, 2004

Ben Last is no longer using Python.
Counting down in Python Posted: Mar 17, 2013 4:06 AM
Reply to this message Reply

This post originated from an RSS feed registered with Python Buzz by Ben Last.
Original Post: Counting down in Python
Feed Title: The Law Of Unintended Consequences
Feed URL:
Feed Description: The Law Of Unintended Consequences
Latest Python Buzz Posts
Latest Python Buzz Posts by Ben Last
Latest Posts From The Law Of Unintended Consequences

I have events coming up in my life; some are good and some are not so good. Anticipating the good ones is positive, and one way to do that is to have a little countdown visible on my desktop. I run Ubuntu Linux on my home laptop, and so I wanted a way to drop a countdown into the menu bar (where Unity shows indicator output).

Unity (the Ubuntu window manager/interface) does this using indicators (yet another term to learn for gadgets). It took about 30 minutes to work out how to knock up a little indicator in Python, and here it is for anyone who needs it. I based it on a bare-bones clock indicator: the link to the original is in the comments.

#!/usr/bin/env python
# Unity indicator for countdown to a fixed date
# author: ben last
# based on Phil Ayres' clock indicator
# (see
# 17 Mar 2013
import gobject
import gtk
import appindicator
import os, sys
import time

#Define the target time as a time_t

def on_left_click(widget,ind):
def on_timer(ind):
  """Update the label - we always use local time."""

  #Get the local time as a time_t (seconds since the epoch)
  #Get the offset to the target such that a positive value
  #means the target is in the future, in seconds.  Also save
  #in offset, so we can spot the end of the countdown.

  #If we're at or past the target, reset to zero
  if seconds < 0: seconds=0

  #Convert to days, hours, minutes and seconds
  (days,seconds) = divmod(seconds,24*60*60)
  (hours,seconds) = divmod(seconds,60*60)
  (minutes,seconds) = divmod(seconds,60)

  #Generate the label

  #Debug output
  #sys.stdout.write("%s %s\n"%(offset,label))
  #ind is the indicator - setting the label shows the countdown
  #in the indicator area.

  #returning False means no more calls to this method
  return offset>=0

if __name__ == "__main__":
    #Create the indicator with a clock icon
    ind = appindicator.Indicator("simple-countdown",
    ind.set_status (appindicator.STATUS_ACTIVE)

    #Set the label

    #Creata a menu and an item
    menu = gtk.Menu()
    item = gtk.MenuItem("Quit")

    item.connect("activate", on_left_click, ind)

    #Start a one-second tick
    gtk.timeout_add(1000, on_timer, ind)


Read: Counting down in Python

Topic: New Blog Software Previous Topic   Next Topic Topic: You can call me The Doctor

Sponsored Links


Copyright © 1996-2018 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use