A tkinter canvas can be used to draw in a window. Use this widget to draw graphs or plots. You can even use it to create graphical editors.

You can draw several widgets in the canvas: arc bitmap, images, lines, rectangles, text, pieslices, ovals, polygons, ovals, polygons, and rectangles. Rectangles can be both outline and interior.

The canvas has two coordinate systems: the window system (left top corner x=0,y=0) and the canvas coordinate system that defines where items are drawn.

Related course: Python Desktop Apps with Tkinter

Example

introduction

The canvas is a general purpose widget: you can use it to make any kind of graphics including plots, drawings, charts, show images and much more.

A canvas is added with one line of code:

1
myCanvas = tkinter.Canvas(root, bg="white", height=300, width=300)

This defines where to add it, the background and size. After creation, you can draw on top of it.

If you want to draw an arc simply call its method create_arc():

1
arc = myCanvas.create_arc(coord, start=0, extent=150, fill="red")

tkinter canvas

tkinter canvas

Adding a canvas to a tk window and drawing in it is very easy.
This example opens a window, adds a canvas and draws two arcs in it. This program will draw two arcs, a green one and red one that together make up a circle.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import tkinter

# init tk
root = tkinter.Tk()

# create canvas
myCanvas = tkinter.Canvas(root, bg="white", height=300, width=300)

# draw arcs
coord = 10, 10, 300, 300
arc = myCanvas.create_arc(coord, start=0, extent=150, fill="red")
arv2 = myCanvas.create_arc(coord, start=150, extent=215, fill="green")

# add to window and show
myCanvas.pack()
root.mainloop()

Download Tkinter examples