[TKinter] get a canvas items's id...

Jeff Epler jepler at unpythonic.net
Thu May 15 15:46:28 EDT 2003


On Thu, May 15, 2003 at 09:25:37PM +0200, polux wrote:
> ...after having clicked on it ! is it possible ?
> 
> I know i've clicked on an item with a certain tag, but the only way I've
> foud to get its id is to use find_closest....

Yes, that's the way to do it.

You could also use a binding on each canvas item, but I don't recommend
this.  Something like:
    def make_callback_with_id(id, real_callback):
        def callback(event):
            real_callback(event, id)
        return callback            
    ...
    def f(event, id):
        print "clicked on", id
    c = Tkinter.Canvas(...)
    item = c.create_XXX(...)
    c.tag_bind(item, "<Button-1>", make_callback_with_id(item, f)
(untested)

Jeff





More information about the Python-list mailing list