Код: Выделить всё
import tkinter as tk
root = tk.Tk()
frame_top = tk.Frame(root, bg="lightyellow")
frame_top.pack(fill="both", expand=True)
label_top_1 = tk.Label(frame_top, text="Label", fg="white", bg="darkred")
label_top_1.pack(padx=50, pady=20)
label_top_2 = tk.Label(frame_top, text="Label", fg="white", bg="darkred")
label_top_2.pack(padx=50, pady=20)
frame_bottom = tk.Frame(root, bg="lightblue")
frame_bottom.pack(fill="both", expand=True)
label_bottom_1 = tk.Label(frame_bottom, text="Label", fg="white", bg="darkred")
label_bottom_1.pack(padx=50, pady=20)
label_bottom_2 = tk.Label(frame_bottom, text="Label", fg="white", bg="darkred")
label_bottom_2.pack(padx=50, pady=20)
root.mainloop()

Добавление обработки событий щелчка мыши:
Код: Выделить всё
def frame_top_onclick(event):
print("top frame clicked")
def frame_bottom_onclick(event):
print("bottom frame clicked")
frame_top.bind("", frame_top_onclick, add="+")
frame_bottom.bind("", frame_bottom_onclick, add="+")
Конечно, я мог бы добавить обработку событий для каждого дочернего элемента:
Код: Выделить всё
label_top_1.bind("", frame_top_onclick, add="+")
label_top_2.bind("", frame_top_onclick, add="+")
label_bottom_1.bind("", frame_bottom_onclick, add="+")
label_bottom_2.bind("", frame_bottom_onclick, add="+")
Существует ли встроенный способ распространения обработки событий от родительского виджета к его дочерним элементам?>
Мобильная версия