I was curious if it is possible to create an clickable element that contains multiple elements?
ttk.Button appears to take text or an image.
I would like to have a clickable element that will have 8 text items and 2 images inside it. Clicking anywhere in that element will trigger the same backend method.
Any code examples would be helpful as still wading through TKinter -- only my second project to use it.
Use bindtags
and give the same tag for all widgets that you want to be clickable, then use bind_class
to bind all the widgets.
Here's an example
import tkinter as tk
def clicked(event):
print("Clicked !")
class ClickableElement(tk.Frame):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.bindtags('Click') # or pass a list if you want to have multiple tag names
for x in range(8):
lbl = tk.Label(self, text=f"Text {x}")
lbl.bindtags('Click')
lbl.pack()
root = tk.Tk()
root.geometry("200x200")
frame = ClickableElement(root) # or tk.Frame(root, class_='Click')
frame.pack(fill='both', expand=True)
frame2 = tk.Frame(root, bg='red')
frame2.pack(expand=True, fill='both')
root.bind_class('Click', "<Button-1>", clicked)
root.mainloop()
The above example will make both text and Frame clickable. You can expand on this to include images. Alternatively, You can use bind
on each widget inside the frame.