Fandom

Programmer's Wiki

Cairo

407pages on
this wiki
Add New Page
Talk0 Share

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

in a windowEdit

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 widgetEdit

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()
     clock.set_size_request(110,50)

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.

     hbox.pack_start(but1)
     hbox.pack_start(but2)
     hbox.pack_start(ebox)
     ebox.add(clock)
     window.add(hbox)

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.

     window.show_all()

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.