Fun With Python 1: totem applet

I listen to a lot of podcasts, and I use totem to do it. However I would like to be able to pause the player without having it take up all that screen real estate. What we need is an applet! Thankfully python is super compact and the gnome bindings are great, so this barely takes 75 lines.


Put this file in /usr/local/bin and make it executable (sudo chmod +x /usr/local/bin/totem-applet.py)

totem-applet.py

#!/usr/bin/env python
import pygtk
import sys
pygtk.require('2.0')

import gtk
import gnomeapplet

import subprocess

def sample_factory(applet, iid):
	hbox = gtk.HBox()
	label = gtk.Label("Totem:")
	hbox.add(label)
	
	button = gtk.Button("")
	image = gtk.Image()
	image.set_from_stock("gtk-media-previous",gtk.ICON_SIZE_BUTTON)
	button.set_image(image)
	button.connect("clicked",previous_clicked)
	hbox.add(button)
	
	button = gtk.Button("")
	image = gtk.Image()
	image.set_from_stock("gtk-media-play",gtk.ICON_SIZE_BUTTON)
	button.set_image(image)
	button.connect("clicked",play_clicked)
	hbox.add(button)
	
	button = gtk.Button("")
	image = gtk.Image()
	image.set_from_stock("gtk-media-pause",gtk.ICON_SIZE_BUTTON)
	button.set_image(image)
	button.connect("clicked",pause_clicked)
	hbox.add(button)
	
	button = gtk.Button("")
	image = gtk.Image()
	image.set_from_stock("gtk-media-next",gtk.ICON_SIZE_BUTTON)
	button.set_image(image)
	button.connect("clicked",next_clicked)
	hbox.add(button)
	
	applet.add(hbox)
	applet.show_all()
	return True

def previous_clicked(event):
	subprocess.Popen(("totem","--previous"))
	
def play_clicked(event):
	subprocess.Popen(("totem","--play"))
	
def pause_clicked(event):
	subprocess.Popen(("totem","--pause"))
	
def next_clicked(event):
	subprocess.Popen(("totem","--next"))
	
if len(sys.argv) == 2:
	if sys.argv[1] == "run-in-window":   
		main_window = gtk.Window(gtk.WINDOW_TOPLEVEL)
		main_window.set_title("Python Applet")
		main_window.connect("destroy", gtk.main_quit) 
		app = gnomeapplet.Applet()
		sample_factory(app, None)
		app.reparent(main_window)
		main_window.show_all()
		gtk.main()
		sys.exit()
        
gnomeapplet.bonobo_factory("OAFIID:GNOME_TotemApplet_Factory", 
                                     gnomeapplet.Applet.__gtype__, 
                                     "simple remote control", "1.0", sample_factory)

You’ll also need totem-applet.server, which should be copied to /usr/lib/bonobo/servers/. Then you can right click on a panel and add the Totem Applet like any other applet.

One thought on “Fun With Python 1: totem applet”

Comments are closed.