I am trying to show the image for a button when hovering over it and hide it when leaving the button with my mouse:
import customtkinter as ctk
from PIL import Image
window = ctk.CTk()
window.geometry("200x200")
icon = ctk.CTkImage(
light_image=Image.open("trash.png"),
dark_image=Image.open("trash.png")
)
def show_icon(event):
button.configure(image=icon)
button._draw()
def hide_icon(event):
button.configure(image=None)
button._draw()
button = ctk.CTkButton(
window,
text="",
image=None,
fg_color="white",
hover_color="white",
width=36,
)
button.pack()
button.bind("<Enter>", show_icon)
button.bind("<Leave>", hide_icon)
window.mainloop()
It works, but the icon is constantly flickering when I have my mouse over the button even when I dont move the mouse.
Does someone know why this is happening?
Edit: It only flickers when I am hovering over the image in the button itsself. When my mouse is anywhere else over the button everything is okay.
When you hide the icon you do this
button.configure(image=None)
which reveals the underlying button and produces an <Enter>
event.
Produce a same-size blank icon and do this when hiding
button.configure(image="blank.png")
which will prevent the generation of spurious events.