How to change colors of multiple widgets after hovering in Tkinter

mountdoom12 at gmail.com mountdoom12 at gmail.com
Thu Jan 10 19:32:28 CET 2013


Hello,

I´m trying to make a script, which will change the background and foreground color of widgets after hovering. 

-------------------------
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 hover on any button, nothing happens, they stay white. I know I could use a function, but there would be two functions for every widget (1 for , 1 for ). I'd like to create a single function, which will recolor that widget I hover on and explain why this script is not doing what I want it to do.

I hope I described my problem well. Thanks for every answer.

PS: I would like to avoid classes.

mountDoom



More information about the Python-list mailing list