intertwingly

It’s just data

gtk StatusIcon


From time to time, I have found a need for limited communication with an otherwise background process.  A tray icon serves this purpose well.  Instead of dynamically generated SVG, you certainly can use Cairo or static filesTooltips, blinking, callbacks, and menus are also possibilities.

# enable cron
import os
if not 'DISPLAY' in os.environ:
  os.environ['DISPLAY']=':0.0'
if not 'HOME' in os.environ:
  os.environ['HOME']=os.path.expanduser('~'+os.getlogin())

import gtk
icon = gtk.StatusIcon()

def set_image(color):
  import rsvg
  image = """
    <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100">
      <path d="M42,95L50,30L58,95Z" fill="%s" stroke="none"/>
      <path d="M39,20a15,15 0,0,0 0,20 M28,10a30,30 0,0,0 0,40
        M61,20a15,15 0,0,1 0,20 M72,10a30,30 0,0,1 0,40" stroke="%s"
        fill="none" stroke-width="5" stroke-linecap="round"/>
    </svg>
  """
  handle=rsvg.Handle()
  handle.write(image % (color,color))
  handle.close()
  try:
    gtk.gdk.threads_enter()
    icon.set_from_pixbuf(handle.get_pixbuf())
  finally:
    gtk.gdk.threads_leave()
  
# spin GTK off in a background thread
import threading
gtk.gdk.threads_init()
class gtkmain(threading.Thread):
  def run(self):
    gtk.main()
gtkmain().start()

# user logic
import time
set_image('red')
time.sleep(3)
set_image('yellow')
time.sleep(3)
set_image('green')
time.sleep(3)
gtk.main_quit()