Eric Brunel eric_brunel at despammed.com
Mon Sep 13 10:36:17 CEST 2004

Roger Binns wrote:
> Eric Brunel wrote:
>>Compare this with the tk canvas command manual at
>>http://www.tcl.tk/man/tcl8.3/TkCmd/canvas.htm and you'll see we're
>>quite far from it: I didn't see any means to move canvas items
>>individually, or group them with tags, or knowing which items are at
>>a given position, all things a tk canvas can do very easily...
> How does wxArt2D compare?  http://wxart2d.sourceforge.net/
> It hasn't been wrapped for wxPython yet, but seems to cover the same
> sorts of issues you mentioned in the message.
> Roger 

In fact, I almost forgot another very powerful feature of the tk canvas, which 
is the ability to add event bindings not only on the canvas itself, but also on 
each individual canvas item, or on a group of items via tags. Here is a very 
simple example:

from Tkinter import *

root = Tk()

cnv = Canvas(root)

circle1 = cnv.create_oval(10, 10, 20, 20, fill='red')

## Event binding on a single item
def bindingA(event):
   print "I'm circle1!"

cnv.tag_bind(circle1, '<1>', bindingA)

## Event binding on several objects grouped via a tag
cnv.create_oval(30, 10, 40, 20, fill='blue', tags=('MY_CIRCLES',))
cnv.create_oval(40, 40, 50, 50, fill='blue', tags=('MY_CIRCLES',))

def bindingB(event):
   print "We're MY CIRCLES!"

cnv.tag_bind('MY_CIRCLES', '<1>', bindingB)

## New item with same tag => binding is inherited
def addCircle():
   cnv.create_oval(30, 70, 40, 80, fill='blue', tags=('MY_CIRCLES',))

Button(root, text='Add', command=addCircle).pack()


If you can run it, you'll see that each circle responds to mouse clicks by a 
print. Clicking the "Add" button adds a new blue circle, which inherits 
automatically the bindings created on its tag(s).

The part on the drawing itself seems to be covered by wxArt2D, but since I'm not 
fluent in the way wxWidgets/wxPython handles user event, I've some difficulty to 
figure out if these "item bindings" can also be done.
- Eric Brunel <eric (underscore) brunel (at) despammed (dot) com> -
PragmaDev : Real Time Software Development Tools - http://www.pragmadev.com

More information about the Python-list mailing list