Skip to content
Tutorials Center
My learning

Topics

Lesson 25 of 40 ยท Canvas and Drawing

Tkinter Canvas: Drawing Shapes, Lines and Text

Learn the Tkinter Canvas coordinate system and draw lines, rectangles, ovals, arcs, polygons and text, with item options and helper math for stars and circles.

The Canvas widget is Tkinter's drawing board. Instead of pixels, it holds a list of items: lines, rectangles, ovals, polygons, text and images. Each item stays alive after you draw it. You can move it, recolor it, delete it, or ask whether the mouse is over it. That makes the canvas the right tool for diagrams, charts, games, simple paint programs and custom widgets.

This lesson covers the coordinate system, every shape-creating method and their options, and the small bit of geometry you need for circles and stars. The canvas needs a display, so Tkinter code is read-only. The runnable blocks compute the coordinates with plain Python, and you can paste the resulting numbers into a real create_* call.

Creating a canvas

Python
import tkinter as tk

root = tk.Tk()
canvas = tk.Canvas(root, width=400, height=300, bg="white", highlightthickness=0)
canvas.pack()

canvas.create_line(20, 20, 380, 20, fill="gray", width=2)
canvas.create_rectangle(50, 60, 150, 140, outline="navy", fill="lightblue", width=3)
canvas.create_oval(200, 60, 300, 140, fill="gold")
canvas.create_text(200, 250, text="Hello, canvas", font=("Helvetica", 18, "bold"), fill="darkgreen")

root.mainloop()

A white 400 by 300 area opens. It shows a thin gray line across the top, a light blue square with a thick navy border at the left, a gold circle at the right and the green bold text "Hello, canvas" centered near the bottom. highlightthickness=0 removes the thin focus border that otherwise offsets your drawing by a couple of pixels.

The coordinate system

The origin (0, 0) is the top-left corner. x grows to the right and y grows downward, the opposite of a maths graph. All coordinates are floats or ints measured in pixels (or in other units if you give strings like "2c" for centimeters).

x goes right, y goes down

Origin (0, 0) is the top-left corner
x Grows to the right
y Grows downward

Flip y yourself if you want a math-style chart.

1 / 2

The shape methods

Every method returns an integer item id. Keep it if you want to change the item later.

Method Coordinates Notes
create_line(x1, y1, x2, y2, ...) two or more points arrow="last", dash=(4, 2), smooth=True, capstyle, joinstyle
create_rectangle(x1, y1, x2, y2) opposite corners outline, fill, width, dash
create_oval(x1, y1, x2, y2) bounding box a circle if the box is square
create_arc(x1, y1, x2, y2) bounding box of the full oval start, extent, style
create_polygon(x1, y1, x2, y2, x3, y3, ...) three or more points smooth=True rounds the corners; fill defaults to black
create_text(x, y, text=...) anchor point anchor, font, width (wrap), justify
create_image(x, y, image=...) anchor point see Canvas: Tags, Moving Items and Images
create_window(x, y, window=widget) anchor point embeds a normal widget

Colors are names ("red", "navy") or hex strings ("#336699"). An empty string "" means "no color": fill="" makes a hollow shape and outline="" removes the border.

Arcs

An arc is a slice of an oval. Angles are measured in degrees counter-clockwise from 3 o'clock. start=0, extent=90 is the upper-right quarter.

Python
canvas.create_arc(50, 50, 250, 250, start=30, extent=120, style="pieslice", fill="tomato")

A pie-slice shape spans from 30 to 150 degrees, pointing upward, with its tip at the oval's center. Other styles: "chord" (a straight line closes the arc) and "arc" (only the curve, no fill).

Text

create_text places text relative to its anchor. The default anchor="center" centers the text on the point. Use anchor="nw" to place the text's top-left corner at the point, "w" for left-center, and so on. width=200 wraps long text at 200 pixels, and justify="center" aligns wrapped lines. Fonts are tuples (family, size, "bold italic") or tkinter.font.Font objects.

Circles and squares from a center point

create_oval wants a bounding box, but humans think in "center and radius". Wrap it in a helper:

Python
def circle(canvas, cx, cy, r, **options):
    return canvas.create_oval(cx - r, cy - r, cx + r, cy + r, **options)

circle(canvas, 200, 150, 40, fill="orange", outline="")

An orange disc with radius 40 appears at the center of the canvas. The next block is not Tkinter; it computes the same boxes, plus the points of a regular polygon and a five-pointed star for create_polygon.

Python Stand-in: coordinates for circles, polygons and stars

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

In a real program you would write canvas.create_polygon(star(100, 100, 50, 20), fill="gold", outline="black"): create_polygon accepts a single flat list of numbers as well as separate arguments. Because y grows downward, -90 degrees is "up" on screen, which is why the first vertex of each shape sits above the center.

A canvas as a chart

Canvas drawing is a great fit for custom bar charts. Scale values to the available height (remembering that y is flipped), then draw one rectangle per value. This stand-in turns data into rectangle coordinates:

Python Stand-in: bar chart rectangles from data

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Each printed line is a call you could paste into the canvas code, with labels added by create_text underneath. Notice that y1 is smaller for bigger values.

Lines, dashes and arrows

Python
canvas.create_line(30, 200, 370, 200, arrow="last", width=2)          # arrow at the end
canvas.create_line(30, 230, 370, 230, dash=(6, 3), fill="gray")        # dashed
canvas.create_line(30, 260, 100, 280, 200, 250, 370, 270, smooth=True) # curve through points

You see an arrow, a dashed gray line and a smooth curve. Lines never close on themselves; use a polygon for a filled closed shape.

Common mistakes

  • Treating y as growing upward. Draw a test rectangle first to check.
  • Giving create_rectangle a width and height instead of the second corner. The third and fourth arguments are coordinates, not sizes.
  • Forgetting that create_polygon fills with black by default; pass fill="" for an outline.
  • Creating an image object in a function and not storing it: the garbage collector deletes it and the canvas shows nothing (details in the next lesson).
  • Drawing in a loop with thousands of items per frame. The canvas keeps every item; delete or move items instead of recreating them.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Where is the point (0, 0) on a Tkinter canvas, and which way does y grow?

    Screen coordinates start at the top-left corner and y increases downward.
  2. 2. What do the arguments of create_rectangle(50, 60, 150, 140) mean?

    Rectangles and ovals both take two corner points of their bounding box.
  3. 3. How do you draw a circle of radius r around (cx, cy)?

    Tkinter has no create_circle; an oval in a square bounding box is a circle.
  4. 4. What does fill="" do?

    An empty color string means no color, so only the outline is drawn.
  5. 5. Which angle does arc start=0 refer to?

    Arc angles are measured counter-clockwise from the positive x direction (3 o'clock).

Summary

  • A Canvas stores drawing items; every create_* returns an item id.
  • Origin top-left, y grows down; rectangles and ovals use two corners.
  • Colors accept names or hex; "" means no color.
  • Use helpers for circles, polygons and stars, and scale data carefully for charts.
  • Anchors position text and images relative to their point.

Next: Canvas: Tags, Moving Items and Images