Reputation: 35
Hey I have a few photos that I played around with using Tkinter and I'm wondering if there is a way I could make my respond to a mouse click? In other words, I'm looking to create a program where I click on a photo on a canvas and have it up open an .xml file on my computer.
code noob here. Appreciate all input to this question.
Upvotes: 0
Views: 135
Reputation: 76264
Tag the image with a unique string, then use tag_bind
to register a handler for the click event.
from Tkinter import *
def image_clicked(event):
print "an image on the canvas was clicked!"
print "now opening xml file..."
#todo: open xml file here
root = Tk()
canvas = Canvas(root, width=500, height=500)
canvas.pack()
canvas.create_rectangle([0,0,100,100], fill="blue", tag="opens_xml")
canvas.tag_bind("opens_xml", "<1>", image_clicked)
root.mainloop()
In the above example, image_clicked
is only called when the blue rectangle is clicked.
Upvotes: 1