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()
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()
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 button image Nick_tkinter 4 295 Mar-04-2021, 11:33 PM
Last Post: deanhystad
  tkinter python button position problem Nick_tkinter 3 513 Jan-31-2021, 05:15 AM
Last Post: deanhystad
  TKinter restarting the mainloop when button pressed zazas321 7 654 Jan-26-2021, 06:38 AM
Last Post: zazas321
  tkinter touchscreen scrolling - button press makes unwanted scrolling nanok66 1 334 Dec-28-2020, 10:00 PM
Last Post: nanok66
Question [Tkinter] Can I set background color for each item in tkinter Combobox? water 1 825 Dec-10-2020, 07:48 PM
Last Post: Larz60+
  Tkinter - How can I remove the background borders from ttk.Button? TurboC 4 2,435 Oct-18-2020, 10:58 AM
Last Post: TurboC
  Tkinter menu font size -method to change tonycat 2 1,203 Oct-11-2020, 02:43 AM
Last Post: tonycat
  tkinter | Button color text on Click Maryan 2 534 Oct-09-2020, 08:56 PM
Last Post: Maryan
  Tkinter - How can I change the default Notebook border color? TurboC 4 2,209 Oct-06-2020, 10:35 PM
Last Post: TurboC
  [Tkinter] Trying to change font size w/o changing button size python63 3 3,464 Aug-05-2020, 01:04 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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