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: 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: 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 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("",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("", 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("", lambda event: Hover1.configure(bg="red")) Hover1.bind("", 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()