simple, old-style graphic ?

Fredrik Lundh fredrik at pythonware.com
Tue Jun 10 19:30:18 CEST 2003


Alessandro de Manzano wrote:

> I'd need a little suggestion :-)
>
> For educational purposes I'ld find a Python module (Windows platform) to
> draw simple 2D graphic (even 3D if possible, but is not a requirement)
> Like very old Basics, so lines, points, circles, rectangles, colors, simple
> text strings, etc.
>
> As last resort I thought about some OpenGL module but I would really prefer
> something easier and simpler.
>
> Have you some idea ?

is this simple enough?

from Tkinter import *

root = Tk()
root.title("WCK PixelCanvas")

canvas = PixelCanvas(root, background="white")
canvas.pack()

# create the pixmap!
canvas.update()

canvas.draw_line((0, 0, 100, 100), "red")
canvas.draw_line((0, 100, 100, 0), "blue", 10)
canvas.draw_rect((100, 100, 200, 200), "gold")
canvas.draw_ellipse((50, 50, 150, 150), "gold", "black")
canvas.draw_image((200, 100), PhotoImage(file="guido.gif"))
canvas.draw_text((100, 200), "WCK PixelCanvas", font="Helvetica 30")

root.mainloop()

where the PixelCanvas is defined as:

from WCK import Widget

class PixelCanvas(Widget):

    ui_option_width = 640
    ui_option_height = 480

    def __init__(self, master, **options):
        self.pixmap = self.size = None
        self.ui_init(master, options)

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

    def ui_handle_resize(self, width, height):
        if (width, height) != self.size:
            background = self.ui_brush(self.ui_option_background)
            pixmap = self.ui_pixmap(width, height)
            pixmap.rectangle((0, 0, width, height), background)
            if self.pixmap:
                pixmap.paste(self.pixmap)
            self.pixmap = pixmap
            self.size = width, height

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

    def ui_handle_repair(self, draw, x0, y0, x1, y1):
        if self.pixmap:
            draw.paste(self.pixmap)
        else:
            background = self.ui_brush(self.ui_option_background)
            draw.rectangle((x0, y0, x1, y1), background)

    def draw_line(self, xy, stroke="black", stroke_width=1):
        if self.pixmap:
            if stroke: stroke = self.ui_pen(stroke, stroke_width)
            self.pixmap.line(xy, stroke)
            self.ui_damage()

    def draw_rect(self, xy, fill=None, stroke=None, stroke_width=1):
        if self.pixmap:
            if fill: fill = self.ui_brush(fill)
            if stroke: stroke = self.ui_pen(stroke, stroke_width)
            self.pixmap.rectangle(xy, fill, stroke)
            self.ui_damage()

    def draw_ellipse(self, xy, fill=None, stroke=None, stroke_width=1):
        if self.pixmap:
            if fill: fill = self.ui_brush(fill)
            if stroke: stroke = self.ui_pen(stroke, stroke_width)
            self.pixmap.ellipse(xy, fill, stroke)
            self.ui_damage()

    def draw_polygon(self, xy, fill=None, stroke=None, stroke_width=1):
        if self.pixmap:
            if fill: fill = self.ui_brush(fill)
            if stroke: stroke = self.ui_pen(stroke, stroke_width)
            self.pixmap.polygon(xy, fill, stroke)
            self.ui_damage()

    def draw_text(self, xy, text, fill="black", font="helvetica"):
        if self.pixmap:
            self.pixmap.text(xy, text, self.ui_font(fill, font))
            self.ui_damage()

    def draw_image(self, xy, image):
        if self.pixmap:
            self.pixmap.paste(self.ui_image(image), xy)

and the WCK library is available from:

    http://effbot.org/zone/wck.htm

</F>








More information about the Python-list mailing list