Skip to content

Commit 2425346

Browse files
evildmphugovk
andauthored
gh-106996: Add a how-to section to the turtle documentation (#107153)
Co-authored-by: Hugo van Kemenade <[email protected]>
1 parent 329e4a1 commit 2425346

File tree

1 file changed

+114
-0
lines changed

1 file changed

+114
-0
lines changed

Doc/library/turtle.rst

+114
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ graphical output it can be a way to do that without the overhead of
5555
introducing more complex or external libraries into their work.
5656

5757

58+
.. _turtle-tutorial:
59+
5860
Tutorial
5961
========
6062

@@ -174,6 +176,118 @@ Finally, complete the filling::
174176
``end_fill()`` command.)
175177

176178

179+
.. _turtle-how-to:
180+
181+
How to...
182+
=========
183+
184+
This section covers some typical turtle use-cases and approaches.
185+
186+
187+
Get started as quickly as possible
188+
----------------------------------
189+
190+
One of the joys of turtle graphics is the immediate, visual feedback that's
191+
available from simple commands - it's an excellent way to introduce children
192+
to programming ideas, with a minimum of overhead (not just children, of
193+
course).
194+
195+
The turtle module makes this possible by exposing all its basic functionality
196+
as functions, available with ``from turtle import *``. The :ref:`turtle
197+
graphics tutorial <turtle-tutorial>` covers this approach.
198+
199+
It's worth noting that many of the turtle commands also have even more terse
200+
equivalents, such as ``fd()`` for :func:`forward`. These are especially
201+
useful when working with learners for whom typing is not a skill.
202+
203+
.. _note:
204+
205+
You'll need to have the :mod:`Tk interface package <tkinter>` installed on
206+
your system for turtle graphics to work. Be warned that this is not
207+
always straightforward, so check this in advance if you're planning to
208+
use turtle graphics with a learner.
209+
210+
211+
Use the ``turtle`` module namespace
212+
-----------------------------------
213+
214+
Using ``from turtle import *`` is convenient - but be warned that it imports a
215+
rather large collection of objects, and if you're doing anything but turtle
216+
graphics you run the risk of a name conflict (this becomes even more an issue
217+
if you're using turtle graphics in a script where other modules might be
218+
imported).
219+
220+
The solution is to use ``import turtle`` - ``fd()`` becomes
221+
``turtle.fd()``, ``width()`` becomes ``turtle.width()`` and so on. (If typing
222+
"turtle" over and over again becomes tedious, use for example ``import turtle
223+
as t`` instead.)
224+
225+
226+
Use turtle graphics in a script
227+
-------------------------------
228+
229+
It's recommended to use the ``turtle`` module namespace as described
230+
immediately above, for example::
231+
232+
import turtle as t
233+
from random import random
234+
235+
for i in range(100):
236+
steps = int(random() * 100)
237+
angle = int(random() * 360)
238+
t.right(angle)
239+
t.fd(steps)
240+
241+
Another step is also required though - as soon as the script ends, Python
242+
will also close the turtle's window. Add::
243+
244+
t.mainloop()
245+
246+
to the end of the script. The script will now wait to be dismissed and
247+
will not exit until it is terminated, for example by closing the turtle
248+
graphics window.
249+
250+
251+
Use object-oriented turtle graphics
252+
-----------------------------------
253+
254+
.. seealso:: :ref:`Explanation of the object-oriented interface <turtle-explanation>`
255+
256+
Other than for very basic introductory purposes, or for trying things out
257+
as quickly as possible, it's more usual and much more powerful to use the
258+
object-oriented approach to turtle graphics. For example, this allows
259+
multiple turtles on screen at once.
260+
261+
In this approach, the various turtle commands are methods of objects (mostly of
262+
``Turtle`` objects). You *can* use the object-oriented approach in the shell,
263+
but it would be more typical in a Python script.
264+
265+
The example above then becomes::
266+
267+
from turtle import Turtle
268+
from random import random
269+
270+
t = Turtle()
271+
for i in range(100):
272+
steps = int(random() * 100)
273+
angle = int(random() * 360)
274+
t.right(angle)
275+
t.fd(steps)
276+
277+
t.screen.mainloop()
278+
279+
Note the last line. ``t.screen`` is an instance of the :class:`Screen`
280+
that a Turtle instance exists on; it's created automatically along with
281+
the turtle.
282+
283+
The turtle's screen can be customised, for example::
284+
285+
t.screen.title('Object-oriented turtle demo')
286+
t.screen.bgcolor("orange")
287+
288+
289+
.. _turtle-explanation:
290+
177291
Explanation
178292
===========
179293

0 commit comments

Comments
 (0)