Skip to content

Commit fab36fb

Browse files
miss-islingtonevildmphugovk
authored
[3.12] gh-106996: Add a how-to section to the turtle documentation (GH-107153) (#107233)
Co-authored-by: Daniele Procida <daniele@vurt.org> Co-authored-by: Hugo van Kemenade <hugovk@users.noreply.github.com>
1 parent 2cdde10 commit fab36fb

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
@@ -46,6 +46,8 @@ graphical output it can be a way to do that without the overhead of
4646
introducing more complex or external libraries into their work.
4747

4848

49+
.. _turtle-tutorial:
50+
4951
Tutorial
5052
========
5153

@@ -165,6 +167,118 @@ Finally, complete the filling::
165167
``end_fill()`` command.)
166168

167169

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

0 commit comments

Comments
 (0)