Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #36584
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Subject | Re: How to change colors of multiple widgets after hovering in Tkinter |
| Date | 2013-01-10 20:13 +0100 |
| Organization | None |
| References | <f393c060-3017-420a-8ead-e790d36a302d@googlegroups.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.378.1357845233.2939.python-list@python.org> (permalink) |
mountdoom12@gmail.com wrote:
> I´m trying to make a script, which will change the background and
> foreground color of widgets after hovering.
> 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.
You did.
> 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"))
This calls Hover1.configure(bg="red") once and binds the result of that
method call (which is None) to the event. So the above line is equivalent to
Hover1.configure(bg="red")
Hover1.bind("<Enter>", None)
You say you don't want to write a function, but that is really the correct
aproach. Fortunately there is a way to create such a function on the fly:
def f(event):
Hover1.configure(bg="red")
can be written as
f = lambda event: Hover1.configure(bg="red")
With that your code becomes
Hover1.bind("<Enter>", lambda event: Hover1.configure(bg="red"))
Hover1.bind("<Leave>", lambda event: Hover1.configure(bg="white"))
and so on. In this specific case this doesn't have the desired effect
because when the mouse enters a Button widget its background color changes
to 'activebackground'. So you don't really need to bind the enter/leave
events. Specify an activebackground instead when you create the buttons. For
example:
Hover1 = Button(root, text="Red color", bg="white", activebackground="red")
Hover1.pack()
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
How to change colors of multiple widgets after hovering in Tkinter mountdoom12@gmail.com - 2013-01-10 10:32 -0800
Re: How to change colors of multiple widgets after hovering in Tkinter Peter Otten <__peter__@web.de> - 2013-01-10 20:13 +0100
Re: How to change colors of multiple widgets after hovering in Tkinter Rick Johnson <rantingrickjohnson@gmail.com> - 2013-01-10 21:43 -0800
Re: How to change colors of multiple widgets after hovering in Tkinter Rick Johnson <rantingrickjohnson@gmail.com> - 2013-01-10 21:43 -0800
csiph-web