Making a list of mouse over event functions in Tkinter Making a list of mouse over event functions in Tkinter tkinter tkinter

Making a list of mouse over event functions in Tkinter


I can't write a function definition for every single possible label, they would number in the hundreds or thousands. I'm sure there's a very pythonic way to do it, but I have no idea what.

Check out lambda functions which are nearly identical to what you want.

In your case, something like:

def update_bottom_scroll_bar(text):    # whatever you want to do to update the text at the bottomfor treatment in treament_list:  # For each treatment in the list    label = ttk.Label(frames[treatment[1] - 1], text=treatment[0])  # Build the label for treatment    label.bind("<Enter>", lambda event, t=treatment: update_bottom_scroll_bar(text=t))    label.bind("<Leave>", lambda event: update_bottom_scroll_bar(text='Default label text'))    labels.append(label)  # Add the treatment to the list    label.pack()

Also please spell your variables right, I corrected treament to treatment...