- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to bind a click event to a Canvas in Tkinter?
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 win.geometry("750x280") #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: canvas.delete(image_item)) canvas.pack() win.mainloop()
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.
- How to draw a dot on a canvas on a click event in Tkinter Python?
- How to bind events to Tkinter Canvas items?
- How to handle a Button click event in tkinter?
- How to use multiple click event on HTML5 canvas?
- How to bind a Tkinter event to the left mouse button being held down?
- How to bind a key to a button in Tkinter?
- How to simulate a click event using jQuery?
- How to bind the Enter key to a tkinter window?
- How to submit a form using jQuery click event?
- How to handle a link click event using jQuery?
- How to handle a double click event using jQuery?
- How to bind to shift+tab in Tkinter?
- How to create a Button on a Tkinter Canvas?
- How to draw a line on a Tkinter canvas?