How to stop an event in Tkinter?

I have 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 a "in canvas" message in the console. When I click] on Oval, I have two messages “in an oval” and “in a canvas”, but I want to have only the first message. Is there a way to stop the collection of events?

I can accomplish this task with some global flag, but I think there should be a more natural way for Tkl.

+5
source share
2 answers

Here is the simplest example to solve your problem:

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 at Canvas.

+2
source

Python tkinter: .

, : Canvas, , tag_bind. , , return "break" Tk + . :

  • callback,
  • , callback .
    • "break", : bind Canvas , tag_bind, . , , unbind.
    • "break": , Canvas

Text, Canvas.

0

All Articles