Programmer's Wiki

cairo is a graphics library for GTK+, and provides some functions for drawing some fancier things than GTK+ or X11 does.

in a window[]

The clock-face tutorial is a good starting point to see how you can use cairo to draw stuff. It shows how to use various drawing commands directly on your GTK+ window. You can also have a look at the second part of the clock-face tutorial.

in a widget[]

To get cairo drawing commands to work in a widget, you have to get your head around GTK+ widgets. There is a tutorial courtesy of PyGTK.

The following incomplete code can be used with the clock-face code to draw the clock face in a widget, next to some buttons. Our first step is to create the object that will do the cairo drawing. The code for this class is in the clock-face tutorial. The size request is mysterious to me.

 def main():
     clock = EggClockFace()

The next step is to create a bunch of widgets, but don't knit them together.

     window = gtk.Window()
     hbox = gtk.HBox()
     but1 = gtk.Button("What's up?")
     but2 = gtk.Button("Dammit")
     ebox = gtk.EventBox()

Now knit them together. I think the order is not important here.


To make the program do something, we connect some signals to actions. Note that that the clock can receive signals, but doesn't receive all signals. For example, we have to attach button_press_event to the event box in which the clock lives. On the other hand, the clock receives screen-changed messages. If anyone can explain what's up with that, please comment.

     but1.connect("clicked", gtk.main_quit)
     ebox.connect_object("button_press_event", clock.togglecolor, clock)
     clock.connect("screen-changed", clock.screen_changed)
     window.connect("destroy", gtk.main_quit)

Now we are done. We continue as usual.