Tkinter canvas item bind/break problem

Greg Ewing greg at cosc.canterbury.ac.nz
Wed Jun 13 22:36:17 EDT 2001


I have a Tkinter canvas item with a binding
for mouse clicks, and a similar binding for the
canvas itself. I want the item binding to take
precedence over the canvas binding.

According to my reading of the docs for the
Tk canvas widget, and my understanding of how
Tkinter works, I should be able to achieve this by
returning "break" from the handler for the
canvas item.

But this doesn't seem to work -- both handlers
get called when I click on the item (the item
one first, then the canvas one.)

Anyone know what might be going wrong? Do
canvas item bindings work differently from regular
bindings in this regard?

Code snippet illustrating the problem is attached
below. Versions I am using are:

Python version: 1.5.2 (#1, Nov 29 1999, 13:42:43)  [GCC 2.8.1]
Tk version: 8.0
Tcl version: 8.0

-------------------------------------------------------------------
import Tkinter, Canvas, sys

def click_canvas(event):
  print "Canvas clicked at", event.x, event.y

def click_item(event):
  print "Item clicked at:", event.x, event.y
  return "break"

print "Python version:", sys.version
print "Tk version:", Tkinter.TkVersion
print "Tcl version:", Tkinter.TclVersion
root = Tkinter.Tk()
canvas = Tkinter.Canvas(root, width = 300, height = 200)
canvas.pack()
item = Canvas.Rectangle(canvas, 50, 50, 100, 100, fill = "green")
canvas.bind("<Button-1>", click_canvas)
item.bind("<Button-1>", click_item)
root.mainloop()
-------------------------------------------------------------------

Thanks for any help,

Greg Ewing, Computer Science Dept, University of Canterbury,	  
Christchurch, New Zealand
greg at cosc.canterbury.ac.nz
http://www.cosc.canterbury.ac.nz/~greg



More information about the Python-list mailing list