CairoCubeGladeApp

This example requires SimpleGladeApp.py, don't forget to download it.

Cairo4GladeApp.png

Cairo4GladeApp.png

cairo4gladeapp.glade

Cairo4GladeApp.py

#!/usr/bin/env python
# -*- coding UTF-8 -*-

from SimpleGladeApp import SimpleGladeApp
import gtk
import cairo
import cairo.gtk

class Cairo4GladeApp(SimpleGladeApp):
        def new(self):
                self.elapsed = 95
                self.count = 0
                self.on_timeout()

        def on_timeout(self):
                gtk.timeout_add(self.elapsed, self.on_timeout)
                self.count += 0.01
                if self.count > 1:
                        self.count = 0
                self.drawingarea1.queue_draw()

        def on_expose_event(self, area, event):
                self.ctx = cairo.Context()
                cairo.gtk.set_target_drawable(self.ctx, self.drawingarea1.window)
                width, height = self.drawingarea1.window.get_size()
                self.ctx.scale(width/100.0, height/100.0)

                self.ctx.set_rgb_color(0,0,0)
                self.ctx.rectangle(0, 0, width, height)
                self.ctx.fill()

                self.draw_rectangle1()
                self.draw_rectangle4()
                self.draw_rectangle2()
                self.draw_rectangle3()

        def draw_rectangle1(self):
                self.ctx.set_line_width(0.3)
                self.ctx.set_rgb_color(1, 1, 1)
                self.ctx.rectangle(27, 27, 30, 30)
                self.ctx.save()
                self.ctx.set_alpha(self.count)
                self.ctx.set_rgb_color(0.4, 0.7, 0.9)
                self.ctx.fill()
                self.ctx.restore()
                self.ctx.stroke()

        def draw_rectangle2(self):
                self.ctx.rectangle(39, 39, 30, 30)
                self.ctx.save()
                self.ctx.set_alpha(self.count)
                self.ctx.set_rgb_color(0.3, 0.1, 0.2)
                self.ctx.fill()
                self.ctx.restore()
                self.ctx.stroke()

        def draw_rectangle3(self):
                self.ctx.set_line_width(0.2)
                self.ctx.move_to(27, 27)
                self.ctx.rel_line_to(30, 0)
                self.ctx.rel_line_to(12, 12)
                self.ctx.rel_line_to(-30, 0)
                self.ctx.close_path()
                self.ctx.save()
                self.ctx.set_alpha(self.count)
                self.ctx.set_rgb_color(0.8, 0.4, 0.5)
                self.ctx.fill()
                self.ctx.restore()
                self.ctx.stroke()

        def draw_rectangle4(self):
                self.ctx.move_to(27, 57)
                self.ctx.rel_line_to(30, 0)
                self.ctx.rel_line_to(12, 12)
                self.ctx.rel_line_to(-30, 0)
                self.ctx.close_path()
                self.ctx.save()
                self.ctx.set_alpha(self.count)
                self.ctx.set_rgb_color(0.2, 0.4, 0.2)
                self.ctx.fill()
                self.ctx.restore()
                self.ctx.stroke()

app = Cairo4GladeApp('cairo4gladeapp.glade')
app.run()