- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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 events to Tkinter Canvas items?
Tkinter events can be bound with the widgets to perform a set of operations on the widgets. To be more specific, we can also bind an event handler to Canvas Items by using bind(<Button>, callback) method. Binding the event with the canvas item makes a canvas item dynamic which can be customized by event handlers.
Example
#Import the required Libraries from tkinter import * import random #Create an instance of Tkinter frame win = Tk() #Set the geometry of the window win.geometry("700x350") #Crate a canvas canvas=Canvas(win,width=700,height=350,bg='white') def draw_shapes(e): canvas.delete(ALL) canvas.create_oval(random.randint(5,300),random.randint(1,300),25,25,fill='O rangeRed2') canvas.pack() #Bind the spacebar Key to a function win.bind("<space>", draw_shapes) win.mainloop()
Output
Running the above code will display a window that contains a Canvas.
When we press the <Space> key, it will generate random shapes in the canvas window.
- Related Articles
- How to bind multiple events with one "bind" in Tkinter?
- How to reconfigure Tkinter canvas items?
- How to bind a click event to a Canvas in Tkinter?
- How to hide and show canvas items on Tkinter?
- How to bind jQuery events on elements generated through other events?
- How to bind to shift+tab in Tkinter?
- How to bind events on dynamically created elements using jQuery?
- How to clear Tkinter Canvas?
- How to bind jQuery events within dynamic content returned via Ajax?
- How to bind a key to a button in Tkinter?
- How to bind the Enter key to a tkinter window?
- How to bind all the number keys in Tkinter?
- How to capture events on Tkinter child widgets?
- How to bind the Escape key to close a window in Tkinter?
- How to bind the spacebar key to a certain method in Tkinter?

Advertisements