What you can do is get the children of the tk.Frame
and bind them to a function.
from tkinter import *
from tkinter import messagebox
class ClickFrame:
def __init__(self,root):
self.root = root
self.root.geometry("700x500")
Label(self.root,text="Clickable Frame!",font=("arial",15)).pack(fill=X,side=TOP)
self.Frame1 = Frame(self.root,bg="light grey")
self.Frame1.pack(fill=BOTH,expand=1,padx=100,pady=100)
for i in range(8):
Label(self.Frame1,text=f"This is Label {i}").pack(pady=5,anchor="w",padx=5)
self.Frame1.bind("<Button-1>",self.detect_click)
for wid in self.Frame1.winfo_children():
wid.bind("<Button-1>",self.detect_click)
def detect_click(self,event,*args):
messagebox.showerror("Clicked",f"Clicked the widget {event.widget}")
print(event.widget,type(event.widget))
root=Tk()
ob=ClickFrame(root)
root.mainloop()
You also can use bind_all()
to bind . However, this will bind everything in the window.
from tkinter import *
from tkinter import messagebox
class ClickFrame:
def __init__(self,root):
self.root = root
self.root.geometry("700x500")
Label(self.root,text="Clickable Frame!",font=("arial",15)).pack(fill=X,side=TOP)
self.Frame1 = Frame(self.root,bg="light grey")
self.Frame1.pack(fill=BOTH,expand=1,padx=100,pady=100)
for i in range(8):
Label(self.Frame1,text=f"This is Labe {i}").pack(pady=5,anchor="w",padx=5)
self.Frame1.bind_all("<Button-1>",self.detect_click)
def detect_click(self,event,*args):
messagebox.showerror("Clicked",f"Clicked the widget {event.widget}")
root=Tk()
ob=ClickFrame(root)
root.mainloop()