Turtle is a basic shape drawing package.
If you have learn't how to use Logo lots of the commands will be familiar.
Setting up a Turtle screen
import turtle
t=turtle.Turtle()
wn=turtle.Screen()
t.fd(300)
wn.exitonclick()
#To use turtle graphics you must import this library at the top of #your program
#Create a turtle in a variable called t (Note the CAPITAL)
#Create a turtle screen in a variable called wn
#Move turtle t forward 300
#Allow turtle window to close when clicked on (goes at end)
This would output as
Basic Commands
import turtle
g=turtle.Turtle()
m=turtle.Turtle()
wn=turtle.Screen()
g.fd(50)
g.rt(90)
g.bk(50)
g.lt(90)
g.fd(50)
g.home()
m.pensize(10)
m.pencolor("red")
m.pu()
m.bk(200)
m.pd()
m.fd(150)
wn.exitonclick()
#fd for forward
#rt for right in degrees
#bk for back
#lt for left in degrees
#home goes back to start pos
#sets pensize
#sets line colour (NB American spelling
#pu to lift pen up and not draw
#pd to put pendown and draw
This would output as
NOTE How the home() command still draws a line home
Turtle Circle
import turtle
g=turtle.Turtle()
wn=turtle.Screen()
g.circle(100)
g.bk(150)
g.circle(40,180)
wn.exitonclick()
#import turtle commands
#setup variable g as turtle
#setup window for turtles
#draw a circle with radius of 100
#back 150
#draw a circle of radius 40 which only goes through #180 degrees (half circle)
#allow user to close the turtle window
This would output as
Using Turtle to demonstrate other Python Commands
Basic Functions
import turtle
g=turtle.Turtle()
wn=turtle.Screen()
def square():
g.fd(100)
g.rt(90)
g.fd(100)
g.rt(90)
g.fd(100)
g.rt(90)
g.fd(100)
g.rt(90)
square()
wn.exitonclick()
#Define a square function
#Running square function
import turtle
g=turtle.Turtle()
wn=turtle.Screen()
for i in range(4):
g.fd(200)
g.lt(90)
wn.exitonclick()
#Repeat what is underneath 4 times
import turtle
g=turtle.Turtle()
wn=turtle.Screen()
diff = 90
for i in range(4):
g.fd(diff)
g.lt(diff)
wn.exitonclick()
#Create variable diff put 90 inside it
#Repeat everything underneath 4 times
#Forward whatever is in diff
#Left whatever is in diff
This would output as a square
Now the same program changing diff by one every time
import turtle
g=turtle.Turtle()
wn=turtle.Screen()
diff = 90
for i in range(20):
g.fd(diff)
g.lt(diff)
diff = diff +1
wn.exitonclick()
This would output as
To find out more about variables
Other Useful Turtle Commands
t=turtle.Turtle() or using RawTurtle at bottom
t.clear()
t.width(1)
t.shape("arrow")
t.speed(10)
t.setpos(-100,-100)
t.home()
t.stamp()
t.ht()
t.st()
t.pencolor("green")
t.fillcolor("Blue")
t.undo(6)
#Clears the screen of all lines drawn
#Sets line width
#turtle shape, “circle”, “square”, “triangle”, “classic”
#0=instant 1=slowest 10=fast 6=normal
#uses cartesian coordinates to move turtle
#turtle returns to start position
#Stamps a copy of turtle
#Hide turtle
#Show turtle
#set pen colour to green (can use rgb values)
#shapes filled will be blue inside #NOTE US Spelling of colour
#undo last 6 turtle commands
NOTE this doesn't reset the turtle(s) back to the start position
Turtle on a better screen canvas
I have found that the screen which comes with turtle can freeze in Windows
This code although more complex leads to a better screen
You have to define the turtle using RawTurtle instead of turtle.Turtle
from turtle import TurtleScreen, RawTurtle, TK
root = TK.Tk()
cv1 = TK.Canvas(root, width=500, height=500, bg="#ddffff")
cv1.pack()
s1 = TurtleScreen(cv1)
s1.bgcolor("orange")
p = RawTurtle(s1)
t = RawTurtle(s1)
#Insert turtle moves here
TK.mainloop()
#Set width and height of #screen
#Could use(0.85, 0.85, 1)
#Colour of background
#Define turtle in variable p
#Define turtle in variable t
#This must go on the end
Programming a button below your Turtle Screen
from turtle import TurtleScreen, RawTurtle, TK
root = TK.Tk()
cv1 = TK.Canvas(root, width=500, height=500, bg="#ddffff")
cv1.pack()
s1 = TurtleScreen(cv1)
s1.bgcolor("orange")
p = RawTurtle(s1)
#define what your button is going to do inside a function
def rt90():
p.rt(90)
#code to create the button, text sets what text will be on button,
#command refers back to function you created above
right90=TK.Button(text="Right 90",command=rt90)
#This line places the button on the canvas
#side determines where (left right centre)
right90.pack(side="left")
TK.mainloop()
This example shows two buttons after the square button has been pressed
The drawing canvas has been reduced to 200x200