Drawing and animation with Tkinter's Canvas
The Canvas
widget is undoubtedly one of the most powerful widgets available in Tkinter. It can be used to build anything from custom widgets and views to complete user interfaces.
As the name implies, a Canvas
widget is a blank area on which figures and images can be drawn. To understand its basic usage, let's create a small demo script.
Begin the script by creating a root window and a Canvas
object:
# simple_canvas_demo.py
import tkinter as tk
root = tk.Tk()
canvas = tk.Canvas(
root, background='black',
width=1024, height=768
)
canvas.pack()
Creating a Canvas
object is just like creating any other Tkinter widget. In addition to the parent widget and background
argument, we can also specify width
and height
arguments to set the size of the Canvas
. Setting the size of a Canvas
widget is important, because it defines not only the size of the widget but also the viewport; that is, the area in which...