CairoAlphaGladeApp

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

Cairo2GladeApp.png

Cairo2GladeApp.png

cairo2gladeapp.glade

Cairo2GladeApp.py

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

from SimpleGladeApp import SimpleGladeApp
from math import pi
import gtk
import cairo
import cairo.gtk

class Cairo2GladeApp(SimpleGladeApp):

        def new(self):
                self.ctx = cairo.Context()

        def on_expose(self, area, event):
                self.width, self.height = self.drawingarea1.window.get_size()
                cairo.gtk.set_target_drawable(self.ctx, self.drawingarea1.window)
                self.ctx.set_line_width(4)
                self.ctx.set_line_join(cairo.LINE_JOIN_BEVEL)
                self.ctx.set_rgb_color (0,0,0)
                self.draw_square()
                self.draw_arc()

        def draw_arc(self, *args):
                x = 2 * self.width  /4
                y = 3 * self.height /6
                radius = self.width /3
                angle1 = 0 * (pi/180)
                angle2 = 360 * (pi/180)
                self.ctx.arc(x, y, radius, angle1, angle2)
                self.ctx.save()
                self.ctx.set_rgb_color(3, 0.2, 0.2)
                self.ctx.fill()
                self.ctx.restore()
                self.ctx.stroke()

        def draw_square(self, *args):
                x = self.width /4
                y = self.height/4
                w = self.width/2
                h = self.height/2
                self.ctx.rectangle(x,y,w,h)
                self.ctx.save ()
                self.ctx.set_rgb_color (0, 0, 2)
                self.ctx.fill()
                self.ctx.restore()
                self.ctx.stroke()

        def on_value_changed(self, *args):
                alpha = self.hscale2.get_value()
                self.ctx.set_alpha(float(alpha))
                value = self.hscale1.get_value()
                self.ctx.set_tolerance(value)
                self.drawingarea1.queue_draw()

app = Cairo2GladeApp('cairo2gladeapp.glade')
app.run()