Python Forum
[tkinter] color change for hovering over button
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[tkinter] color change for hovering over button
#1
The following code will not work as intended. It only turns yellow when leaving the button but not red when entering. What am I missing?

import tkinter as tk

def onLeave(event):
    b.config(bg='yellow')

def onEnter(event):
    b.config(bg='red')
    

root = tk.Tk()
root.minsize(100,100)

b=tk.Button(root)
b.pack() 
b.bind('<Leave>', onLeave)
b.bind('<Enter>', onEnter)

root.mainloop()
Reply
#2
This works on enter
import tkinter as tk

root = tk.Tk()
btn = tk.Button(text='Button', activebackground='red').pack()
root.mainloop()
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#3
Thanks for the reply. It works but I cannot explain to my students why my original code doesn't work. With activebackground you don't need the <Enter> bind at all.
I'm contemplating using tkinter to teach gui's but issues like this don't give me confidence in the toolkit. Is this typical for tkinter?

BTW, I searched for the reason my code does not work. I suspect it's not redrawing the widget after the color change. Someone said one should call .update() on the widget but that did not help. Even .update() and .update_idletasks() on the root window did not work.
Reply
#4
Using bind events
#! /usr/bin/env python3

import tkinter as tk

def on_leave(event):
    btn['bg'] = 'yellow'

def on_enter(event):
    btn['activebackground'] = 'blue'

root = tk.Tk()
root.geometry('200x200')
btn = tk.Button(text='Button')
btn['font'] = 'sans 18 normal'
btn.pack()
btn.bind('<Leave>', on_leave)
btn.bind('<Enter>', on_enter)
root.mainloop()
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#5
Thank you menator01. That's more like what I was looking for.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] TKinter Remove Button Frame Nu2Python 8 751 Jan-16-2024, 06:44 PM
Last Post: rob101
  tkinter - touchscreen, push the button like click the mouse John64 5 717 Jan-06-2024, 03:45 PM
Last Post: deanhystad
  Centering and adding a push button to a grid window, TKinter Edward_ 15 4,150 May-25-2023, 07:37 PM
Last Post: deanhystad
  Can't change the colour of Tk button text Pilover 6 14,390 Nov-15-2022, 10:11 PM
Last Post: woooee
  [PyQt] [Solved]Change text color of one line in TextBrowser Extra 2 4,666 Aug-23-2022, 09:11 PM
Last Post: Extra
  [Tkinter] Tkinter don't change the image DQT 2 1,528 Jul-22-2022, 10:26 AM
Last Post: menator01
  [WxPython] [SOLVED] How to change button label? Winfried 3 1,977 May-31-2022, 06:37 PM
Last Post: Winfried
  Tkinter - How can I change the default Notebook border color? TurboC 5 14,562 May-23-2022, 03:44 PM
Last Post: bigmac
Question [Tkinter] Change Treeview column color? water 3 9,284 Mar-04-2022, 11:20 AM
Last Post: Larz60+
  Can't get tkinter button to change color based on changes in data dford 4 3,314 Feb-13-2022, 01:57 PM
Last Post: dford

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020