How to change colors of several widgets after freezing in Tkinter?

I am trying to make a script that will change the background and foreground color of a widget after freezing.

from Tkinter import * root=Tk() Hover1=Button(root,text="Red color", bg="white") Hover1.pack() Hover2=Button(root,text="Yellow color", bg="white") Hover2.pack() Hover1.bind("<Enter>",Hover1.configure(bg="red")) Hover1.bind("<Leave>",Hover1.configure(bg="white")) Hover2.bind("<Enter>",Hover2.configure(bg="yellow")) Hover2.bind("<Leave>",Hover2.configure(bg="white")) root.mainloop() 

but when I press any button nothing happens, they remain white. I know that I can use a function, but for each widget there will be two functions (1 for, 1 for). I would like to create one function that will repaint this widget that I am hanging on and explain why this script does not do what I want it to do.

I hope I have described my problem well. Thanks for every reply.

PS: I would like to avoid classes.

mountDoom

+2
source share
1 answer

You need to provide the called function to bind to the event. Instead, you call the function and pass its result. Fix it like this:

 Hover1.bind("<Enter>", lambda event, h=Hover1: h.configure(bg="red")) 
+2
source

Source: https://habr.com/ru/post/1242326/


All Articles