Tkinter does not have passing events to parent widgets. But it is possible to simulate the effect through the use of bindtags
Making a binding to a widget is not equal to adding a binding to a widget. It is binding to a bindtag
which has the same name as the widget, but it's not actually the widget.
Widgets have a list of bindtags
, when an event happens on a widget, the bindings for each tag are processed.
import Tkinter as tk
class BuildCanvas(tk.Frame):
def __init__(self, parent):
tk.Frame.__init__(self, parent)
self.main = tk.Canvas(self, width=250, height=250,
borderwidth=0, highlightthickness=0,
background="linen")
self.main.pack(side="top", fill="both", expand=True)
self.main.bind("<1>", self.on_main_click)
for x in range(10):
for y in range(10):
canvas = tk.Canvas(self.main, width=35, height=35,
borderwidth=1, highlightthickness=0,
relief="groove")
if ((x+y)%2 == 0):
canvas.configure(bg="yellow")
self.main.create_window(x*45, y*45, anchor="nw", window=canvas)
bindtags = list(canvas.bindtags())
bindtags.insert(1, self.main)
canvas.bindtags(tuple(bindtags))
canvas.bind("<1>", self.on_sub_click)
def on_sub_click(self, event):
print("sub-canvas binding")
if event.widget.cget("background") == "yellow":
return "break"
def on_main_click(self, event):
print("main widget binding")
if __name__ == "__main__":
root = tk.Tk()
BuildCanvas(root).pack (fill="both", expand=True)
root.mainloop()