Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #36584

Re: How to change colors of multiple widgets after hovering in Tkinter

Path csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'example:': 0.03; 'function,': 0.07; 'mouse': 0.07; 'none)': 0.07; 'tkinter': 0.07; 'subject:How': 0.09; 'events.': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'script,': 0.09; 'def': 0.10; 'bind': 0.16; 'did.': 0.16; 'lambda': 0.16; 'received:80.91.229.3': 0.16; 'received:dip.t-dialin.net': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-dialin.net': 0.16; 'subject:Tkinter': 0.16; 'subject:colors': 0.16; 'wrote:': 0.17; 'specify': 0.17; 'widget': 0.17; 'changes': 0.20; 'equivalent': 0.20; 'written': 0.20; 'trying': 0.21; 'import': 0.21; 'do.': 0.21; 'button,': 0.22; "i'd": 0.22; 'script': 0.24; 'header:User-Agent:1': 0.26; '(which': 0.26; "doesn't": 0.28; 'correct': 0.28; 'header:X -Complaints-To:1': 0.28; "skip:' 10": 0.30; 'becomes': 0.30; 'function': 0.30; 'button': 0.30; 'code': 0.31; 'could': 0.32; 'event.': 0.33; 'skip:h 40': 0.33; 'subject:change': 0.33; 'problem': 0.33; 'to:addr:python-list': 0.33; 'described': 0.35; 'doing': 0.35; 'there': 0.35; 'received:org': 0.36; 'really': 0.36; 'explain': 0.36; 'but': 0.36; 'method': 0.36; 'two': 0.37; 'why': 0.37; 'well.': 0.37; 'subject:: ': 0.38; 'nothing': 0.38; 'instead': 0.39; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'your': 0.60; 'stay': 0.61; 'email addr:gmail.com': 0.63; 'color': 0.69; 'fortunately': 0.84; 'white.': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Peter Otten <__peter__@web.de>
Subject Re: How to change colors of multiple widgets after hovering in Tkinter
Date Thu, 10 Jan 2013 20:13:38 +0100
Organization None
References <f393c060-3017-420a-8ead-e790d36a302d@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset="UTF-8"
Content-Transfer-Encoding 8Bit
X-Gmane-NNTP-Posting-Host p5084bc7b.dip.t-dialin.net
User-Agent KNode/4.7.3
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.378.1357845233.2939.python-list@python.org> (permalink)
Lines 59
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1357845233 news.xs4all.nl 6954 [2001:888:2000:d::a6]:42167
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:36584

Show key headers only | View raw


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 | NextPrevious in thread | Next in thread | Find similar | Unroll thread


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