chboo1
chboo1

Reputation: 73

"Mouse over" event in tkinter

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()

Upvotes: 0

Views: 3069

Answers (1)

AksLolCoding
AksLolCoding

Reputation: 157

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)

Upvotes: 1

Related Questions