The notification bubble is a well known feature of Ubuntu, gently informing us when we are online, when we get tweets and new email and so on. It has an interesting feature that it commands attention for a few moments, but doesn't get in the way and the user can return to what they were doing without really stopping. At the moment I am trying to learn a bit of Swedish and wondered if these attributes might help when learning new words. What if I could use the notification bubble to show me words at random intervals so grab my attention momentarily while using my computer?
Programming the notification bubble is very straightforward. A very helpful post in Milinda Pathirage's blog explained how to use the notification bubble from the shell, C++ and Python. I picked the Python option as it is a language I've been using a fair bit recently and have grown to really like. The script explained how to display one notification but what I wanted to do was display a number of notifications with a random time gap between them. The notifications would display a digit and the Swedish word for a number and I wanted to be able to switch the script between displaying the numbers in a random order or in sequence.
At the moment the script has only the number one to ten in it, but it could be easily expanded. The script is quite short and shown below. After the import statements is a Python tuple containing the words I want to learn. The “pynotify.init()” line actually specifies how the notification bubble will be set out. In this example I have gone for a simple notification, one with just a single text line, but you can have multiple lines and icons too. The options available are explained at in the “Layout cases (with examples in C, Python and C#)” of the Notification Development Guidelines entry to the Ubuntu Wiki. The icons could prove useful for learning other words, a picture could be displayed along side the Swedish word for an object to provide a visually interesting way to learn new words.
# -*- coding: utf-8 -*-
from optparse import OptionParser
# what I'm trying to learn!
swenum = ("en/ett", "två", "tre", "fyra", "fem", "sex", "sju", "åtta", "nio", "tio")
if __name__ == "__main__":
# Check whether your notification agent supports the
# summary only layout.
if not pynotify.init("summary-only"):
# parse command line
usage = "usage: %prog [options]"
parser = OptionParser(usage)
parser.add_option("-r","--random", action="store_true", dest="random_mode", help="Display numbers in a random order rather than sequentially")
parser.add_option("-n","--number", type="int", dest="num_notifications", default=10, help="Number of notifications to display")
(options, args) = parser.parse_args()
num = 0
# generate all of the notifications required
for i in range(0, options.num_notifications):
# pick the next number randomly or sequentially
num = random.randrange(1,10)
num = (num % 10) + 1
# display the notification
n = pynotify.Notification(
"%d %s" % (num, swenum[num-1]))
# pause the script for a random period between 30 seconds and two minutes
slp = random.randrange(30,120)
As well as an option to show numbers in a random order, I wanted an option to control how many notifications are displayed so I can use the program for varying lengths of time. To handle passing these options through the command line I used the elegant OptionParser class. This makes sure arguments passed are understood, defaults are provided and also has the added advantage of also providing a “--help” option to your script too. The lines immediately below deal with generating a number either randomly or sequentially then displaying the notification itself. The last two lines cause the script to be put to “sleep” (meaning it will pause) for random period between thirty seconds and two minutes before generating the next notification.
So far I'm finding the script is helping me memorise some new words and it does so without getting in the way. Hopefully I will be able to use this script to build up my vocabulary in this language further.