Unlock Python Turtle: Master Basic Drawing Commands in Minutes
This tutorial introduces Python's turtle module, showing how to set up the canvas, control the drawing pen, and use a wide range of basic commands—including movement, drawing shapes, and screen management—to create graphics quickly and efficiently.
Hello, everyone. I'm a Python enthusiast.
Introduction
When you draw with code, it can feel tricky; today we introduce the magical drawing module turtle . It lets the pen move freely with simple commands, making graphics creation intuitive.
Importing the Module
import turtleBasic Turtle Usage
1. Set Canvas Size
To draw, you need a canvas. Use the following command to set its width, height, and background color:
turtle.screensize(width, height, color) # set width, height, color2. Set Canvas Coordinates
Configure the window's position on the screen:
turtle.setup(width, height, startx, starty) # set width, height, start x, start y3. Show Main Window
Like Tkinter, you need mainloop() to display the window:
import turtle
turtle.Screen().title('Drawing Tool') # set window title
turtle.screensize(400, 400, "red") # canvas size and background color
turtle.setup(width=600, height=600, startx=300, starty=50) # window size and position
turtle.mainloop() # display the main window4. Configure the Pen
Create a pen and set its size and color:
turtle.pencolor('green') # set pen color to green (default is black)
turtle.pensize(1) # set pen size
turtle.speed() # set pen speed (range 0-10)This creates the first arrow; the turtle draws by moving this arrow.
5. Basic Drawing Commands
1. Forward
turtle.forward(100) # move forward 100 pixels
turtle.fd(100)2. Backward
turtle.backward(200) # move backward 200 pixels
turtle.back(200)
turtle.bk(200)3. Go To
turtle.goto(100, 200) # move directly to (100,200)
turtle.setpos(100, 200)
turtle.setposition(100, 200)4. Left
turtle.left(50) # turn left 50 degrees
turtle.lt(50)5. Right
turtle.right(60) # turn right 60 degrees
turtle.rt(60)6. Pen Up (Prepare to Move)
turtle.penup()7. Pen Down (Resume Drawing)
turtle.pendown()8. Draw Circle
turtle.circle(radius, extent, steps) # draw a circle
turtle.dot(int) # also draws a dot9. Move Along X Axis
turtle.setx(100) # move 100 pixels on X axis10. Move Along Y Axis
turtle.sety(100) # move 100 pixels on Y axis11. Set Heading Angle
turtle.setheading(90) # set current angle to 90 degrees
turtle.seth(90)12. Return Home
turtle.home()Below is a complete script demonstrating these commands and showing the turtle's trajectory:
import turtle
import time
t = turtle.Pen()
t.shape('turtle')
t.pencolor('green')
turtle.bgcolor('gray')
t.width = 3
time.sleep(1)
t.penup() # prepare to move
t.forward(100) # forward 100
time.sleep(1)
t.backward(200) # backward 200
time.sleep(1)
t.goto(100, 200) # go to (100,200)
time.sleep(1)
t.left(50) # left 50
time.sleep(1)
t.right(60) # right 60
time.sleep(1)
t.penup() # prepare to move
t.setx(100) # move on X
time.sleep(1)
t.sety(100) # move on Y
turtle.seth(90)
turtle.home()
turtle.mainloop() # keep window openThe turtle's path can also be visualized:
Additional Commands
13. Stamp (Copy Current Shape)
turtle.stamp()14. Clear Stamps
turtle.clearstamp()15. Clear All Stamps
turtle.clearstamps()16. Undo
turtle.undo()17. Get Position
turtle.position()
# or turtle.pos()18. Angle to a Point
turtle.towards(x, y)19. X Coordinate
turtle.xcor()20. Y Coordinate
turtle.ycor()21. Heading
turtle.heading()22. Distance to a Point
turtle.distance(x, y)23. Clear Drawing (Keep Turtle State)
turtle.clear()
# or turtle.clearscreen()24. Reset (Clear and Reset State)
turtle.reset()
# or turtle.resetscreen()25. Visibility Check
turtle.isvisible()26. Write Text
turtle.write(text, font)27. Fill Color
turtle.fillcolor('red')28. Pen Down Check
turtle.isdown() # note: no isup() method29. Hide Turtle
turtle.hideturtle()
# or turtle.ht()30. Show Turtle
turtle.showturtle()
# or turtle.st()31. Begin Fill
turtle.begin_fill()32. End Fill
turtle.end_fill()33. Set Circle Degrees
turtle.degrees(fullcircle=360.0)34. Use Radians
turtle.radians()Advanced Control
35. Main Loop and Done
turtle.mainloop()
# or turtle.done36. Set Mode
turtle.mode('standard') # counter‑clockwise
# or 'world' for clockwise37. Set Drawing Delay (ms)
turtle.delay(delay=None)38. Record Polygon Vertices
turtle.begin_poly()
# draw shape
turtle.end_poly()
poly = turtle.get_poly()Conclusion
The turtle module is a powerful and easy‑to‑use tool for creating graphics in Python; with just a few intuitive commands you can produce sophisticated drawings, and many more features are available for further exploration.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Python Crawling & Data Mining
Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
