0

I'm trying to make a python script were if you put your mouse over an element in tkinter something would happen.I know i could make a function that repeats via after and make an if statement but i think there might be an easier way. The onmouseover event from HTML is an exemple of what I want. Thanks in advance! My code:

from tkinter import Tk, Canvas, PhotoImage
root=Tk()
c=Canvas(root, width=500, height=500) # width and height are placeholders here
root.title("wdihihfwaheuih") # the title too
imag = PhotoImage(file="example.pgm")
image = c.create_image(250, 250, anchor="c", image=imag) # <-- element i want to "onmouseover"
root.mainloop()
chboo1
  • 63
  • 9

1 Answers1

1

Here is what to do:

#Create element called 'elem'

#Binding hovers
def on_start_hover():
    #What you do when the mouse hovers

def on_end_hover():
    #What to do when the mouse stops hovering

elem.bind('<Enter>', on_start_hover)
elem.bind('<Leave>', on_end_hover)
AksLolCoding
  • 157
  • 10
  • what type is the "elem" is it a PhotoImage? I get an error: Traceback (most recent call last): File "main.py", line 45, in game = Main() File "main.py", line 22, in __init__ self.camicon.bind("") AttributeError: 'int' object has no attribute 'bind' – chboo1 May 25 '20 at 22:48
  • "elem" has to be a Tkinter widget, you can put the Photoimage on a label or button and then bind that. – AksLolCoding May 26 '20 at 17:24