How to bind a click event to a Canvas in Tkinter?

TkinterServer Side ProgrammingProgramming

The Canvas widget is undoubtedly the most powerful widget available in tkinter. It can be used to create and develop from custom widgets to a complete user interface. We can even bind the click event to handle the canvas and its object.


In this example, we will add an image inside the canvas widget and will bind a button object to remove the image from the canvas.

In order to bind a click event, we can use the tag_bind() method and use the delete(image object) to delete the image.

#Import the required library
from tkinter import*
#Create an instance of tkinter frame
win= Tk()
#Set the geometry
#Create an canvas object
canvas= Canvas(win, width= 1000, height= 750)
#Load an image inside the canvas
smiley = PhotoImage(file='smile.gif')
#Create an image in the canvas object
image_item = canvas.create_image((200, 140), image=smiley)
#Bind the Button Event to the Canvas Widget
canvas.tag_bind(image_item, '<Button-1>', lambda e:


Executing the above code will display an image of smiley.

Now, "Left Click" the mouse button over the image, and it will be deleted instantly from the canvas.

Published on 15-Apr-2021 17:34:07