How to change colors of multiple widgets after hovering in Tkinter

Discussion in 'Python' started by mountdoom12@gmail.com, Jan 10, 2013.

  1. Guest

    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 itto do.

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

    PS: I would like to avoid classes.

    mountDoom
     
    , Jan 10, 2013
    #1
    1. Advertising

  2. Peter Otten Guest

    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()
     
    Peter Otten, Jan 10, 2013
    #2
    1. Advertising

  3. Rick Johnson Guest

    On Thursday, January 10, 2013 1:13:38 PM UTC-6, Peter Otten wrote:
    > mountdoom wrote:
    > > I´m trying to make a script, which will change the background and
    > > foreground color of widgets after hovering.


    Peter's advice is spot on except you may want ALL widgets to change colors on <ENTER> and <LEAVE> events. If you want all widgets use the "w.bind_all"method instead of "w.bind". Also check out the "w.bind_class" method to confine bindings to one particular class of widget (like a Tkinter.Button).
     
    Rick Johnson, Jan 11, 2013
    #3
  4. Rick Johnson Guest

    On Thursday, January 10, 2013 1:13:38 PM UTC-6, Peter Otten wrote:
    > mountdoom wrote:
    > > I´m trying to make a script, which will change the background and
    > > foreground color of widgets after hovering.


    Peter's advice is spot on except you may want ALL widgets to change colors on <ENTER> and <LEAVE> events. If you want all widgets use the "w.bind_all"method instead of "w.bind". Also check out the "w.bind_class" method to confine bindings to one particular class of widget (like a Tkinter.Button).
     
    Rick Johnson, Jan 11, 2013
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. spohle
    Replies:
    1
    Views:
    336
    Simon Forman
    Jun 24, 2006
  2. AAaron123

    TreeNode colors come from anchor colors

    AAaron123, Aug 7, 2008, in forum: ASP .Net
    Replies:
    1
    Views:
    564
    darrel
    Aug 7, 2008
  3. ZelluX
    Replies:
    3
    Views:
    1,965
    Peter Otten
    Dec 1, 2008
  4. jef
    Replies:
    1
    Views:
    128
    Eliyahu Goldin
    Apr 12, 2005
  5. max
    Replies:
    1
    Views:
    157
Loading...

Share This Page