Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Python Turtle

Turtle

Line

  • getturtle
  • forward
  • exitonclick

Without the call to turtle.exitonclick() at the end of the program it would automatically close the window and we would not be able to see anything. This command installs an event-handler so when we click on the mouse it will end the application.

import turtle

trt = turtle.getturtle()
trt.forward(100)

turtle.exitonclick()

Square (forward, left)

  • forward
  • left
import turtle

trt = turtle.getturtle()
trt.forward(100)
trt.left(90)
trt.forward(100)
trt.left(90)
trt.forward(100)
trt.left(90)
trt.forward(100)

turtle.exitonclick()

Circle

  • circle
import turtle

trt = turtle.getturtle()
trt.circle(100)

turtle.exitonclick()

Square using the circle command

  • steps
import turtle

trt = turtle.getturtle()
trt.circle(100, steps=4)

turtle.exitonclick()
import turtle

trt = turtle.getturtle()
trt.right(45)
trt.circle(100, steps=4)

turtle.exitonclick()

Triangle

import turtle

trt = turtle.getturtle()
trt.circle(100, steps=3)

turtle.exitonclick()

Move turtle without drawing (penup, pendown, goto)

  • penup
  • pendown
  • goto

Coordinates (0, 0) are in the middle of the screen.

import turtle

trt = turtle.getturtle()
trt.circle(30)
trt.penup()
trt.goto(200, 0)
trt.pendown()
trt.circle(50)

turtle.exitonclick()

Turtle house

  • right
  • forward
  • goto
import turtle

def house():
    trt = turtle.getturtle()

    for i in range(4):
        trt.forward(50)
        trt.right(90)
    trt.goto(25, 25)
    trt.goto(50, 0)

house()

turtle.exitonclick()

Turtle screen

  • getscreen
  • bgcolor
import turtle

scr = turtle.getscreen()

scr.bgcolor('blue')

turtle.exitonclick()

Tutle onclick - mouse click event

import turtle
import time

scr = turtle.getscreen()
t = turtle.getturtle()

#s.onclick(lambda x, y: t.left(10) )
scr.onclick(lambda x, y: print(x, y), btn=1 )
scr.onclick(lambda x, y: print('middle'), btn=2 )
scr.onclick(lambda x, y: print('right'), btn=3 )

scr.mainloop()

#while True:
#    t.forward(1)
#    #time.sleep(0.1)

Turle Commands

  • docs

  • forward(distance), fd

  • backward(distance), bk

  • left(angle), lt

  • right(angle), rt

  • goto(x, y)

  • home() goto(0,0) - the center

  • pos() - get current position

  • penup()

  • pendown()

  • clear()

  • circle(radius), circle(radius, extent, steps)

  • dot() ???

Turtle Colors

  • Named Colors See the names
  • Hex Code - use a color picker
  • Color Tuple r,g,b each one goes from 0-1
  • color
  • pencolor
  • fillcolor
  • s.bgcolor
  • begin_fill + end_fill
import turtle

t = turtle.getturtle()

t.forward(50)

t.color("blue")
t.forward(50)

t.color("green", "yellow")
t.forward(50)

t.pencolor("purple")
t.fillcolor("orange")
t.forward(50)


turtle.exitonclick()
import turtle

t = turtle.getturtle()

t.fillcolor("orange")

t.begin_fill()
t.circle(30)

t.end_fill()

turtle.exitonclick()

Turtle shape

  • shapesize
  • shape('square')
import turtle

trt = turtle.getturtle()
trt.shapesize(3, 5)  # height, width , default is 1,1
trt.forward(50)


trt.shape('classic')
trt.shape('arrow')
trt.shape('circle')
trt.shape('triangle')
trt.shape('square')
trt.shape('turtle')

turtle.exitonclick()

Turtle speed

  • speed (1-10)

  • pen(pencolor=, fillcolor=, pensize=, speed=)

Undo the turtle

  • undo()

  • undobufferentries()

  • setundobuffer(17)

  • clear()

  • reset()

  • showturtle()

  • hideturtle()

  • stamp()

  • clearstamp(N)

Create new turtle

x = turtle.Turtle()

Clone a turtle

y = t.clone()

import turtle

# based on the example in the documentation

t = turtle.getturtle()
t.color('red', 'yellow')
t.begin_fill()
while True:
    t.forward(100)
    t.left(190)
    if abs(t.pos()) < 1:
        break
t.end_fill()

turtle.exitonclick()
import turtle

t = turtle.getturtle()

while True:
    res = turtle.numinput("Angles?", "How many angles?", default=3, minval=3, maxval = 180)
    if res is None:
        break
    t.clear()
    print(res)
    t.circle(100, steps=int(res))

import turtle

t = turtle.getturtle()

res = turtle.numinput("Angles?", "How many angles?", default=3, minval=3, maxval = 180)
print(res)
t.circle(100, steps=int(res))

turtle.exitonclick()
import turtle

t = turtle.getturtle()

res = turtle.textinput("Shape?", "Which shape shall I draw? rectangle or triangle ?")
print(res)
if res == 'triangle':
    t.circle(100, steps=3)
elif res == 'rectangle':
    t.circle(100, steps=4)
else:
    pass
    

turtle.exitonclick()
import turtle

def screen_details():
    scr = turtle.getscreen()
    print(dir(scr))
    print(scr.screensize())
    print(scr.window_width())
    print(scr.window_height())

def draw_top_box():
    trt = turtle.getturtle()
    scr = turtle.getscreen()
    speed = trt.speed()
    print(speed)
    trt.speed(0)
    trt.penup()
    trt.setpos(20-scr.window_width()/2, scr.window_height()/2-20)
    trt.pendown()
    trt.setheading(0)
    trt.forward(scr.window_width()-40)
    trt.setheading(270)
    trt.forward(200)
    trt.setheading(180)
    trt.forward(scr.window_width()-40)
    trt.write("GitHub", font=("Arial", 30, 'normal'))
    trt.setheading(90)
    trt.forward(200)

    trt.speed(speed)

#turtle.bye()
def click(x, y):
    print('click at', x, y)
def button():
    print("button")

def main():
    trt = turtle.getturtle()
    scr = turtle.getscreen()
    scr.onclick(click)
    #scr.onkey(button,  'a')
    #scr.listen() # is needed to make the onkey settings work
    screen_details()
    draw_top_box()
    turtle.mainloop()

    #turtle.exitonclick()

main()