Skip to content
tbaugis edited this page Dec 9, 2012 · 34 revisions

The hamster graphics library is a sprite-styled abstraction layer on top of cairo. In short it simplifies drawing things in gnome and python at the same time giving you also full access to the underlying Cairo functions.
Hamster graphics library branched of and is used by Project Hamster – the GNOME time tracker.

Repository Contents

Best way to get started is to check out the source and run examples:

git clone git://github.com/projecthamster/experiments.git
cd experiments
./bouncy_bubbles.py

All the scripts in root folder of the checkout are examples and experiments – the core bit is in the /lib folder.

Hello world

import gtk
from lib import graphics

class Scene(graphics.Scene):
    def __init__(self):
        graphics.Scene.__init__(self)
        self.add_child(graphics.Label("Hello World", 24, "#000", x = 5, y = 5))

window = gtk.Window(gtk.WINDOW_TOPLEVEL)
window.add(Scene())
window.show_all()
gtk.main()

In order to get started with hamster graphics library, apart from pygtk and cairo (and those most probably are already there on your linux box) you will need just one file – the graphics.py located in the lib folder. Feel free to place where handy.

The main canvas object on which we are doing the drawing, is called Scene. It is named so because it can also do motion, but we will get to that later. In constructor we add a label to the scene. Label is one of the simple objects at your disposal. There is also circle, rectangle (with optionally rounded corners) and polygon. And all the custom drawing can be done sub-classing the Sprite class.

Functionality

Here is what you get:

  • shape accurate mouse events and dragging
  • animation
  • sprite style approach
  • drawing without waiting for context (explained in tutorial)
  • but when necessary – falling back to the cairo context