turtle --- 龜圖學 (Turtle graphics)¶
原始碼:Lib/turtle.py
介紹¶
龜圖學是由 Wally Feurzeig,Seymour Papert 與 Cynthia Solomon 於 1967 年開發的,一個 以 Logo 程式語言撰寫的廣受歡迎的幾何繪圖工具。
This is an optional module. If it is missing from your copy of CPython, look for documentation from your distributor (that is, whoever provided Python to you). If you are the distributor, see 可選模組的需求.
Get started¶
Imagine a robotic turtle starting at (0, 0) in the x-y plane. After an import turtle, give it the
command turtle.forward(15), and it moves (on-screen!) 15 pixels in the
direction it is facing, drawing a line as it moves. Give it the command
turtle.right(25), and it rotates in-place 25 degrees clockwise.
In Python, turtle graphics provides a representation of a physical "turtle" (a little robot with a pen) that draws on a sheet of paper on the floor.
It's an effective and well-proven way for learners to encounter programming concepts and interaction with software, as it provides instant, visible feedback. It also provides convenient access to graphical output in general.
Turtle drawing was originally created as an educational tool, to be used by teachers in the classroom. For the programmer who needs to produce some graphical output it can be a way to do that without the overhead of introducing more complex or external libraries into their work.
教學¶
New users should start here. In this tutorial we'll explore some of the basics of turtle drawing.
啟動一個烏龜環境¶
在 Python shell 中,引入 turtle 模組中所有物件:
from turtle import *
If you run into a No module named '_tkinter' error, you'll have to
install the Tk interface package on your system.
基本繪圖¶
Send the turtle forward 100 steps:
forward(100)
You should see (most likely, in a new window on your display) a line drawn by the turtle, heading East. Change the direction of the turtle, so that it turns 120 degrees left (anti-clockwise):
left(120)
Let's continue by drawing a triangle:
forward(100)
left(120)
forward(100)
Notice how the turtle, represented by an arrow, points in different directions as you steer it.
Experiment with those commands, and also with backward() and
right().
Pen control¶
Try changing the color - for example, color('blue') - and
width of the line - for example, width(3) - and then drawing again.
You can also move the turtle around without drawing, by lifting up the pen:
up() before moving. To start drawing again, use down().
The turtle's position¶
Send your turtle back to its starting-point (useful if it has disappeared off-screen):
home()
The home position is at the center of the turtle's screen. If you ever need to know them, get the turtle's x-y coordinates with:
pos()
Home is at (0, 0).
And after a while, it will probably help to clear the window so we can start anew:
clearscreen()
Making algorithmic patterns¶
Using loops, it's possible to build up geometric patterns:
for steps in range(100):
for c in ('blue', 'red', 'green'):
color(c)
forward(steps)
right(30)
- which of course, are limited only by the imagination!
Let's draw the star shape at the top of this page. We want red lines, filled in with yellow:
color('red')
fillcolor('yellow')
Just as up() and down() determine whether lines will be drawn,
filling can be turned on and off:
begin_fill()
Next we'll create a loop:
while True:
forward(200)
left(170)
if abs(pos()) < 1:
break
abs(pos()) < 1 is a good way to know when the turtle is back at its
home position.
Finally, complete the filling:
end_fill()
(Note that filling only actually takes place when you give the
end_fill() command.)
How to...¶
This section covers some typical turtle use-cases and approaches.
Get started as quickly as possible¶
One of the joys of turtle graphics is the immediate, visual feedback that's available from simple commands - it's an excellent way to introduce children to programming ideas, with a minimum of overhead (not just children, of course).
The turtle module makes this possible by exposing all its basic functionality
as functions, available with from turtle import *. The turtle
graphics tutorial covers this approach.
It's worth noting that many of the turtle commands also have even more terse
equivalents, such as fd() for forward(). These are especially
useful when working with learners for whom typing is not a skill.
You'll need to have the
Tk interface packageinstalled on your system for turtle graphics to work. Be warned that this is not always straightforward, so check this in advance if you're planning to use turtle graphics with a learner.
Automatically begin and end filling¶
Starting with Python 3.14, you can use the fill() context manager
instead of begin_fill() and end_fill() to automatically begin and
end fill. Here is an example:
with fill():
for i in range(4):
forward(100)
right(90)
forward(200)
The code above is equivalent to:
begin_fill()
for i in range(4):
forward(100)
right(90)
end_fill()
forward(200)
Use the turtle module namespace¶
Using from turtle import * is convenient - but be warned that it imports a
rather large collection of objects, and if you're doing anything but turtle
graphics you run the risk of a name conflict (this becomes even more an issue
if you're using turtle graphics in a script where other modules might be
imported).
The solution is to use import turtle - fd() becomes
turtle.fd(), width() becomes turtle.width() and so on. (If typing
"turtle" over and over again becomes tedious, use for example import turtle
as t instead.)
Use turtle graphics in a script¶
It's recommended to use the turtle module namespace as described
immediately above, for example:
import turtle as t
from random import random
for i in range(100):
steps = int(random() * 100)
angle = int(random() * 360)
t.right(angle)
t.fd(steps)
Another step is also required though - as soon as the script ends, Python will also close the turtle's window. Add:
t.mainloop()
to the end of the script. The script will now wait to be dismissed and will not exit until it is terminated, for example by closing the turtle graphics window.
Use object-oriented turtle graphics¶
Other than for very basic introductory purposes, or for trying things out as quickly as possible, it's more usual and much more powerful to use the object-oriented approach to turtle graphics. For example, this allows multiple turtles on screen at once.
In this approach, the various turtle commands are methods of objects (mostly of
Turtle objects). You can use the object-oriented approach in the shell,
but it would be more typical in a Python script.
The example above then becomes:
from turtle import Turtle
from random import random
t = Turtle()
for i in range(100):
steps = int(random() * 100)
angle = int(random() * 360)
t.right(angle)
t.fd(steps)
t.screen.mainloop()
Note the last line. t.screen is an instance of the Screen
that a Turtle instance exists on; it's created automatically along with
the turtle.
The turtle's screen can be customised, for example:
t.screen.title('Object-oriented turtle demo')
t.screen.bgcolor("orange")
Turtle graphics reference¶
備註
In the following documentation the argument list for functions is given. Methods, of course, have the additional first argument self which is omitted here.
Turtle methods¶
- Turtle motion
- Move and draw
- Tell Turtle's state
- Setting and measurement
- Pen control
- Drawing state
- Color control
- Filling
- More drawing control
- Turtle state
- Visibility
- Appearance
- Using events
- Special Turtle methods
Methods of TurtleScreen/Screen¶
- Window control
- Animation control
- Using screen events
- Settings and special methods
- Input methods
- Methods specific to Screen
Methods of RawTurtle/Turtle and corresponding functions¶
Most of the examples in this section refer to a Turtle instance called
turtle.