Turtle Graphics (Python)

vegaseat 0 Tallied Votes 2K Views Share

The Tkinter library folder \lib-tk contains a module called turtle. You can import this module and experiment with turtle graphics. Tell the turtle to move and draw circles, lines, rectangles and more, and watch it, as it performs on a Tkinter canvas. In typical turtle fashion it moves at its own animated speed.

# the turtle module provides turtle graphics primitives, it uses a Tkinter canvas
# turtle is part of the Tkinter library (lib-tk)
# tested with Python24  vegaseat  30jun2005

from turtle import *
import time

# pen/turtle starts at the center (x=0, y=0) of the turtle display area

color("green")
# pen up, don't draw
up()
# centers the circle
goto(0,-50)
# pen down, draw
down()
# radius=50  center is 50 radius units above the turtle
circle(50)
up()
# center the turtle again
goto(0,0)
down()

# draw blue 100x100 squares
color("blue")
for deg in range(0, 61, 6):
    right(90 + deg)
    forward(100)
    right(90)
    forward(100)
    right(90)
    forward(100)
    right(90)
    forward(100)
    
up()
goto(-150,-120)
color("red")
write("Done!")

time.sleep(5)  # wait 5 seconds
vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

Turtle graphics was originally meant for kids from 6 to 86. It was also used in robotics applications.

kvutza 0 Newbie Poster

It looks to me that something like below would result in nicer, equiangularly rotated squares.

for step in range(24):
    right(105)
    ...
CoatsyJnr 0 Newbie Poster

I have used this code and added question here it is

from turtle import *
from time import *
inc=raw_input("What ingrament would you like? [1-360]")
inc=int(inc)
ang=raw_input("What angle would you like? [1-360]")
ang=int(ang)
col_squ=raw_input("What colour would you like the square to be?")
col_cir=raw_input("What colour would you like the circle to be?")
    # pen/turtle starts at the center (x=0, y=0) of the turtle display area
color(col_cir)
    # pen up, don't draw
up()
    # centers the circle
goto(0,-50)
    # pen down, draw
down()
    # radius=50 center is 50 radius units above the turtle
circle(50)
up()
    # center the turtle again
goto(0,0)
down()
    # draw blue 100x100 squares
color(col_squ)
for deg in range(0, 360, inc):
    down()
    right(90 + ang)
    forward(100)
    right(90)
    forward(100)
    right(90)
    forward(100)
    right(90)
    forward(100)
    up()
goto(-150,-120)
color("red")
write("Done!")
sleep(5) # wait 5 seconds
Ene Uran 638 Posting Virtuoso

Another look at the Python module turtle:

# module turtle is part of Tkinter
# turtle starts in the center of canvas (0, 0) by default
# draw 3 turtle lines to form a triangle

import turtle as tu

tu.title('draw an equilateral triangle')
# set at 50% of original size
tu.setup(width=0.5, height=0.5)

# optional ...
# values for speed are 'fastest' (no delay), 'fast', (delay 5ms),
# 'normal' (delay 10ms), 'slow' (delay 15ms), 'slowest' (delay 20ms)
#tu.speed('fastest')

# optional ...
# turtle by default starts at (x=0, y=0) center of a (450x550) window
# to pick another center lift the pen up then move
# to the right x units and up y units or ...
# to the left -x units and down -y units
# now drop the pen down to start drawing
tu.up()
tu.goto(60, 100)
tu.down()

# optional pen color
tu.color('red')

# form a triangle by drawing 3 lines of 150 pixels length
# changing the direction by 120 degrees (360/3) each time
# starts at upper right corner and points down
for k in range(3):
    tu.right(120)
    tu.forward(150)

# keep showing until window corner x is clicked
tu.done()
woooee 814 Nearly a Posting Maven

Just to see what it would take, modifying Ene's code to draw a right angle triangle of unknown dimensions is just a little more tedious

import turtle as tu
import random

tu.title('draw a right angle triangle')
# set at 50% of original size
tu.setup(width=0.5, height=0.5)

tu.up()
tu.goto(60, 100)
tu.down()

# optional pen color
tu.color('red')

hypotenuse=random.randint(100, 200)
base=random.randint(10, 99)
side = (hypotenuse**2 - base**2)**0.5
## round up the "old" way
side += 0.5
side = int(side)

for distance in (base, side):
    tu.forward(distance)
    tu.right(90)

## since we are using whole numbers, the sides will not be exact,
## so go to the starting point
tu.goto(60, 100)

# keep showing until window corner x is clicked
tu.done()
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.