Drawing with Python's Turtle Graphics Module

import turtle

Canvas Configuration

Turtle graphics operate on a canvas, which defines the drawing area. Its dimensions and initial placement can be customized.

  • screensize() Method

    turtle.screensize(canvwidth=None, canvheight=None, bg=None)
    
    • canvwidth: Width in pixels.
    • canvheight: Height in pixels.
    • bg: Background color as a string or RGB tuple.

    Example:

    import turtle
    turtle.screensize(850, 650, "lightblue")
    print(turtle.screensize())  # Outputs default (400, 300)
    
  • setup() Method

    turtle.setup(width=0.5, height=0.75, startx=None, starty=None)
    
    • width, height: Integer values specify pixels; floats specify screan proportions.
    • startx, starty: Coordinates for the window's top-left corner; defaults center the window.

    Example:

    import turtle
    turtle.setup(1024, 768, 100, 50)
    

Pen Attributes

Properties of the drawing pen include color and stroke width.

  • turtle.pensize(width): Sets line thickness.
  • turtle.pencolor(color): Sets pen color using a string (e.g., "coral") or RGB tuple.
  • turtle.speed(rate): Contorls drawing speed with an integer from 0 (slowest) to 10 (fastest).

Drawing Commands

Commands are categorized into movement, control, and global operations.

Movement Commands

The coordinate origin is at the center, with a standard Cartesian system.

Translation

  • turtle.forward(steps): Moves the pen forward by steps pixels. Alias: turtle.fd(steps).
  • turtle.backward(steps): Moves backward.
  • turtle.goto(x, y): Moves pen to coordinates (x, y).
  • turtle.setx(pos): Moves to a specific x-coordinate.
  • turtle.sety(pos): Moves to a specific y-coordinate.
  • turtle.home(): Returns pen too origin (center), facing east.

Rotation Angles are measured from the positive x-axis (0°).

  • turtle.setheading(angle): Sets absolute orientation.
  • turtle.right(deg): Rotates clockwise by deg degrees.
  • turtle.left(deg): Rotates counterclockwise.

Pen State

  • turtle.pendown(): Lowers pen to draw while moving (default).
  • turtle.penup(): Lifts pen to move without drawing.

Circle Drawing

  • turtle.circle(radius): Draws a circle with specified radius; negative radius draws clockwise.

Control Commands

  • turtle.fillcolor(color): Sets fill color for shapes.
  • turtle.color(pen_color, fill_color): Sets both pen and fill colors.
  • turtle.begin_fill(): Starts shape filling.
  • turtle.end_fill(): Completes filling.
  • turtle.hideturtle(): Conceals the turtle cursor.
  • turtle.showturtle(): Reveals the cursor.

Global Commands

  • turtle.clear(): Erases drawings without resetting pen state.
  • turtle.reset(): Clears canvas and resets pen to initial state.
  • turtle.undo(): Reverses the last action.
  • turtle.isvisible(): Returns visibility status.
  • turtle.stamp(): Places a copy of the turtle shape on the canvas.
  • turtle.write(text, font=("Arial", 12, "normal")): Renders text with optional font parameters.

Tags: python turtle-graphics programming graphics Tutorial

Posted on Tue, 01 Sep 2026 16:09:09 +0000 by drranch