Drawing and animation with Tkinter's Canvas

The Canvas widget is undoubtedly the most powerful widget available in Tkinter. It can be used to build anything from custom widgets and views to complete user interfaces. As the name implies, Canvas is a blank area on which figures and images can be drawn.

A Canvas object can be created like any other widget:

root = tk.Tk()
canvas = tk.Canvas(root, width=1024, height=768)
canvas.pack()

Canvas accepts the usual widget configuration arguments, as well as width and height for setting its size. Once created, we can start adding items to canvas using its many create_() methods.

For example, we can add a rectangle with this code:

canvas.create_rectangle(100, 100, 200, 200, fill='orange')

The first four ...

Get Python GUI Programming with Tkinter now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.