We're back after a server migration that caused effbot.org to fall over a bit harder than expected. Expect some glitches.

An AggDraw-based WCK View

Updated October 15, 2005 | August 21, 2005 | Fredrik Lundh

This is a simple WCK widget base class, which uses the AggDraw library to render the widget contents.

To use this widget, create a subclass and implement the agg_draw method.

# File: wckAggView.py

from WCK import Widget

import aggdraw

class AggView(Widget):

    ui_option_width = 100
    ui_option_height = 100

    def ui_handle_config(self):
        return int(self.ui_option_width), int(self.ui_option_height)

    def ui_handle_resize(self, width, height):
        self.size = width, height
        self.image = None

    def ui_handle_clear(self, draw, x0, y0, x1, y1):
        pass # ignore clear events

    def ui_handle_repair(self, draw, x0, y0, x1, y1):
        if self.image is None:
	    # create agg drawing context
            ink = self.winfo_rgb(self.ui_option_background)
            ink = "#%02x%02x%02x" % (ink[0]/256, ink[1]/256, ink[2]/256)
            d = aggdraw.Draw("RGB", self.size, ink)
	    self.agg_draw(d, 0, 0, self.size[0], self.size[1])
            # update pixmap
            self.image = self.ui_image(d.mode, d.size, d.tostring())
        draw.paste(self.image)

    ##
    # Forces redraw.

    def agg_damage(self, x0=None, y0=None, x1=None, y1=None):
        self.image = None
        self.ui_damage(x0, y0, x1, y1)

    ##
    # (Hook) Called to draw the AGG view contents.  This is
    # called whenever the view needs to be redrawn.

    def agg_draw(self, draw, x0, y0, x1, y1):
        pass

Note that if you’re using WCK 1.1a1 or earlier, the “update pixmap” code should be replaced with:

            # update pixmap
            from PIL import Image
            im = Image.fromstring(d.mode, d.size, d.tostring())
            self.image = self.ui_image(im)

Here’s a short example:

import Tkinter

root = Tkinter.Tk()

class MyView(AggView):
    def agg_draw(self, draw, x0, y0, x1, y1):
        draw.line((x0, y0, x1, y1), aggdraw.Pen("black", 10))
        draw.line((x0, y1, x1, y0), aggdraw.Pen("black", 10))

widget = MyView(root, width=200, height=300)
widget.pack(fill="both", expand=1)

root.mainloop()