I have some code like this
from Tkinter import *
master = Tk()
def oval_mouse_click(event):
print "in oval"
def canvas_mouse_click(event):
print "in canvas"
w = Canvas(master, width = 800, height = 600)
uid = w.create_oval(390, 290, 410, 310, fill='blue')
w.tag_bind(uid, "<Button-1>", lambda x: oval_mouse_click(x))
w.bind("<Button-1>" , canvas_mouse_click)
w.pack()
mainloop()
When I click on Canvas I have "in canvas" message in console. When I click] on Oval I have two messages "in oval" and "in canvas", but I want to have only first message. Is there any way to stop event raising?
I can do this task with some global flag but I think there should be more natural way for Tkl.
Here is the simplest example to handle your issue:
import Tkinter
def oval_mouse_click(event):
print "in oval"
event.widget.tag_click = True
def canvas_mouse_click(event):
if event.widget.tag_click:
event.widget.tag_click = False
return
print "in canvas"
root = Tkinter.Tk()
canvas = Tkinter.Canvas(width=400, height=300)
oid = canvas.create_oval(400/2-10, 300/2-10, 400/2+10, 300/2+10, fill='blue')
canvas.tag_click = False
canvas.tag_bind(oid, "<Button-1>", oval_mouse_click)
canvas.bind("<Button-1>" , canvas_mouse_click)
canvas.pack()
root.mainloop()
There is no other easier way to handle this under Canvas
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With