CustomTkinter icon indicating copy to clipboard operation
CustomTkinter copied to clipboard

`.bind("<Enter>")` does not work on image in CTkButton

Open infinitel8p opened this issue 2 years ago • 3 comments

When experimenting with tktooltip (https://github.com/gnikit/tkinter-tooltip) I noticed apart from #924, that the tooltip doesnt show when the mouse is on the image from my button. It seems that the .bind("<Enter>") doesnt count/work for the image in the button

Am I overlooking something? It would be nice if this would work on the image as well since im planning to only have a image in the button when 924 is fixed.

Code to reproduce the issue:

pip install tkinter-tooltip

from customtkinter import CTk, CTkButton, CTkImage
from tktooltip import ToolTip
from PIL import Image

def callback(event):
    # Callback function code goes here
    print("Mouse entered the button")

root = CTk()

test_image = CTkImage(light_image=Image.open(
    f"image.png"), size=(50, 50))
button = CTkButton(root, text="Hover over me", image=test_image)

ToolTip(button, "This is a tooltip")

button.bind("<Enter>", callback)
button.pack()

root.mainloop()

infinitel8p avatar Dec 30 '22 08:12 infinitel8p

@infinitel8p This is happening because the image label within the button is a separate widget. You have to add those bindings/tooltips for button._image_label:

from customtkinter import CTk, CTkButton, CTkImage
from tktooltip import ToolTip
from PIL import Image

def callback(event):
    # Callback function code goes here
    print("Mouse entered the button")

root = CTk()

test_image = CTkImage(light_image=Image.open(
    f"image.png"), size=(50, 50))
button = CTkButton(root, text="Hover over me", image=test_image)

ToolTip(button, "This is a tooltip", delay=0.01)
ToolTip(button._image_label, "This is a tooltip", delay=0.01)

button.bind("<Enter>", callback)
button._image_label.bind("<Enter>", callback)
button.pack()

root.mainloop()

Akascape avatar Dec 30 '22 13:12 Akascape

@infinitel8p did you use the latest version? For me there is no problem, the tooltip is showing on the text and the image.

TomSchimansky avatar Jan 10 '23 13:01 TomSchimansky

@TomSchimansky

@infinitel8p This is happening because the image label within the button is a separate widget. You have to add those bindings/tooltips for button._image_label:

from customtkinter import CTk, CTkButton, CTkImage
from tktooltip import ToolTip
from PIL import Image

def callback(event):
    # Callback function code goes here
    print("Mouse entered the button")

root = CTk()

test_image = CTkImage(light_image=Image.open(
    f"h.png"), size=(50, 50))
button = CTkButton(root, text="Hover over me", image=test_image)

ToolTip(button, "This is a tooltip", delay=0.01)
ToolTip(button._image_label, "This is a tooltip", delay=0.01)

button.bind("<Enter>", callback)
button._image_label.bind("<Enter>", callback)
button.pack()

root.mainloop()

when doing it as @Akascape suggested here it works fine, just binding the button itself does not work though. I have to manually bind the image label of the button to have the tooltip pop up there as well. It would be nice if this would happen automatically when binding something on the button as in my eyes the image is part of the button as well.

Im using customtkinter 5.0.3 right now

infinitel8p avatar Jan 10 '23 15:01 infinitel8p

seems to be fixed in 5.0.5

infinitel8p avatar Jan 31 '23 12:01 infinitel8p