annotate Lib/turtle.py @ 79453:366c19bb5ca2

MERGE: Closes #16126: PyErr_Format format mismatch in _testcapimodule.c
author Jesus Cea <jcea@jcea.es>
date Thu, 04 Oct 2012 16:07:03 +0200
parents 8d60c1c89105
children 0547f89aa620 cb876235f29d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1 #
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2 # turtle.py: a Tkinter based turtle graphics module for Python
53772
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
3 # Version 1.1b - 4. 5. 2009
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
4 #
58218
89a9dd8c628d Merged revisions 77209,77229,77359-77360,77371 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 56893
diff changeset
5 # Copyright (C) 2006 - 2010 Gregor Lingl
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
6 # email: glingl@aon.at
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
7 #
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
8 # This software is provided 'as-is', without any express or implied
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
9 # warranty. In no event will the authors be held liable for any damages
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
10 # arising from the use of this software.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
11 #
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
12 # Permission is granted to anyone to use this software for any purpose,
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
13 # including commercial applications, and to alter it and redistribute it
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
14 # freely, subject to the following restrictions:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
15 #
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
16 # 1. The origin of this software must not be misrepresented; you must not
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
17 # claim that you wrote the original software. If you use this software
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
18 # in a product, an acknowledgment in the product documentation would be
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
19 # appreciated but is not required.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
20 # 2. Altered source versions must be plainly marked as such, and must not be
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
21 # misrepresented as being the original software.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
22 # 3. This notice may not be removed or altered from any source distribution.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
23
9585
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
24
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 31515
diff changeset
25 """
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 31515
diff changeset
26 Turtle graphics is a popular way for introducing programming to
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 31515
diff changeset
27 kids. It was part of the original Logo programming language developed
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
28 by Wally Feurzig and Seymour Papert in 1966.
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 31515
diff changeset
29
71766
bf4f65043d7d #12677: correct turtle orientation in doc
Sandro Tosi <sandro.tosi@gmail.com>
parents: 68586
diff changeset
30 Imagine a robotic turtle starting at (0, 0) in the x-y plane. After an ``import turtle``, give it
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 31515
diff changeset
31 the command turtle.forward(15), and it moves (on-screen!) 15 pixels in
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 31515
diff changeset
32 the direction it is facing, drawing a line as it moves. Give it the
71766
bf4f65043d7d #12677: correct turtle orientation in doc
Sandro Tosi <sandro.tosi@gmail.com>
parents: 68586
diff changeset
33 command turtle.right(25), and it rotates in-place 25 degrees clockwise.
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 31515
diff changeset
34
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 31515
diff changeset
35 By combining together these and similar commands, intricate shapes and
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 31515
diff changeset
36 pictures can easily be drawn.
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
37
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
38 ----- turtle.py
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
39
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
40 This module is an extended reimplementation of turtle.py from the
51765
37e4df9fdaa6 Merged revisions 69821 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 51763
diff changeset
41 Python standard distribution up to Python 2.5. (See: http://www.python.org)
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
42
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
43 It tries to keep the merits of turtle.py and to be (nearly) 100%
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
44 compatible with it. This means in the first place to enable the
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
45 learning programmer to use all the commands, classes and methods
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
46 interactively when using the module from within IDLE run with
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
47 the -n switch.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
48
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
49 Roughly it has the following features added:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
50
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
51 - Better animation of the turtle movements, especially of turning the
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
52 turtle. So the turtles can more easily be used as a visual feedback
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
53 instrument by the (beginning) programmer.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
54
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
55 - Different turtle shapes, gif-images as turtle shapes, user defined
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
56 and user controllable turtle shapes, among them compound
51765
37e4df9fdaa6 Merged revisions 69821 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 51763
diff changeset
57 (multicolored) shapes. Turtle shapes can be stretched and tilted, which
51763
e0815fee24f6 Merged revisions 69819 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 49759
diff changeset
58 makes turtles very versatile geometrical objects.
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
59
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
60 - Fine control over turtle movement and screen updates via delay(),
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
61 and enhanced tracer() and speed() methods.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
62
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
63 - Aliases for the most commonly used commands, like fd for forward etc.,
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
64 following the early Logo traditions. This reduces the boring work of
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
65 typing long sequences of commands, which often occur in a natural way
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
66 when kids try to program fancy pictures on their first encounter with
51765
37e4df9fdaa6 Merged revisions 69821 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 51763
diff changeset
67 turtle graphics.
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
68
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
69 - Turtles now have an undo()-method with configurable undo-buffer.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
70
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
71 - Some simple commands/methods for creating event driven programs
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
72 (mouse-, key-, timer-events). Especially useful for programming games.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
73
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
74 - A scrollable Canvas class. The default scrollable Canvas can be
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
75 extended interactively as needed while playing around with the turtle(s).
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
76
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
77 - A TurtleScreen class with methods controlling background color or
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
78 background image, window and canvas size and other properties of the
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
79 TurtleScreen.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
80
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
81 - There is a method, setworldcoordinates(), to install a user defined
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
82 coordinate-system for the TurtleScreen.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
83
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
84 - The implementation uses a 2-vector class named Vec2D, derived from tuple.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
85 This class is public, so it can be imported by the application programmer,
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
86 which makes certain types of computations very natural and compact.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
87
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
88 - Appearance of the TurtleScreen and the Turtles at startup/import can be
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
89 configured by means of a turtle.cfg configuration file.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
90 The default configuration mimics the appearance of the old turtle module.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
91
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
92 - If configured appropriately the module reads in docstrings from a docstring
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
93 dictionary in some different language, supplied separately and replaces
51765
37e4df9fdaa6 Merged revisions 69821 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 51763
diff changeset
94 the English ones by those read in. There is a utility function
37e4df9fdaa6 Merged revisions 69821 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 51763
diff changeset
95 write_docstringdict() to write a dictionary with the original (English)
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
96 docstrings to disc, so it can serve as a template for translations.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
97
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
98 Behind the scenes there are some features included with possible
72985
e19ccbbe70dd Remove duplication.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 71766
diff changeset
99 extensions in mind. These will be commented and documented elsewhere.
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
100
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 31515
diff changeset
101 """
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 31515
diff changeset
102
53772
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
103 _ver = "turtle 1.1b- - for Python 3.1 - 4. 5. 2009"
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
104
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
105 # print(_ver)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
106
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
107 import tkinter as TK
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
108 import types
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
109 import math
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
110 import time
65656
4b542217e93e Issue #10193: Simplified instrospection used by turtle module
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 64405
diff changeset
111 import inspect
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
112
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
113 from os.path import isfile, split, join
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
114 from copy import deepcopy
53772
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
115 from tkinter import simpledialog
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
116
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
117 _tg_classes = ['ScrolledCanvas', 'TurtleScreen', 'Screen',
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
118 'RawTurtle', 'Turtle', 'RawPen', 'Pen', 'Shape', 'Vec2D']
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
119 _tg_screen_functions = ['addshape', 'bgcolor', 'bgpic', 'bye',
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
120 'clearscreen', 'colormode', 'delay', 'exitonclick', 'getcanvas',
53772
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
121 'getshapes', 'listen', 'mainloop', 'mode', 'numinput',
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
122 'onkey', 'onkeypress', 'onkeyrelease', 'onscreenclick', 'ontimer',
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
123 'register_shape', 'resetscreen', 'screensize', 'setup',
53772
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
124 'setworldcoordinates', 'textinput', 'title', 'tracer', 'turtles', 'update',
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
125 'window_height', 'window_width']
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
126 _tg_turtle_functions = ['back', 'backward', 'begin_fill', 'begin_poly', 'bk',
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
127 'circle', 'clear', 'clearstamp', 'clearstamps', 'clone', 'color',
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
128 'degrees', 'distance', 'dot', 'down', 'end_fill', 'end_poly', 'fd',
56893
70eb6a13abcd Merged revisions 75393,75416,75581,75609,75615 via svnmerge from
Georg Brandl <georg@python.org>
parents: 53772
diff changeset
129 'fillcolor', 'filling', 'forward', 'get_poly', 'getpen', 'getscreen', 'get_shapepoly',
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
130 'getturtle', 'goto', 'heading', 'hideturtle', 'home', 'ht', 'isdown',
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
131 'isvisible', 'left', 'lt', 'onclick', 'ondrag', 'onrelease', 'pd',
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
132 'pen', 'pencolor', 'pendown', 'pensize', 'penup', 'pos', 'position',
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
133 'pu', 'radians', 'right', 'reset', 'resizemode', 'rt',
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
134 'seth', 'setheading', 'setpos', 'setposition', 'settiltangle',
53772
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
135 'setundobuffer', 'setx', 'sety', 'shape', 'shapesize', 'shapetransform', 'shearfactor', 'showturtle',
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
136 'speed', 'st', 'stamp', 'tilt', 'tiltangle', 'towards',
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
137 'turtlesize', 'undo', 'undobufferentries', 'up', 'width',
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
138 'write', 'xcor', 'ycor']
53772
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
139 _tg_utilities = ['write_docstringdict', 'done']
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
140
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
141 __all__ = (_tg_classes + _tg_screen_functions + _tg_turtle_functions +
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
142 _tg_utilities) # + _math_functions)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
143
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
144 _alias_list = ['addshape', 'backward', 'bk', 'fd', 'ht', 'lt', 'pd', 'pos',
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
145 'pu', 'rt', 'seth', 'setpos', 'setposition', 'st',
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
146 'turtlesize', 'up', 'width']
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
147
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
148 _CFG = {"width" : 0.5, # Screen
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
149 "height" : 0.75,
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
150 "canvwidth" : 400,
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
151 "canvheight": 300,
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
152 "leftright": None,
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
153 "topbottom": None,
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
154 "mode": "standard", # TurtleScreen
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
155 "colormode": 1.0,
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
156 "delay": 10,
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
157 "undobuffersize": 1000, # RawTurtle
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
158 "shape": "classic",
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
159 "pencolor" : "black",
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
160 "fillcolor" : "black",
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
161 "resizemode" : "noresize",
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
162 "visible" : True,
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
163 "language": "english", # docstrings
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
164 "exampleturtle": "turtle",
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
165 "examplescreen": "screen",
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
166 "title": "Python Turtle Graphics",
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
167 "using_IDLE": False
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
168 }
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
169
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
170 def config_dict(filename):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
171 """Convert content of config-file into dictionary."""
64405
06e5fd4959e8 Use a context manager for some file objects.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 60058
diff changeset
172 with open(filename, "r") as f:
06e5fd4959e8 Use a context manager for some file objects.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 60058
diff changeset
173 cfglines = f.readlines()
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
174 cfgdict = {}
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
175 for line in cfglines:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
176 line = line.strip()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
177 if not line or line.startswith("#"):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
178 continue
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
179 try:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
180 key, value = line.split("=")
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
181 except:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
182 print("Bad line in config-file %s:\n%s" % (filename,line))
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
183 continue
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
184 key = key.strip()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
185 value = value.strip()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
186 if value in ["True", "False", "None", "''", '""']:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
187 value = eval(value)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
188 else:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
189 try:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
190 if "." in value:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
191 value = float(value)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
192 else:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
193 value = int(value)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
194 except:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
195 pass # value need not be converted
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
196 cfgdict[key] = value
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
197 return cfgdict
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
198
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
199 def readconfig(cfgdict):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
200 """Read config-files, change configuration-dict accordingly.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
201
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
202 If there is a turtle.cfg file in the current working directory,
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
203 read it from there. If this contains an importconfig-value,
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
204 say 'myway', construct filename turtle_mayway.cfg else use
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
205 turtle.cfg and read it from the import-directory, where
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
206 turtle.py is located.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
207 Update configuration dictionary first according to config-file,
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
208 in the import directory, then according to config-file in the
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
209 current working directory.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
210 If no config-file is found, the default configuration is used.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
211 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
212 default_cfg = "turtle.cfg"
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
213 cfgdict1 = {}
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
214 cfgdict2 = {}
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
215 if isfile(default_cfg):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
216 cfgdict1 = config_dict(default_cfg)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
217 if "importconfig" in cfgdict1:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
218 default_cfg = "turtle_%s.cfg" % cfgdict1["importconfig"]
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
219 try:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
220 head, tail = split(__file__)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
221 cfg_file2 = join(head, default_cfg)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
222 except:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
223 cfg_file2 = ""
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
224 if isfile(cfg_file2):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
225 cfgdict2 = config_dict(cfg_file2)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
226 _CFG.update(cfgdict2)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
227 _CFG.update(cfgdict1)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
228
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
229 try:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
230 readconfig(_CFG)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
231 except:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
232 print ("No configfile read, reason unknown")
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
233
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
234
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
235 class Vec2D(tuple):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
236 """A 2 dimensional vector class, used as a helper class
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
237 for implementing turtle graphics.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
238 May be useful for turtle graphics programs also.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
239 Derived from tuple, so a vector is a tuple!
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
240
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
241 Provides (for a, b vectors, k number):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
242 a+b vector addition
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
243 a-b vector subtraction
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
244 a*b inner product
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
245 k*a and a*k multiplication with scalar
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
246 |a| absolute value of a
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
247 a.rotate(angle) rotation
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
248 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
249 def __new__(cls, x, y):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
250 return tuple.__new__(cls, (x, y))
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
251 def __add__(self, other):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
252 return Vec2D(self[0]+other[0], self[1]+other[1])
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
253 def __mul__(self, other):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
254 if isinstance(other, Vec2D):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
255 return self[0]*other[0]+self[1]*other[1]
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
256 return Vec2D(self[0]*other, self[1]*other)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
257 def __rmul__(self, other):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
258 if isinstance(other, int) or isinstance(other, float):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
259 return Vec2D(self[0]*other, self[1]*other)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
260 def __sub__(self, other):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
261 return Vec2D(self[0]-other[0], self[1]-other[1])
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
262 def __neg__(self):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
263 return Vec2D(-self[0], -self[1])
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
264 def __abs__(self):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
265 return (self[0]**2 + self[1]**2)**0.5
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
266 def rotate(self, angle):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
267 """rotate self counterclockwise by angle
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
268 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
269 perp = Vec2D(-self[1], self[0])
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
270 angle = angle * math.pi / 180.0
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
271 c, s = math.cos(angle), math.sin(angle)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
272 return Vec2D(self[0]*c+perp[0]*s, self[1]*c+perp[1]*s)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
273 def __getnewargs__(self):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
274 return (self[0], self[1])
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
275 def __repr__(self):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
276 return "(%.2f,%.2f)" % self
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
277
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
278
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
279 ##############################################################################
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
280 ### From here up to line : Tkinter - Interface for turtle.py ###
51765
37e4df9fdaa6 Merged revisions 69821 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 51763
diff changeset
281 ### May be replaced by an interface to some different graphics toolkit ###
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
282 ##############################################################################
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
283
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
284 ## helper functions for Scrolled Canvas, to forward Canvas-methods
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
285 ## to ScrolledCanvas class
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
286
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
287 def __methodDict(cls, _dict):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
288 """helper function for Scrolled Canvas"""
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
289 baseList = list(cls.__bases__)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
290 baseList.reverse()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
291 for _super in baseList:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
292 __methodDict(_super, _dict)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
293 for key, value in cls.__dict__.items():
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
294 if type(value) == types.FunctionType:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
295 _dict[key] = value
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
296
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
297 def __methods(cls):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
298 """helper function for Scrolled Canvas"""
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
299 _dict = {}
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
300 __methodDict(cls, _dict)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
301 return _dict.keys()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
302
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
303 __stringBody = (
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
304 'def %(method)s(self, *args, **kw): return ' +
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
305 'self.%(attribute)s.%(method)s(*args, **kw)')
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
306
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
307 def __forwardmethods(fromClass, toClass, toPart, exclude = ()):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
308 ### MANY CHANGES ###
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
309 _dict_1 = {}
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
310 __methodDict(toClass, _dict_1)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
311 _dict = {}
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
312 mfc = __methods(fromClass)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
313 for ex in _dict_1.keys():
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
314 if ex[:1] == '_' or ex[-1:] == '_' or ex in exclude or ex in mfc:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
315 pass
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
316 else:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
317 _dict[ex] = _dict_1[ex]
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
318
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
319 for method, func in _dict.items():
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
320 d = {'method': method, 'func': func}
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
321 if isinstance(toPart, str):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
322 execString = \
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
323 __stringBody % {'method' : method, 'attribute' : toPart}
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
324 exec(execString, d)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
325 setattr(fromClass, method, d[method]) ### NEWU!
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
326
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
327
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
328 class ScrolledCanvas(TK.Frame):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
329 """Modeled after the scrolled canvas class from Grayons's Tkinter book.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
330
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
331 Used as the default canvas, which pops up automatically when
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
332 using turtle graphics functions or the Turtle class.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
333 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
334 def __init__(self, master, width=500, height=350,
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
335 canvwidth=600, canvheight=500):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
336 TK.Frame.__init__(self, master, width=width, height=height)
49759
8b9bda5b8d4a Merged revisions 67279 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 49345
diff changeset
337 self._rootwindow = self.winfo_toplevel()
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
338 self.width, self.height = width, height
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
339 self.canvwidth, self.canvheight = canvwidth, canvheight
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
340 self.bg = "white"
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
341 self._canvas = TK.Canvas(master, width=width, height=height,
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
342 bg=self.bg, relief=TK.SUNKEN, borderwidth=2)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
343 self.hscroll = TK.Scrollbar(master, command=self._canvas.xview,
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
344 orient=TK.HORIZONTAL)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
345 self.vscroll = TK.Scrollbar(master, command=self._canvas.yview)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
346 self._canvas.configure(xscrollcommand=self.hscroll.set,
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
347 yscrollcommand=self.vscroll.set)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
348 self.rowconfigure(0, weight=1, minsize=0)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
349 self.columnconfigure(0, weight=1, minsize=0)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
350 self._canvas.grid(padx=1, in_ = self, pady=1, row=0,
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
351 column=0, rowspan=1, columnspan=1, sticky='news')
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
352 self.vscroll.grid(padx=1, in_ = self, pady=1, row=0,
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
353 column=1, rowspan=1, columnspan=1, sticky='news')
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
354 self.hscroll.grid(padx=1, in_ = self, pady=1, row=1,
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
355 column=0, rowspan=1, columnspan=1, sticky='news')
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
356 self.reset()
49759
8b9bda5b8d4a Merged revisions 67279 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 49345
diff changeset
357 self._rootwindow.bind('<Configure>', self.onResize)
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
358
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
359 def reset(self, canvwidth=None, canvheight=None, bg = None):
51765
37e4df9fdaa6 Merged revisions 69821 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 51763
diff changeset
360 """Adjust canvas and scrollbars according to given canvas size."""
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
361 if canvwidth:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
362 self.canvwidth = canvwidth
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
363 if canvheight:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
364 self.canvheight = canvheight
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
365 if bg:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
366 self.bg = bg
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
367 self._canvas.config(bg=bg,
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
368 scrollregion=(-self.canvwidth//2, -self.canvheight//2,
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
369 self.canvwidth//2, self.canvheight//2))
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
370 self._canvas.xview_moveto(0.5*(self.canvwidth - self.width + 30) /
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
371 self.canvwidth)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
372 self._canvas.yview_moveto(0.5*(self.canvheight- self.height + 30) /
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
373 self.canvheight)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
374 self.adjustScrolls()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
375
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
376
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
377 def adjustScrolls(self):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
378 """ Adjust scrollbars according to window- and canvas-size.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
379 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
380 cwidth = self._canvas.winfo_width()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
381 cheight = self._canvas.winfo_height()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
382 self._canvas.xview_moveto(0.5*(self.canvwidth-cwidth)/self.canvwidth)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
383 self._canvas.yview_moveto(0.5*(self.canvheight-cheight)/self.canvheight)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
384 if cwidth < self.canvwidth or cheight < self.canvheight:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
385 self.hscroll.grid(padx=1, in_ = self, pady=1, row=1,
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
386 column=0, rowspan=1, columnspan=1, sticky='news')
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
387 self.vscroll.grid(padx=1, in_ = self, pady=1, row=0,
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
388 column=1, rowspan=1, columnspan=1, sticky='news')
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
389 else:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
390 self.hscroll.grid_forget()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
391 self.vscroll.grid_forget()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
392
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
393 def onResize(self, event):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
394 """self-explanatory"""
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
395 self.adjustScrolls()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
396
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
397 def bbox(self, *args):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
398 """ 'forward' method, which canvas itself has inherited...
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
399 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
400 return self._canvas.bbox(*args)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
401
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
402 def cget(self, *args, **kwargs):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
403 """ 'forward' method, which canvas itself has inherited...
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
404 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
405 return self._canvas.cget(*args, **kwargs)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
406
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
407 def config(self, *args, **kwargs):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
408 """ 'forward' method, which canvas itself has inherited...
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
409 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
410 self._canvas.config(*args, **kwargs)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
411
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
412 def bind(self, *args, **kwargs):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
413 """ 'forward' method, which canvas itself has inherited...
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
414 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
415 self._canvas.bind(*args, **kwargs)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
416
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
417 def unbind(self, *args, **kwargs):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
418 """ 'forward' method, which canvas itself has inherited...
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
419 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
420 self._canvas.unbind(*args, **kwargs)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
421
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
422 def focus_force(self):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
423 """ 'forward' method, which canvas itself has inherited...
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
424 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
425 self._canvas.focus_force()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
426
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
427 __forwardmethods(ScrolledCanvas, TK.Canvas, '_canvas')
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
428
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
429
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
430 class _Root(TK.Tk):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
431 """Root class for Screen based on Tkinter."""
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
432 def __init__(self):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
433 TK.Tk.__init__(self)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
434
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
435 def setupcanvas(self, width, height, cwidth, cheight):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
436 self._canvas = ScrolledCanvas(self, width, height, cwidth, cheight)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
437 self._canvas.pack(expand=1, fill="both")
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
438
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
439 def _getcanvas(self):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
440 return self._canvas
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
441
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
442 def set_geometry(self, width, height, startx, starty):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
443 self.geometry("%dx%d%+d%+d"%(width, height, startx, starty))
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
444
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
445 def ondestroy(self, destroy):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
446 self.wm_protocol("WM_DELETE_WINDOW", destroy)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
447
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
448 def win_width(self):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
449 return self.winfo_screenwidth()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
450
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
451 def win_height(self):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
452 return self.winfo_screenheight()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
453
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
454 Canvas = TK.Canvas
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
455
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
456
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
457 class TurtleScreenBase(object):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
458 """Provide the basic graphics functionality.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
459 Interface between Tkinter and turtle.py.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
460
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
461 To port turtle.py to some different graphics toolkit
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
462 a corresponding TurtleScreenBase class has to be implemented.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
463 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
464
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
465 @staticmethod
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
466 def _blankimage():
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
467 """return a blank image object
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
468 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
469 img = TK.PhotoImage(width=1, height=1)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
470 img.blank()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
471 return img
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
472
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
473 @staticmethod
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
474 def _image(filename):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
475 """return an image object containing the
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
476 imagedata from a gif-file named filename.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
477 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
478 return TK.PhotoImage(file=filename)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
479
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
480 def __init__(self, cv):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
481 self.cv = cv
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
482 if isinstance(cv, ScrolledCanvas):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
483 w = self.cv.canvwidth
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
484 h = self.cv.canvheight
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
485 else: # expected: ordinary TK.Canvas
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
486 w = int(self.cv.cget("width"))
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
487 h = int(self.cv.cget("height"))
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
488 self.cv.config(scrollregion = (-w//2, -h//2, w//2, h//2 ))
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
489 self.canvwidth = w
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
490 self.canvheight = h
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
491 self.xscale = self.yscale = 1.0
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
492
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
493 def _createpoly(self):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
494 """Create an invisible polygon item on canvas self.cv)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
495 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
496 return self.cv.create_polygon((0, 0, 0, 0, 0, 0), fill="", outline="")
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
497
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
498 def _drawpoly(self, polyitem, coordlist, fill=None,
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
499 outline=None, width=None, top=False):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
500 """Configure polygonitem polyitem according to provided
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
501 arguments:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
502 coordlist is sequence of coordinates
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
503 fill is filling color
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
504 outline is outline color
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
505 top is a boolean value, which specifies if polyitem
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
506 will be put on top of the canvas' displaylist so it
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
507 will not be covered by other items.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
508 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
509 cl = []
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
510 for x, y in coordlist:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
511 cl.append(x * self.xscale)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
512 cl.append(-y * self.yscale)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
513 self.cv.coords(polyitem, *cl)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
514 if fill is not None:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
515 self.cv.itemconfigure(polyitem, fill=fill)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
516 if outline is not None:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
517 self.cv.itemconfigure(polyitem, outline=outline)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
518 if width is not None:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
519 self.cv.itemconfigure(polyitem, width=width)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
520 if top:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
521 self.cv.tag_raise(polyitem)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
522
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
523 def _createline(self):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
524 """Create an invisible line item on canvas self.cv)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
525 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
526 return self.cv.create_line(0, 0, 0, 0, fill="", width=2,
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
527 capstyle = TK.ROUND)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
528
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
529 def _drawline(self, lineitem, coordlist=None,
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
530 fill=None, width=None, top=False):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
531 """Configure lineitem according to provided arguments:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
532 coordlist is sequence of coordinates
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
533 fill is drawing color
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
534 width is width of drawn line.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
535 top is a boolean value, which specifies if polyitem
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
536 will be put on top of the canvas' displaylist so it
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
537 will not be covered by other items.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
538 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
539 if coordlist is not None:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
540 cl = []
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
541 for x, y in coordlist:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
542 cl.append(x * self.xscale)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
543 cl.append(-y * self.yscale)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
544 self.cv.coords(lineitem, *cl)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
545 if fill is not None:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
546 self.cv.itemconfigure(lineitem, fill=fill)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
547 if width is not None:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
548 self.cv.itemconfigure(lineitem, width=width)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
549 if top:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
550 self.cv.tag_raise(lineitem)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
551
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
552 def _delete(self, item):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
553 """Delete graphics item from canvas.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
554 If item is"all" delete all graphics items.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
555 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
556 self.cv.delete(item)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
557
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
558 def _update(self):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
559 """Redraw graphics items on canvas
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
560 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
561 self.cv.update()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
562
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
563 def _delay(self, delay):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
564 """Delay subsequent canvas actions for delay ms."""
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
565 self.cv.after(delay)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
566
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
567 def _iscolorstring(self, color):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
568 """Check if the string color is a legal Tkinter color string.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
569 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
570 try:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
571 rgb = self.cv.winfo_rgb(color)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
572 ok = True
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
573 except TK.TclError:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
574 ok = False
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
575 return ok
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
576
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
577 def _bgcolor(self, color=None):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
578 """Set canvas' backgroundcolor if color is not None,
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
579 else return backgroundcolor."""
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
580 if color is not None:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
581 self.cv.config(bg = color)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
582 self._update()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
583 else:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
584 return self.cv.cget("bg")
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
585
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
586 def _write(self, pos, txt, align, font, pencolor):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
587 """Write txt at pos in canvas with specified font
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
588 and color.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
589 Return text item and x-coord of right bottom corner
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
590 of text's bounding box."""
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
591 x, y = pos
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
592 x = x * self.xscale
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
593 y = y * self.yscale
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
594 anchor = {"left":"sw", "center":"s", "right":"se" }
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
595 item = self.cv.create_text(x-1, -y, text = txt, anchor = anchor[align],
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
596 fill = pencolor, font = font)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
597 x0, y0, x1, y1 = self.cv.bbox(item)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
598 self.cv.update()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
599 return item, x1-1
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
600
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
601 ## def _dot(self, pos, size, color):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
602 ## """may be implemented for some other graphics toolkit"""
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
603
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
604 def _onclick(self, item, fun, num=1, add=None):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
605 """Bind fun to mouse-click event on turtle.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
606 fun must be a function with two arguments, the coordinates
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
607 of the clicked point on the canvas.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
608 num, the number of the mouse-button defaults to 1
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
609 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
610 if fun is None:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
611 self.cv.tag_unbind(item, "<Button-%s>" % num)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
612 else:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
613 def eventfun(event):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
614 x, y = (self.cv.canvasx(event.x)/self.xscale,
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
615 -self.cv.canvasy(event.y)/self.yscale)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
616 fun(x, y)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
617 self.cv.tag_bind(item, "<Button-%s>" % num, eventfun, add)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
618
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
619 def _onrelease(self, item, fun, num=1, add=None):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
620 """Bind fun to mouse-button-release event on turtle.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
621 fun must be a function with two arguments, the coordinates
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
622 of the point on the canvas where mouse button is released.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
623 num, the number of the mouse-button defaults to 1
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
624
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
625 If a turtle is clicked, first _onclick-event will be performed,
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
626 then _onscreensclick-event.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
627 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
628 if fun is None:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
629 self.cv.tag_unbind(item, "<Button%s-ButtonRelease>" % num)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
630 else:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
631 def eventfun(event):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
632 x, y = (self.cv.canvasx(event.x)/self.xscale,
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
633 -self.cv.canvasy(event.y)/self.yscale)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
634 fun(x, y)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
635 self.cv.tag_bind(item, "<Button%s-ButtonRelease>" % num,
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
636 eventfun, add)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
637
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
638 def _ondrag(self, item, fun, num=1, add=None):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
639 """Bind fun to mouse-move-event (with pressed mouse button) on turtle.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
640 fun must be a function with two arguments, the coordinates of the
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
641 actual mouse position on the canvas.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
642 num, the number of the mouse-button defaults to 1
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
643
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
644 Every sequence of mouse-move-events on a turtle is preceded by a
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
645 mouse-click event on that turtle.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
646 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
647 if fun is None:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
648 self.cv.tag_unbind(item, "<Button%s-Motion>" % num)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
649 else:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
650 def eventfun(event):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
651 try:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
652 x, y = (self.cv.canvasx(event.x)/self.xscale,
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
653 -self.cv.canvasy(event.y)/self.yscale)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
654 fun(x, y)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
655 except:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
656 pass
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
657 self.cv.tag_bind(item, "<Button%s-Motion>" % num, eventfun, add)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
658
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
659 def _onscreenclick(self, fun, num=1, add=None):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
660 """Bind fun to mouse-click event on canvas.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
661 fun must be a function with two arguments, the coordinates
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
662 of the clicked point on the canvas.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
663 num, the number of the mouse-button defaults to 1
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
664
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
665 If a turtle is clicked, first _onclick-event will be performed,
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
666 then _onscreensclick-event.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
667 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
668 if fun is None:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
669 self.cv.unbind("<Button-%s>" % num)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
670 else:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
671 def eventfun(event):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
672 x, y = (self.cv.canvasx(event.x)/self.xscale,
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
673 -self.cv.canvasy(event.y)/self.yscale)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
674 fun(x, y)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
675 self.cv.bind("<Button-%s>" % num, eventfun, add)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
676
53772
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
677 def _onkeyrelease(self, fun, key):
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
678 """Bind fun to key-release event of key.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
679 Canvas must have focus. See method listen
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
680 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
681 if fun is None:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
682 self.cv.unbind("<KeyRelease-%s>" % key, None)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
683 else:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
684 def eventfun(event):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
685 fun()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
686 self.cv.bind("<KeyRelease-%s>" % key, eventfun)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
687
53772
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
688 def _onkeypress(self, fun, key=None):
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
689 """If key is given, bind fun to key-press event of key.
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
690 Otherwise bind fun to any key-press.
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
691 Canvas must have focus. See method listen.
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
692 """
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
693 if fun is None:
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
694 if key is None:
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
695 self.cv.unbind("<KeyPress>", None)
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
696 else:
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
697 self.cv.unbind("<KeyPress-%s>" % key, None)
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
698 else:
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
699 def eventfun(event):
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
700 fun()
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
701 if key is None:
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
702 self.cv.bind("<KeyPress>", eventfun)
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
703 else:
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
704 self.cv.bind("<KeyPress-%s>" % key, eventfun)
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
705
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
706 def _listen(self):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
707 """Set focus on canvas (in order to collect key-events)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
708 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
709 self.cv.focus_force()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
710
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
711 def _ontimer(self, fun, t):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
712 """Install a timer, which calls fun after t milliseconds.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
713 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
714 if t == 0:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
715 self.cv.after_idle(fun)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
716 else:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
717 self.cv.after(t, fun)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
718
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
719 def _createimage(self, image):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
720 """Create and return image item on canvas.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
721 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
722 return self.cv.create_image(0, 0, image=image)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
723
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
724 def _drawimage(self, item, pos, image):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
725 """Configure image item as to draw image object
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
726 at position (x,y) on canvas)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
727 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
728 x, y = pos
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
729 self.cv.coords(item, (x * self.xscale, -y * self.yscale))
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
730 self.cv.itemconfig(item, image=image)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
731
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
732 def _setbgpic(self, item, image):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
733 """Configure image item as to draw image object
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
734 at center of canvas. Set item to the first item
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
735 in the displaylist, so it will be drawn below
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
736 any other item ."""
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
737 self.cv.itemconfig(item, image=image)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
738 self.cv.tag_lower(item)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
739
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
740 def _type(self, item):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
741 """Return 'line' or 'polygon' or 'image' depending on
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
742 type of item.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
743 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
744 return self.cv.type(item)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
745
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
746 def _pointlist(self, item):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
747 """returns list of coordinate-pairs of points of item
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
748 Example (for insiders):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
749 >>> from turtle import *
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
750 >>> getscreen()._pointlist(getturtle().turtle._item)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
751 [(0.0, 9.9999999999999982), (0.0, -9.9999999999999982),
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
752 (9.9999999999999982, 0.0)]
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
753 >>> """
66407
cc9c51f9524f Merged revisions 86697 via svnmerge from
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 65952
diff changeset
754 cl = self.cv.coords(item)
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
755 pl = [(cl[i], -cl[i+1]) for i in range(0, len(cl), 2)]
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
756 return pl
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
757
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
758 def _setscrollregion(self, srx1, sry1, srx2, sry2):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
759 self.cv.config(scrollregion=(srx1, sry1, srx2, sry2))
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
760
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
761 def _rescale(self, xscalefactor, yscalefactor):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
762 items = self.cv.find_all()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
763 for item in items:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
764 coordinates = list(self.cv.coords(item))
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
765 newcoordlist = []
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
766 while coordinates:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
767 x, y = coordinates[:2]
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
768 newcoordlist.append(x * xscalefactor)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
769 newcoordlist.append(y * yscalefactor)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
770 coordinates = coordinates[2:]
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
771 self.cv.coords(item, *newcoordlist)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
772
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
773 def _resize(self, canvwidth=None, canvheight=None, bg=None):
51765
37e4df9fdaa6 Merged revisions 69821 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 51763
diff changeset
774 """Resize the canvas the turtles are drawing on. Does
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
775 not alter the drawing window.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
776 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
777 # needs amendment
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
778 if not isinstance(self.cv, ScrolledCanvas):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
779 return self.canvwidth, self.canvheight
60058
40f787b151ac Merged revisions 79297,79310,79382,79425-79427,79450 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58218
diff changeset
780 if canvwidth is canvheight is bg is None:
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
781 return self.cv.canvwidth, self.cv.canvheight
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
782 if canvwidth is not None:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
783 self.canvwidth = canvwidth
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
784 if canvheight is not None:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
785 self.canvheight = canvheight
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
786 self.cv.reset(canvwidth, canvheight, bg)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
787
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
788 def _window_size(self):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
789 """ Return the width and height of the turtle window.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
790 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
791 width = self.cv.winfo_width()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
792 if width <= 1: # the window isn't managed by a geometry manager
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
793 width = self.cv['width']
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
794 height = self.cv.winfo_height()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
795 if height <= 1: # the window isn't managed by a geometry manager
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
796 height = self.cv['height']
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
797 return width, height
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
798
53772
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
799 def mainloop(self):
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
800 """Starts event loop - calling Tkinter's mainloop function.
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
801
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
802 No argument.
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
803
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
804 Must be last statement in a turtle graphics program.
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
805 Must NOT be used if a script is run from within IDLE in -n mode
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
806 (No subprocess) - for interactive use of turtle graphics.
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
807
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
808 Example (for a TurtleScreen instance named screen):
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
809 >>> screen.mainloop()
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
810
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
811 """
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
812 TK.mainloop()
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
813
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
814 def textinput(self, title, prompt):
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
815 """Pop up a dialog window for input of a string.
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
816
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
817 Arguments: title is the title of the dialog window,
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
818 prompt is a text mostly describing what information to input.
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
819
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
820 Return the string input
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
821 If the dialog is canceled, return None.
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
822
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
823 Example (for a TurtleScreen instance named screen):
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
824 >>> screen.textinput("NIM", "Name of first player:")
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
825
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
826 """
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
827 return simpledialog.askstring(title, prompt)
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
828
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
829 def numinput(self, title, prompt, default=None, minval=None, maxval=None):
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
830 """Pop up a dialog window for input of a number.
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
831
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
832 Arguments: title is the title of the dialog window,
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
833 prompt is a text mostly describing what numerical information to input.
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
834 default: default value
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
835 minval: minimum value for imput
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
836 maxval: maximum value for input
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
837
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
838 The number input must be in the range minval .. maxval if these are
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
839 given. If not, a hint is issued and the dialog remains open for
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
840 correction. Return the number input.
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
841 If the dialog is canceled, return None.
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
842
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
843 Example (for a TurtleScreen instance named screen):
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
844 >>> screen.numinput("Poker", "Your stakes:", 1000, minval=10, maxval=10000)
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
845
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
846 """
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
847 return simpledialog.askfloat(title, prompt, initialvalue=default,
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
848 minvalue=minval, maxvalue=maxval)
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
849
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
850
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
851 ##############################################################################
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
852 ### End of Tkinter - interface ###
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
853 ##############################################################################
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
854
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
855
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
856 class Terminator (Exception):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
857 """Will be raised in TurtleScreen.update, if _RUNNING becomes False.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
858
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
859 Thus stops execution of turtle graphics script. Main purpose: use in
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
860 in the Demo-Viewer turtle.Demo.py.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
861 """
14957
db1cce1ffb60 Don't rename Tkinter to Tk; closes bug 115714
Martin v. Löwis <martin@v.loewis.de>
parents: 9585
diff changeset
862 pass
9585
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
863
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
864
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
865 class TurtleGraphicsError(Exception):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
866 """Some TurtleGraphics Error
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
867 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
868
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
869
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
870 class Shape(object):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
871 """Data structure modeling shapes.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
872
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
873 attribute _type is one of "polygon", "image", "compound"
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
874 attribute _data is - depending on _type a poygon-tuple,
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
875 an image or a list constructed using the addcomponent method.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
876 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
877 def __init__(self, type_, data=None):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
878 self._type = type_
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
879 if type_ == "polygon":
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
880 if isinstance(data, list):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
881 data = tuple(data)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
882 elif type_ == "image":
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
883 if isinstance(data, str):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
884 if data.lower().endswith(".gif") and isfile(data):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
885 data = TurtleScreen._image(data)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
886 # else data assumed to be Photoimage
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
887 elif type_ == "compound":
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
888 data = []
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
889 else:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
890 raise TurtleGraphicsError("There is no shape type %s" % type_)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
891 self._data = data
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
892
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
893 def addcomponent(self, poly, fill, outline=None):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
894 """Add component to a shape of type compound.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
895
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
896 Arguments: poly is a polygon, i. e. a tuple of number pairs.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
897 fill is the fillcolor of the component,
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
898 outline is the outline color of the component.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
899
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
900 call (for a Shapeobject namend s):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
901 -- s.addcomponent(((0,0), (10,10), (-10,10)), "red", "blue")
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
902
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
903 Example:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
904 >>> poly = ((0,0),(10,-5),(0,10),(-10,-5))
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
905 >>> s = Shape("compound")
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
906 >>> s.addcomponent(poly, "red", "blue")
73816
cc559e1e3bd8 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72985
diff changeset
907 >>> # .. add more components and then use register_shape()
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
908 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
909 if self._type != "compound":
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
910 raise TurtleGraphicsError("Cannot add component to %s Shape"
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
911 % self._type)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
912 if outline is None:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
913 outline = fill
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
914 self._data.append([poly, fill, outline])
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
915
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
916
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
917 class Tbuffer(object):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
918 """Ring buffer used as undobuffer for RawTurtle objects."""
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
919 def __init__(self, bufsize=10):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
920 self.bufsize = bufsize
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
921 self.buffer = [[None]] * bufsize
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
922 self.ptr = -1
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
923 self.cumulate = False
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
924 def reset(self, bufsize=None):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
925 if bufsize is None:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
926 for i in range(self.bufsize):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
927 self.buffer[i] = [None]
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
928 else:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
929 self.bufsize = bufsize
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
930 self.buffer = [[None]] * bufsize
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
931 self.ptr = -1
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
932 def push(self, item):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
933 if self.bufsize > 0:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
934 if not self.cumulate:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
935 self.ptr = (self.ptr + 1) % self.bufsize
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
936 self.buffer[self.ptr] = item
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
937 else:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
938 self.buffer[self.ptr].append(item)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
939 def pop(self):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
940 if self.bufsize > 0:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
941 item = self.buffer[self.ptr]
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
942 if item is None:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
943 return None
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
944 else:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
945 self.buffer[self.ptr] = [None]
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
946 self.ptr = (self.ptr - 1) % self.bufsize
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
947 return (item)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
948 def nr_of_items(self):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
949 return self.bufsize - self.buffer.count([None])
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
950 def __repr__(self):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
951 return str(self.buffer) + " " + str(self.ptr)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
952
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
953
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
954
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
955 class TurtleScreen(TurtleScreenBase):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
956 """Provides screen oriented methods like setbg etc.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
957
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
958 Only relies upon the methods of TurtleScreenBase and NOT
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
959 upon components of the underlying graphics toolkit -
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
960 which is Tkinter in this case.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
961 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
962 _RUNNING = True
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
963
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
964 def __init__(self, cv, mode=_CFG["mode"],
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
965 colormode=_CFG["colormode"], delay=_CFG["delay"]):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
966 self._shapes = {
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
967 "arrow" : Shape("polygon", ((-10,0), (10,0), (0,10))),
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
968 "turtle" : Shape("polygon", ((0,16), (-2,14), (-1,10), (-4,7),
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
969 (-7,9), (-9,8), (-6,5), (-7,1), (-5,-3), (-8,-6),
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
970 (-6,-8), (-4,-5), (0,-7), (4,-5), (6,-8), (8,-6),
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
971 (5,-3), (7,1), (6,5), (9,8), (7,9), (4,7), (1,10),
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
972 (2,14))),
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
973 "circle" : Shape("polygon", ((10,0), (9.51,3.09), (8.09,5.88),
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
974 (5.88,8.09), (3.09,9.51), (0,10), (-3.09,9.51),
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
975 (-5.88,8.09), (-8.09,5.88), (-9.51,3.09), (-10,0),
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
976 (-9.51,-3.09), (-8.09,-5.88), (-5.88,-8.09),
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
977 (-3.09,-9.51), (-0.00,-10.00), (3.09,-9.51),
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
978 (5.88,-8.09), (8.09,-5.88), (9.51,-3.09))),
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
979 "square" : Shape("polygon", ((10,-10), (10,10), (-10,10),
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
980 (-10,-10))),
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
981 "triangle" : Shape("polygon", ((10,-5.77), (0,11.55),
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
982 (-10,-5.77))),
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
983 "classic": Shape("polygon", ((0,0),(-5,-9),(0,-7),(5,-9))),
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
984 "blank" : Shape("image", self._blankimage())
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
985 }
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
986
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
987 self._bgpics = {"nopic" : ""}
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
988
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
989 TurtleScreenBase.__init__(self, cv)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
990 self._mode = mode
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
991 self._delayvalue = delay
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
992 self._colormode = _CFG["colormode"]
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
993 self._keys = []
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
994 self.clear()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
995
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
996 def clear(self):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
997 """Delete all drawings and all turtles from the TurtleScreen.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
998
53772
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
999 No argument.
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
1000
51765
37e4df9fdaa6 Merged revisions 69821 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 51763
diff changeset
1001 Reset empty TurtleScreen to its initial state: white background,
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1002 no backgroundimage, no eventbindings and tracing on.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1003
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1004 Example (for a TurtleScreen instance named screen):
73816
cc559e1e3bd8 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72985
diff changeset
1005 >>> screen.clear()
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1006
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1007 Note: this method is not available as function.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1008 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1009 self._delayvalue = _CFG["delay"]
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1010 self._colormode = _CFG["colormode"]
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1011 self._delete("all")
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1012 self._bgpic = self._createimage("")
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1013 self._bgpicname = "nopic"
9585
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
1014 self._tracing = 1
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1015 self._updatecounter = 0
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1016 self._turtles = []
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1017 self.bgcolor("white")
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1018 for btn in 1, 2, 3:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1019 self.onclick(None, btn)
53772
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
1020 self.onkeypress(None)
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1021 for key in self._keys[:]:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1022 self.onkey(None, key)
53772
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
1023 self.onkeypress(None, key)
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1024 Turtle._pen = None
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1025
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1026 def mode(self, mode=None):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1027 """Set turtle-mode ('standard', 'logo' or 'world') and perform reset.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1028
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1029 Optional argument:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1030 mode -- on of the strings 'standard', 'logo' or 'world'
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1031
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1032 Mode 'standard' is compatible with turtle.py.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1033 Mode 'logo' is compatible with most Logo-Turtle-Graphics.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1034 Mode 'world' uses userdefined 'worldcoordinates'. *Attention*: in
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1035 this mode angles appear distorted if x/y unit-ratio doesn't equal 1.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1036 If mode is not given, return the current mode.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1037
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1038 Mode Initial turtle heading positive angles
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1039 ------------|-------------------------|-------------------
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1040 'standard' to the right (east) counterclockwise
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1041 'logo' upward (north) clockwise
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1042
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1043 Examples:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1044 >>> mode('logo') # resets turtle heading to north
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1045 >>> mode()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1046 'logo'
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1047 """
60058
40f787b151ac Merged revisions 79297,79310,79382,79425-79427,79450 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58218
diff changeset
1048 if mode is None:
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1049 return self._mode
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1050 mode = mode.lower()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1051 if mode not in ["standard", "logo", "world"]:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1052 raise TurtleGraphicsError("No turtle-graphics-mode %s" % mode)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1053 self._mode = mode
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1054 if mode in ["standard", "logo"]:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1055 self._setscrollregion(-self.canvwidth//2, -self.canvheight//2,
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1056 self.canvwidth//2, self.canvheight//2)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1057 self.xscale = self.yscale = 1.0
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1058 self.reset()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1059
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1060 def setworldcoordinates(self, llx, lly, urx, ury):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1061 """Set up a user defined coordinate-system.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1062
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1063 Arguments:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1064 llx -- a number, x-coordinate of lower left corner of canvas
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1065 lly -- a number, y-coordinate of lower left corner of canvas
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1066 urx -- a number, x-coordinate of upper right corner of canvas
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1067 ury -- a number, y-coordinate of upper right corner of canvas
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1068
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1069 Set up user coodinat-system and switch to mode 'world' if necessary.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1070 This performs a screen.reset. If mode 'world' is already active,
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1071 all drawings are redrawn according to the new coordinates.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1072
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1073 But ATTENTION: in user-defined coordinatesystems angles may appear
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1074 distorted. (see Screen.mode())
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1075
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1076 Example (for a TurtleScreen instance named screen):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1077 >>> screen.setworldcoordinates(-10,-0.5,50,1.5)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1078 >>> for _ in range(36):
73816
cc559e1e3bd8 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72985
diff changeset
1079 ... left(10)
cc559e1e3bd8 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72985
diff changeset
1080 ... forward(0.5)
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1081 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1082 if self.mode() != "world":
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1083 self.mode("world")
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1084 xspan = float(urx - llx)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1085 yspan = float(ury - lly)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1086 wx, wy = self._window_size()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1087 self.screensize(wx-20, wy-20)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1088 oldxscale, oldyscale = self.xscale, self.yscale
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1089 self.xscale = self.canvwidth / xspan
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1090 self.yscale = self.canvheight / yspan
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1091 srx1 = llx * self.xscale
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1092 sry1 = -ury * self.yscale
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1093 srx2 = self.canvwidth + srx1
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1094 sry2 = self.canvheight + sry1
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1095 self._setscrollregion(srx1, sry1, srx2, sry2)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1096 self._rescale(self.xscale/oldxscale, self.yscale/oldyscale)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1097 self.update()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1098
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1099 def register_shape(self, name, shape=None):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1100 """Adds a turtle shape to TurtleScreen's shapelist.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1101
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1102 Arguments:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1103 (1) name is the name of a gif-file and shape is None.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1104 Installs the corresponding image shape.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1105 !! Image-shapes DO NOT rotate when turning the turtle,
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1106 !! so they do not display the heading of the turtle!
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1107 (2) name is an arbitrary string and shape is a tuple
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1108 of pairs of coordinates. Installs the corresponding
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1109 polygon shape
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1110 (3) name is an arbitrary string and shape is a
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1111 (compound) Shape object. Installs the corresponding
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1112 compound shape.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1113 To use a shape, you have to issue the command shape(shapename).
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1114
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1115 call: register_shape("turtle.gif")
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1116 --or: register_shape("tri", ((0,0), (10,10), (-10,10)))
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1117
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1118 Example (for a TurtleScreen instance named screen):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1119 >>> screen.register_shape("triangle", ((5,-3),(0,5),(-5,-3)))
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1120
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1121 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1122 if shape is None:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1123 # image
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1124 if name.lower().endswith(".gif"):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1125 shape = Shape("image", self._image(name))
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1126 else:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1127 raise TurtleGraphicsError("Bad arguments for register_shape.\n"
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1128 + "Use help(register_shape)" )
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1129 elif isinstance(shape, tuple):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1130 shape = Shape("polygon", shape)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1131 ## else shape assumed to be Shape-instance
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1132 self._shapes[name] = shape
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1133
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1134 def _colorstr(self, color):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1135 """Return color string corresponding to args.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1136
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1137 Argument may be a string or a tuple of three
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1138 numbers corresponding to actual colormode,
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1139 i.e. in the range 0<=n<=colormode.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1140
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1141 If the argument doesn't represent a color,
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1142 an error is raised.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1143 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1144 if len(color) == 1:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1145 color = color[0]
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1146 if isinstance(color, str):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1147 if self._iscolorstring(color) or color == "":
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1148 return color
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1149 else:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1150 raise TurtleGraphicsError("bad color string: %s" % str(color))
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1151 try:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1152 r, g, b = color
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1153 except:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1154 raise TurtleGraphicsError("bad color arguments: %s" % str(color))
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1155 if self._colormode == 1.0:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1156 r, g, b = [round(255.0*x) for x in (r, g, b)]
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1157 if not ((0 <= r <= 255) and (0 <= g <= 255) and (0 <= b <= 255)):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1158 raise TurtleGraphicsError("bad color sequence: %s" % str(color))
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1159 return "#%02x%02x%02x" % (r, g, b)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1160
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1161 def _color(self, cstr):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1162 if not cstr.startswith("#"):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1163 return cstr
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1164 if len(cstr) == 7:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1165 cl = [int(cstr[i:i+2], 16) for i in (1, 3, 5)]
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1166 elif len(cstr) == 4:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1167 cl = [16*int(cstr[h], 16) for h in cstr[1:]]
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1168 else:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1169 raise TurtleGraphicsError("bad colorstring: %s" % cstr)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1170 return tuple([c * self._colormode/255 for c in cl])
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1171
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1172 def colormode(self, cmode=None):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1173 """Return the colormode or set it to 1.0 or 255.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1174
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1175 Optional argument:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1176 cmode -- one of the values 1.0 or 255
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1177
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1178 r, g, b values of colortriples have to be in range 0..cmode.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1179
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1180 Example (for a TurtleScreen instance named screen):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1181 >>> screen.colormode()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1182 1.0
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1183 >>> screen.colormode(255)
73816
cc559e1e3bd8 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72985
diff changeset
1184 >>> pencolor(240,160,80)
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1185 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1186 if cmode is None:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1187 return self._colormode
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1188 if cmode == 1.0:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1189 self._colormode = float(cmode)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1190 elif cmode == 255:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1191 self._colormode = int(cmode)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1192
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1193 def reset(self):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1194 """Reset all Turtles on the Screen to their initial state.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1195
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1196 No argument.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1197
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1198 Example (for a TurtleScreen instance named screen):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1199 >>> screen.reset()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1200 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1201 for turtle in self._turtles:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1202 turtle._setmode(self._mode)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1203 turtle.reset()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1204
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1205 def turtles(self):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1206 """Return the list of turtles on the screen.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1207
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1208 Example (for a TurtleScreen instance named screen):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1209 >>> screen.turtles()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1210 [<turtle.Turtle object at 0x00E11FB0>]
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1211 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1212 return self._turtles
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1213
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1214 def bgcolor(self, *args):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1215 """Set or return backgroundcolor of the TurtleScreen.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1216
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1217 Arguments (if given): a color string or three numbers
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1218 in the range 0..colormode or a 3-tuple of such numbers.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1219
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1220 Example (for a TurtleScreen instance named screen):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1221 >>> screen.bgcolor("orange")
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1222 >>> screen.bgcolor()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1223 'orange'
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1224 >>> screen.bgcolor(0.5,0,0.5)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1225 >>> screen.bgcolor()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1226 '#800080'
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1227 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1228 if args:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1229 color = self._colorstr(args)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1230 else:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1231 color = None
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1232 color = self._bgcolor(color)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1233 if color is not None:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1234 color = self._color(color)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1235 return color
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1236
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1237 def tracer(self, n=None, delay=None):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1238 """Turns turtle animation on/off and set delay for update drawings.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1239
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1240 Optional arguments:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1241 n -- nonnegative integer
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1242 delay -- nonnegative integer
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1243
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1244 If n is given, only each n-th regular screen update is really performed.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1245 (Can be used to accelerate the drawing of complex graphics.)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1246 Second arguments sets delay value (see RawTurtle.delay())
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1247
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1248 Example (for a TurtleScreen instance named screen):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1249 >>> screen.tracer(8, 25)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1250 >>> dist = 2
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1251 >>> for i in range(200):
73816
cc559e1e3bd8 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72985
diff changeset
1252 ... fd(dist)
cc559e1e3bd8 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72985
diff changeset
1253 ... rt(90)
cc559e1e3bd8 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72985
diff changeset
1254 ... dist += 2
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1255 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1256 if n is None:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1257 return self._tracing
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1258 self._tracing = int(n)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1259 self._updatecounter = 0
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1260 if delay is not None:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1261 self._delayvalue = int(delay)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1262 if self._tracing:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1263 self.update()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1264
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1265 def delay(self, delay=None):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1266 """ Return or set the drawing delay in milliseconds.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1267
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1268 Optional argument:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1269 delay -- positive integer
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1270
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1271 Example (for a TurtleScreen instance named screen):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1272 >>> screen.delay(15)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1273 >>> screen.delay()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1274 15
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1275 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1276 if delay is None:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1277 return self._delayvalue
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1278 self._delayvalue = int(delay)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1279
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1280 def _incrementudc(self):
73816
cc559e1e3bd8 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72985
diff changeset
1281 """Increment upadate counter."""
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1282 if not TurtleScreen._RUNNING:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1283 TurtleScreen._RUNNNING = True
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1284 raise Terminator
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1285 if self._tracing > 0:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1286 self._updatecounter += 1
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1287 self._updatecounter %= self._tracing
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1288
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1289 def update(self):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1290 """Perform a TurtleScreen update.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1291 """
53772
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
1292 tracing = self._tracing
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
1293 self._tracing = True
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1294 for t in self.turtles():
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1295 t._update_data()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1296 t._drawturtle()
53772
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
1297 self._tracing = tracing
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1298 self._update()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1299
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1300 def window_width(self):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1301 """ Return the width of the turtle window.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1302
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1303 Example (for a TurtleScreen instance named screen):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1304 >>> screen.window_width()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1305 640
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1306 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1307 return self._window_size()[0]
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1308
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1309 def window_height(self):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1310 """ Return the height of the turtle window.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1311
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1312 Example (for a TurtleScreen instance named screen):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1313 >>> screen.window_height()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1314 480
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1315 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1316 return self._window_size()[1]
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1317
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1318 def getcanvas(self):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1319 """Return the Canvas of this TurtleScreen.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1320
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1321 No argument.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1322
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1323 Example (for a Screen instance named screen):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1324 >>> cv = screen.getcanvas()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1325 >>> cv
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1326 <turtle.ScrolledCanvas instance at 0x010742D8>
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1327 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1328 return self.cv
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1329
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1330 def getshapes(self):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1331 """Return a list of names of all currently available turtle shapes.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1332
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1333 No argument.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1334
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1335 Example (for a TurtleScreen instance named screen):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1336 >>> screen.getshapes()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1337 ['arrow', 'blank', 'circle', ... , 'turtle']
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1338 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1339 return sorted(self._shapes.keys())
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1340
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1341 def onclick(self, fun, btn=1, add=None):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1342 """Bind fun to mouse-click event on canvas.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1343
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1344 Arguments:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1345 fun -- a function with two arguments, the coordinates of the
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1346 clicked point on the canvas.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1347 num -- the number of the mouse-button, defaults to 1
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1348
73816
cc559e1e3bd8 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72985
diff changeset
1349 Example (for a TurtleScreen instance named screen)
cc559e1e3bd8 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72985
diff changeset
1350
cc559e1e3bd8 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72985
diff changeset
1351 >>> screen.onclick(goto)
cc559e1e3bd8 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72985
diff changeset
1352 >>> # Subsequently clicking into the TurtleScreen will
cc559e1e3bd8 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72985
diff changeset
1353 >>> # make the turtle move to the clicked point.
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1354 >>> screen.onclick(None)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1355 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1356 self._onscreenclick(fun, btn, add)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1357
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1358 def onkey(self, fun, key):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1359 """Bind fun to key-release event of key.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1360
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1361 Arguments:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1362 fun -- a function with no arguments
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1363 key -- a string: key (e.g. "a") or key-symbol (e.g. "space")
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1364
51765
37e4df9fdaa6 Merged revisions 69821 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 51763
diff changeset
1365 In order to be able to register key-events, TurtleScreen
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1366 must have focus. (See method listen.)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1367
73816
cc559e1e3bd8 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72985
diff changeset
1368 Example (for a TurtleScreen instance named screen):
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1369
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1370 >>> def f():
73816
cc559e1e3bd8 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72985
diff changeset
1371 ... fd(50)
cc559e1e3bd8 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72985
diff changeset
1372 ... lt(60)
cc559e1e3bd8 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72985
diff changeset
1373 ...
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1374 >>> screen.onkey(f, "Up")
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1375 >>> screen.listen()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1376
73816
cc559e1e3bd8 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72985
diff changeset
1377 Subsequently the turtle can be moved by repeatedly pressing
cc559e1e3bd8 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72985
diff changeset
1378 the up-arrow key, consequently drawing a hexagon
cc559e1e3bd8 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72985
diff changeset
1379
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1380 """
60058
40f787b151ac Merged revisions 79297,79310,79382,79425-79427,79450 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58218
diff changeset
1381 if fun is None:
53772
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
1382 if key in self._keys:
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
1383 self._keys.remove(key)
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1384 elif key not in self._keys:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1385 self._keys.append(key)
53772
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
1386 self._onkeyrelease(fun, key)
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
1387
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
1388 def onkeypress(self, fun, key=None):
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
1389 """Bind fun to key-press event of key if key is given,
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
1390 or to any key-press-event if no key is given.
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
1391
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
1392 Arguments:
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
1393 fun -- a function with no arguments
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
1394 key -- a string: key (e.g. "a") or key-symbol (e.g. "space")
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
1395
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
1396 In order to be able to register key-events, TurtleScreen
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
1397 must have focus. (See method listen.)
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
1398
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
1399 Example (for a TurtleScreen instance named screen
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
1400 and a Turtle instance named turtle):
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
1401
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
1402 >>> def f():
73816
cc559e1e3bd8 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72985
diff changeset
1403 ... fd(50)
cc559e1e3bd8 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72985
diff changeset
1404 ... lt(60)
cc559e1e3bd8 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72985
diff changeset
1405 ...
cc559e1e3bd8 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72985
diff changeset
1406 >>> screen.onkeypress(f, "Up")
53772
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
1407 >>> screen.listen()
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
1408
73816
cc559e1e3bd8 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72985
diff changeset
1409 Subsequently the turtle can be moved by repeatedly pressing
cc559e1e3bd8 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72985
diff changeset
1410 the up-arrow key, or by keeping pressed the up-arrow key.
cc559e1e3bd8 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72985
diff changeset
1411 consequently drawing a hexagon.
53772
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
1412 """
60058
40f787b151ac Merged revisions 79297,79310,79382,79425-79427,79450 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58218
diff changeset
1413 if fun is None:
53772
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
1414 if key in self._keys:
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
1415 self._keys.remove(key)
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
1416 elif key is not None and key not in self._keys:
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
1417 self._keys.append(key)
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
1418 self._onkeypress(fun, key)
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1419
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1420 def listen(self, xdummy=None, ydummy=None):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1421 """Set focus on TurtleScreen (in order to collect key-events)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1422
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1423 No arguments.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1424 Dummy arguments are provided in order
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1425 to be able to pass listen to the onclick method.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1426
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1427 Example (for a TurtleScreen instance named screen):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1428 >>> screen.listen()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1429 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1430 self._listen()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1431
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1432 def ontimer(self, fun, t=0):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1433 """Install a timer, which calls fun after t milliseconds.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1434
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1435 Arguments:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1436 fun -- a function with no arguments.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1437 t -- a number >= 0
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1438
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1439 Example (for a TurtleScreen instance named screen):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1440
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1441 >>> running = True
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1442 >>> def f():
73816
cc559e1e3bd8 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72985
diff changeset
1443 ... if running:
cc559e1e3bd8 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72985
diff changeset
1444 ... fd(50)
cc559e1e3bd8 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72985
diff changeset
1445 ... lt(60)
cc559e1e3bd8 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72985
diff changeset
1446 ... screen.ontimer(f, 250)
cc559e1e3bd8 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72985
diff changeset
1447 ...
cc559e1e3bd8 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72985
diff changeset
1448 >>> f() # makes the turtle marching around
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1449 >>> running = False
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1450 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1451 self._ontimer(fun, t)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1452
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1453 def bgpic(self, picname=None):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1454 """Set background image or return name of current backgroundimage.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1455
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1456 Optional argument:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1457 picname -- a string, name of a gif-file or "nopic".
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1458
68488
0991b40e895d #11515: fix several typos. Patch by Piotr Kasprzyk.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 66407
diff changeset
1459 If picname is a filename, set the corresponding image as background.
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1460 If picname is "nopic", delete backgroundimage, if present.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1461 If picname is None, return the filename of the current backgroundimage.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1462
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1463 Example (for a TurtleScreen instance named screen):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1464 >>> screen.bgpic()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1465 'nopic'
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1466 >>> screen.bgpic("landscape.gif")
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1467 >>> screen.bgpic()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1468 'landscape.gif'
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1469 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1470 if picname is None:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1471 return self._bgpicname
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1472 if picname not in self._bgpics:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1473 self._bgpics[picname] = self._image(picname)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1474 self._setbgpic(self._bgpic, self._bgpics[picname])
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1475 self._bgpicname = picname
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1476
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1477 def screensize(self, canvwidth=None, canvheight=None, bg=None):
51765
37e4df9fdaa6 Merged revisions 69821 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 51763
diff changeset
1478 """Resize the canvas the turtles are drawing on.
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1479
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1480 Optional arguments:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1481 canvwidth -- positive integer, new width of canvas in pixels
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1482 canvheight -- positive integer, new height of canvas in pixels
68585
bf94b6a73fc8 #11565: Fix several typos. Patch by Piotr Kasprzyk.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 68488
diff changeset
1483 bg -- colorstring or color-tuple, new backgroundcolor
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1484 If no arguments are given, return current (canvaswidth, canvasheight)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1485
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1486 Do not alter the drawing window. To observe hidden parts of
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1487 the canvas use the scrollbars. (Can make visible those parts
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1488 of a drawing, which were outside the canvas before!)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1489
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1490 Example (for a Turtle instance named turtle):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1491 >>> turtle.screensize(2000,1500)
73816
cc559e1e3bd8 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72985
diff changeset
1492 >>> # e.g. to search for an erroneously escaped turtle ;-)
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1493 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1494 return self._resize(canvwidth, canvheight, bg)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1495
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1496 onscreenclick = onclick
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1497 resetscreen = reset
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1498 clearscreen = clear
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1499 addshape = register_shape
53772
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
1500 onkeyrelease = onkey
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1501
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1502 class TNavigator(object):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1503 """Navigation part of the RawTurtle.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1504 Implements methods for turtle movement.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1505 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1506 START_ORIENTATION = {
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1507 "standard": Vec2D(1.0, 0.0),
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1508 "world" : Vec2D(1.0, 0.0),
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1509 "logo" : Vec2D(0.0, 1.0) }
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1510 DEFAULT_MODE = "standard"
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1511 DEFAULT_ANGLEOFFSET = 0
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1512 DEFAULT_ANGLEORIENT = 1
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1513
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1514 def __init__(self, mode=DEFAULT_MODE):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1515 self._angleOffset = self.DEFAULT_ANGLEOFFSET
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1516 self._angleOrient = self.DEFAULT_ANGLEORIENT
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1517 self._mode = mode
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1518 self.undobuffer = None
9585
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
1519 self.degrees()
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1520 self._mode = None
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1521 self._setmode(mode)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1522 TNavigator.reset(self)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1523
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1524 def reset(self):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1525 """reset turtle to its initial values
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1526
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1527 Will be overwritten by parent class
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1528 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1529 self._position = Vec2D(0.0, 0.0)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1530 self._orient = TNavigator.START_ORIENTATION[self._mode]
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1531
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1532 def _setmode(self, mode=None):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1533 """Set turtle-mode to 'standard', 'world' or 'logo'.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1534 """
60058
40f787b151ac Merged revisions 79297,79310,79382,79425-79427,79450 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58218
diff changeset
1535 if mode is None:
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1536 return self._mode
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1537 if mode not in ["standard", "logo", "world"]:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1538 return
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1539 self._mode = mode
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1540 if mode in ["standard", "world"]:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1541 self._angleOffset = 0
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1542 self._angleOrient = 1
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1543 else: # mode == "logo":
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1544 self._angleOffset = self._fullcircle/4.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1545 self._angleOrient = -1
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1546
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1547 def _setDegreesPerAU(self, fullcircle):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1548 """Helper function for degrees() and radians()"""
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1549 self._fullcircle = fullcircle
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1550 self._degreesPerAU = 360/fullcircle
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1551 if self._mode == "standard":
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1552 self._angleOffset = 0
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1553 else:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1554 self._angleOffset = fullcircle/4.
9585
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
1555
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
1556 def degrees(self, fullcircle=360.0):
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 31515
diff changeset
1557 """ Set angle measurement units to degrees.
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 31515
diff changeset
1558
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1559 Optional argument:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1560 fullcircle - a number
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1561
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1562 Set angle measurement units, i. e. set number
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1563 of 'degrees' for a full circle. Dafault value is
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1564 360 degrees.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1565
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1566 Example (for a Turtle instance named turtle):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1567 >>> turtle.left(90)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1568 >>> turtle.heading()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1569 90
65952
7ab72ba38fe0 Merged revisions 85732,85778,85785,85853,85930,86008,86089 via svnmerge from
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 60058
diff changeset
1570
7ab72ba38fe0 Merged revisions 85732,85778,85785,85853,85930,86008,86089 via svnmerge from
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 60058
diff changeset
1571 Change angle measurement unit to grad (also known as gon,
7ab72ba38fe0 Merged revisions 85732,85778,85785,85853,85930,86008,86089 via svnmerge from
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 60058
diff changeset
1572 grade, or gradian and equals 1/100-th of the right angle.)
7ab72ba38fe0 Merged revisions 85732,85778,85785,85853,85930,86008,86089 via svnmerge from
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 60058
diff changeset
1573 >>> turtle.degrees(400.0)
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1574 >>> turtle.heading()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1575 100
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1576
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 31515
diff changeset
1577 """
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1578 self._setDegreesPerAU(fullcircle)
9585
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
1579
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
1580 def radians(self):
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 31515
diff changeset
1581 """ Set the angle measurement units to radians.
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 31515
diff changeset
1582
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1583 No arguments.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1584
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1585 Example (for a Turtle instance named turtle):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1586 >>> turtle.heading()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1587 90
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 31515
diff changeset
1588 >>> turtle.radians()
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 31515
diff changeset
1589 >>> turtle.heading()
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1590 1.5707963267948966
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 31515
diff changeset
1591 """
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1592 self._setDegreesPerAU(2*math.pi)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1593
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1594 def _go(self, distance):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1595 """move turtle forward by specified distance"""
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1596 ende = self._position + self._orient * distance
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1597 self._goto(ende)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1598
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1599 def _rotate(self, angle):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1600 """Turn turtle counterclockwise by specified angle if angle > 0."""
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1601 angle *= self._degreesPerAU
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1602 self._orient = self._orient.rotate(angle)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1603
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1604 def _goto(self, end):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1605 """move turtle to position end."""
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1606 self._position = end
9585
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
1607
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
1608 def forward(self, distance):
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1609 """Move the turtle forward by the specified distance.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1610
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1611 Aliases: forward | fd
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1612
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1613 Argument:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1614 distance -- a number (integer or float)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1615
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1616 Move the turtle forward by the specified distance, in the direction
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1617 the turtle is headed.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1618
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1619 Example (for a Turtle instance named turtle):
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 31515
diff changeset
1620 >>> turtle.position()
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1621 (0.00, 0.00)
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 31515
diff changeset
1622 >>> turtle.forward(25)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 31515
diff changeset
1623 >>> turtle.position()
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1624 (25.00,0.00)
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 31515
diff changeset
1625 >>> turtle.forward(-75)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 31515
diff changeset
1626 >>> turtle.position()
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1627 (-50.00,0.00)
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 31515
diff changeset
1628 """
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1629 self._go(distance)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1630
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1631 def back(self, distance):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1632 """Move the turtle backward by distance.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1633
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1634 Aliases: back | backward | bk
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1635
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1636 Argument:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1637 distance -- a number
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1638
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1639 Move the turtle backward by distance ,opposite to the direction the
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1640 turtle is headed. Do not change the turtle's heading.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1641
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1642 Example (for a Turtle instance named turtle):
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 31515
diff changeset
1643 >>> turtle.position()
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1644 (0.00, 0.00)
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 31515
diff changeset
1645 >>> turtle.backward(30)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 31515
diff changeset
1646 >>> turtle.position()
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1647 (-30.00, 0.00)
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 31515
diff changeset
1648 """
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1649 self._go(-distance)
9585
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
1650
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
1651 def right(self, angle):
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1652 """Turn turtle right by angle units.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1653
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1654 Aliases: right | rt
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1655
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1656 Argument:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1657 angle -- a number (integer or float)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1658
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1659 Turn turtle right by angle units. (Units are by default degrees,
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 31515
diff changeset
1660 but can be set via the degrees() and radians() functions.)
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1661 Angle orientation depends on mode. (See this.)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1662
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1663 Example (for a Turtle instance named turtle):
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 31515
diff changeset
1664 >>> turtle.heading()
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1665 22.0
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 31515
diff changeset
1666 >>> turtle.right(45)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 31515
diff changeset
1667 >>> turtle.heading()
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 31515
diff changeset
1668 337.0
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 31515
diff changeset
1669 """
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1670 self._rotate(-angle)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1671
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1672 def left(self, angle):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1673 """Turn turtle left by angle units.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1674
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1675 Aliases: left | lt
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1676
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1677 Argument:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1678 angle -- a number (integer or float)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1679
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1680 Turn turtle left by angle units. (Units are by default degrees,
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1681 but can be set via the degrees() and radians() functions.)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1682 Angle orientation depends on mode. (See this.)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1683
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1684 Example (for a Turtle instance named turtle):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1685 >>> turtle.heading()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1686 22.0
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1687 >>> turtle.left(45)
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 31515
diff changeset
1688 >>> turtle.heading()
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 31515
diff changeset
1689 67.0
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 31515
diff changeset
1690 """
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1691 self._rotate(angle)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1692
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1693 def pos(self):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1694 """Return the turtle's current location (x,y), as a Vec2D-vector.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1695
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1696 Aliases: pos | position
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1697
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1698 No arguments.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1699
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1700 Example (for a Turtle instance named turtle):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1701 >>> turtle.pos()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1702 (0.00, 240.00)
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 31515
diff changeset
1703 """
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1704 return self._position
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1705
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1706 def xcor(self):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1707 """ Return the turtle's x coordinate.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1708
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1709 No arguments.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1710
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1711 Example (for a Turtle instance named turtle):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1712 >>> reset()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1713 >>> turtle.left(60)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1714 >>> turtle.forward(100)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1715 >>> print turtle.xcor()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1716 50.0
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1717 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1718 return self._position[0]
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1719
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1720 def ycor(self):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1721 """ Return the turtle's y coordinate
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1722 ---
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1723 No arguments.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1724
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1725 Example (for a Turtle instance named turtle):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1726 >>> reset()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1727 >>> turtle.left(60)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1728 >>> turtle.forward(100)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1729 >>> print turtle.ycor()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1730 86.6025403784
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 31515
diff changeset
1731 """
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1732 return self._position[1]
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1733
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1734
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1735 def goto(self, x, y=None):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1736 """Move turtle to an absolute position.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1737
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1738 Aliases: setpos | setposition | goto:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1739
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1740 Arguments:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1741 x -- a number or a pair/vector of numbers
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1742 y -- a number None
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1743
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1744 call: goto(x, y) # two coordinates
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1745 --or: goto((x, y)) # a pair (tuple) of coordinates
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1746 --or: goto(vec) # e.g. as returned by pos()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1747
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1748 Move turtle to an absolute position. If the pen is down,
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1749 a line will be drawn. The turtle's orientation does not change.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1750
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1751 Example (for a Turtle instance named turtle):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1752 >>> tp = turtle.pos()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1753 >>> tp
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1754 (0.00, 0.00)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1755 >>> turtle.setpos(60,30)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1756 >>> turtle.pos()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1757 (60.00,30.00)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1758 >>> turtle.setpos((20,80))
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1759 >>> turtle.pos()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1760 (20.00,80.00)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1761 >>> turtle.setpos(tp)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1762 >>> turtle.pos()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1763 (0.00,0.00)
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 31515
diff changeset
1764 """
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1765 if y is None:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1766 self._goto(Vec2D(*x))
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1767 else:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1768 self._goto(Vec2D(x, y))
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1769
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1770 def home(self):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1771 """Move turtle to the origin - coordinates (0,0).
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1772
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1773 No arguments.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1774
51765
37e4df9fdaa6 Merged revisions 69821 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 51763
diff changeset
1775 Move turtle to the origin - coordinates (0,0) and set its
37e4df9fdaa6 Merged revisions 69821 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 51763
diff changeset
1776 heading to its start-orientation (which depends on mode).
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1777
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1778 Example (for a Turtle instance named turtle):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1779 >>> turtle.home()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1780 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1781 self.goto(0, 0)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1782 self.setheading(0)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1783
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1784 def setx(self, x):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1785 """Set the turtle's first coordinate to x
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1786
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1787 Argument:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1788 x -- a number (integer or float)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1789
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1790 Set the turtle's first coordinate to x, leave second coordinate
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1791 unchanged.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1792
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1793 Example (for a Turtle instance named turtle):
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 31515
diff changeset
1794 >>> turtle.position()
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1795 (0.00, 240.00)
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 31515
diff changeset
1796 >>> turtle.setx(10)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 31515
diff changeset
1797 >>> turtle.position()
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1798 (10.00, 240.00)
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 31515
diff changeset
1799 """
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1800 self._goto(Vec2D(x, self._position[1]))
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1801
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1802 def sety(self, y):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1803 """Set the turtle's second coordinate to y
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1804
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1805 Argument:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1806 y -- a number (integer or float)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1807
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1808 Set the turtle's first coordinate to x, second coordinate remains
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1809 unchanged.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1810
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1811 Example (for a Turtle instance named turtle):
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 31515
diff changeset
1812 >>> turtle.position()
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1813 (0.00, 40.00)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1814 >>> turtle.sety(-10)
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 31515
diff changeset
1815 >>> turtle.position()
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1816 (0.00, -10.00)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1817 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1818 self._goto(Vec2D(self._position[0], y))
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1819
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1820 def distance(self, x, y=None):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1821 """Return the distance from the turtle to (x,y) in turtle step units.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1822
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1823 Arguments:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1824 x -- a number or a pair/vector of numbers or a turtle instance
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1825 y -- a number None None
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1826
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1827 call: distance(x, y) # two coordinates
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1828 --or: distance((x, y)) # a pair (tuple) of coordinates
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1829 --or: distance(vec) # e.g. as returned by pos()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1830 --or: distance(mypen) # where mypen is another turtle
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1831
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1832 Example (for a Turtle instance named turtle):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1833 >>> turtle.pos()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1834 (0.00, 0.00)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1835 >>> turtle.distance(30,40)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1836 50.0
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1837 >>> pen = Turtle()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1838 >>> pen.forward(77)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1839 >>> turtle.distance(pen)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1840 77.0
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 31515
diff changeset
1841 """
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1842 if y is not None:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1843 pos = Vec2D(x, y)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1844 if isinstance(x, Vec2D):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1845 pos = x
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1846 elif isinstance(x, tuple):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1847 pos = Vec2D(*x)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1848 elif isinstance(x, TNavigator):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1849 pos = x._position
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1850 return abs(pos - self._position)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1851
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1852 def towards(self, x, y=None):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1853 """Return the angle of the line from the turtle's position to (x, y).
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1854
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1855 Arguments:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1856 x -- a number or a pair/vector of numbers or a turtle instance
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1857 y -- a number None None
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1858
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1859 call: distance(x, y) # two coordinates
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1860 --or: distance((x, y)) # a pair (tuple) of coordinates
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1861 --or: distance(vec) # e.g. as returned by pos()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1862 --or: distance(mypen) # where mypen is another turtle
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1863
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1864 Return the angle, between the line from turtle-position to position
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1865 specified by x, y and the turtle's start orientation. (Depends on
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1866 modes - "standard" or "logo")
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1867
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1868 Example (for a Turtle instance named turtle):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1869 >>> turtle.pos()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1870 (10.00, 10.00)
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 31515
diff changeset
1871 >>> turtle.towards(0,0)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 31515
diff changeset
1872 225.0
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 31515
diff changeset
1873 """
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1874 if y is not None:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1875 pos = Vec2D(x, y)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1876 if isinstance(x, Vec2D):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1877 pos = x
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1878 elif isinstance(x, tuple):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1879 pos = Vec2D(*x)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1880 elif isinstance(x, TNavigator):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1881 pos = x._position
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1882 x, y = pos - self._position
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1883 result = round(math.atan2(y, x)*180.0/math.pi, 10) % 360.0
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1884 result /= self._degreesPerAU
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1885 return (self._angleOffset + self._angleOrient*result) % self._fullcircle
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1886
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1887 def heading(self):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1888 """ Return the turtle's current heading.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1889
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1890 No arguments.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1891
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1892 Example (for a Turtle instance named turtle):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1893 >>> turtle.left(67)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1894 >>> turtle.heading()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1895 67.0
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1896 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1897 x, y = self._orient
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1898 result = round(math.atan2(y, x)*180.0/math.pi, 10) % 360.0
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1899 result /= self._degreesPerAU
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1900 return (self._angleOffset + self._angleOrient*result) % self._fullcircle
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1901
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1902 def setheading(self, to_angle):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1903 """Set the orientation of the turtle to to_angle.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1904
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1905 Aliases: setheading | seth
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1906
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1907 Argument:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1908 to_angle -- a number (integer or float)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1909
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1910 Set the orientation of the turtle to to_angle.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1911 Here are some common directions in degrees:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1912
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1913 standard - mode: logo-mode:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1914 -------------------|--------------------
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1915 0 - east 0 - north
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1916 90 - north 90 - east
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1917 180 - west 180 - south
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1918 270 - south 270 - west
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1919
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1920 Example (for a Turtle instance named turtle):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1921 >>> turtle.setheading(90)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1922 >>> turtle.heading()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1923 90
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1924 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1925 angle = (to_angle - self.heading())*self._angleOrient
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1926 full = self._fullcircle
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1927 angle = (angle+full/2.)%full - full/2.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1928 self._rotate(angle)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1929
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1930 def circle(self, radius, extent = None, steps = None):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1931 """ Draw a circle with given radius.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1932
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1933 Arguments:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1934 radius -- a number
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1935 extent (optional) -- a number
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1936 steps (optional) -- an integer
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1937
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1938 Draw a circle with given radius. The center is radius units left
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1939 of the turtle; extent - an angle - determines which part of the
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1940 circle is drawn. If extent is not given, draw the entire circle.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1941 If extent is not a full circle, one endpoint of the arc is the
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1942 current pen position. Draw the arc in counterclockwise direction
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1943 if radius is positive, otherwise in clockwise direction. Finally
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1944 the direction of the turtle is changed by the amount of extent.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1945
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1946 As the circle is approximated by an inscribed regular polygon,
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1947 steps determines the number of steps to use. If not given,
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1948 it will be calculated automatically. Maybe used to draw regular
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1949 polygons.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1950
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1951 call: circle(radius) # full circle
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1952 --or: circle(radius, extent) # arc
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1953 --or: circle(radius, extent, steps)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1954 --or: circle(radius, steps=6) # 6-sided polygon
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1955
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1956 Example (for a Turtle instance named turtle):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1957 >>> turtle.circle(50)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1958 >>> turtle.circle(120, 180) # semicircle
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1959 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1960 if self.undobuffer:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1961 self.undobuffer.push(["seq"])
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1962 self.undobuffer.cumulate = True
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1963 speed = self.speed()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1964 if extent is None:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1965 extent = self._fullcircle
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1966 if steps is None:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1967 frac = abs(extent)/self._fullcircle
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1968 steps = 1+int(min(11+abs(radius)/6.0, 59.0)*frac)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1969 w = 1.0 * extent / steps
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1970 w2 = 0.5 * w
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1971 l = 2.0 * radius * math.sin(w2*math.pi/180.0*self._degreesPerAU)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1972 if radius < 0:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1973 l, w, w2 = -l, -w, -w2
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1974 tr = self._tracer()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1975 dl = self._delay()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1976 if speed == 0:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1977 self._tracer(0, 0)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1978 else:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1979 self.speed(0)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1980 self._rotate(w2)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1981 for i in range(steps):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1982 self.speed(speed)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1983 self._go(l)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1984 self.speed(0)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1985 self._rotate(w)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1986 self._rotate(-w2)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1987 if speed == 0:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1988 self._tracer(tr, dl)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1989 self.speed(speed)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1990 if self.undobuffer:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1991 self.undobuffer.cumulate = False
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1992
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1993 ## three dummy methods to be implemented by child class:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1994
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1995 def speed(self, s=0):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1996 """dummy method - to be overwritten by child class"""
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1997 def _tracer(self, a=None, b=None):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1998 """dummy method - to be overwritten by child class"""
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1999 def _delay(self, n=None):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2000 """dummy method - to be overwritten by child class"""
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2001
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2002 fd = forward
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2003 bk = back
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2004 backward = back
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2005 rt = right
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2006 lt = left
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2007 position = pos
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2008 setpos = goto
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2009 setposition = goto
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2010 seth = setheading
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2011
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2012
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2013 class TPen(object):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2014 """Drawing part of the RawTurtle.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2015 Implements drawing properties.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2016 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2017 def __init__(self, resizemode=_CFG["resizemode"]):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2018 self._resizemode = resizemode # or "user" or "noresize"
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2019 self.undobuffer = None
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2020 TPen._reset(self)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2021
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2022 def _reset(self, pencolor=_CFG["pencolor"],
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2023 fillcolor=_CFG["fillcolor"]):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2024 self._pensize = 1
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2025 self._shown = True
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2026 self._pencolor = pencolor
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2027 self._fillcolor = fillcolor
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2028 self._drawing = True
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2029 self._speed = 3
53772
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2030 self._stretchfactor = (1., 1.)
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2031 self._shearfactor = 0.
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2032 self._tilt = 0.
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2033 self._shapetrafo = (1., 0., 0., 1.)
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2034 self._outlinewidth = 1
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2035
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2036 def resizemode(self, rmode=None):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2037 """Set resizemode to one of the values: "auto", "user", "noresize".
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2038
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2039 (Optional) Argument:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2040 rmode -- one of the strings "auto", "user", "noresize"
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2041
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2042 Different resizemodes have the following effects:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2043 - "auto" adapts the appearance of the turtle
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2044 corresponding to the value of pensize.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2045 - "user" adapts the appearance of the turtle according to the
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2046 values of stretchfactor and outlinewidth (outline),
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2047 which are set by shapesize()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2048 - "noresize" no adaption of the turtle's appearance takes place.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2049 If no argument is given, return current resizemode.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2050 resizemode("user") is called by a call of shapesize with arguments.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2051
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2052
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2053 Examples (for a Turtle instance named turtle):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2054 >>> turtle.resizemode("noresize")
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2055 >>> turtle.resizemode()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2056 'noresize'
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2057 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2058 if rmode is None:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2059 return self._resizemode
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2060 rmode = rmode.lower()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2061 if rmode in ["auto", "user", "noresize"]:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2062 self.pen(resizemode=rmode)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2063
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2064 def pensize(self, width=None):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2065 """Set or return the line thickness.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2066
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2067 Aliases: pensize | width
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2068
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2069 Argument:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2070 width -- positive number
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2071
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2072 Set the line thickness to width or return it. If resizemode is set
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2073 to "auto" and turtleshape is a polygon, that polygon is drawn with
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2074 the same line thickness. If no argument is given, current pensize
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2075 is returned.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2076
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2077 Example (for a Turtle instance named turtle):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2078 >>> turtle.pensize()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2079 1
73816
cc559e1e3bd8 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72985
diff changeset
2080 >>> turtle.pensize(10) # from here on lines of width 10 are drawn
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2081 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2082 if width is None:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2083 return self._pensize
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2084 self.pen(pensize=width)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2085
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2086
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2087 def penup(self):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2088 """Pull the pen up -- no drawing when moving.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2089
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2090 Aliases: penup | pu | up
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2091
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2092 No argument
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2093
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2094 Example (for a Turtle instance named turtle):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2095 >>> turtle.penup()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2096 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2097 if not self._drawing:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2098 return
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2099 self.pen(pendown=False)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2100
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2101 def pendown(self):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2102 """Pull the pen down -- drawing when moving.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2103
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2104 Aliases: pendown | pd | down
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2105
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2106 No argument.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2107
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2108 Example (for a Turtle instance named turtle):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2109 >>> turtle.pendown()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2110 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2111 if self._drawing:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2112 return
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2113 self.pen(pendown=True)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2114
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2115 def isdown(self):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2116 """Return True if pen is down, False if it's up.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2117
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2118 No argument.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2119
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2120 Example (for a Turtle instance named turtle):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2121 >>> turtle.penup()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2122 >>> turtle.isdown()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2123 False
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2124 >>> turtle.pendown()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2125 >>> turtle.isdown()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2126 True
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2127 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2128 return self._drawing
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2129
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2130 def speed(self, speed=None):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2131 """ Return or set the turtle's speed.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2132
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2133 Optional argument:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2134 speed -- an integer in the range 0..10 or a speedstring (see below)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2135
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2136 Set the turtle's speed to an integer value in the range 0 .. 10.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2137 If no argument is given: return current speed.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2138
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2139 If input is a number greater than 10 or smaller than 0.5,
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2140 speed is set to 0.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2141 Speedstrings are mapped to speedvalues in the following way:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2142 'fastest' : 0
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2143 'fast' : 10
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2144 'normal' : 6
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2145 'slow' : 3
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2146 'slowest' : 1
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2147 speeds from 1 to 10 enforce increasingly faster animation of
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2148 line drawing and turtle turning.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2149
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2150 Attention:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2151 speed = 0 : *no* animation takes place. forward/back makes turtle jump
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2152 and likewise left/right make the turtle turn instantly.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2153
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2154 Example (for a Turtle instance named turtle):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2155 >>> turtle.speed(3)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2156 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2157 speeds = {'fastest':0, 'fast':10, 'normal':6, 'slow':3, 'slowest':1 }
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2158 if speed is None:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2159 return self._speed
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2160 if speed in speeds:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2161 speed = speeds[speed]
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2162 elif 0.5 < speed < 10.5:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2163 speed = int(round(speed))
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 31515
diff changeset
2164 else:
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2165 speed = 0
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2166 self.pen(speed=speed)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2167
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2168 def color(self, *args):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2169 """Return or set the pencolor and fillcolor.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2170
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2171 Arguments:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2172 Several input formats are allowed.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2173 They use 0, 1, 2, or 3 arguments as follows:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2174
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2175 color()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2176 Return the current pencolor and the current fillcolor
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2177 as a pair of color specification strings as are returned
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2178 by pencolor and fillcolor.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2179 color(colorstring), color((r,g,b)), color(r,g,b)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2180 inputs as in pencolor, set both, fillcolor and pencolor,
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2181 to the given value.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2182 color(colorstring1, colorstring2),
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2183 color((r1,g1,b1), (r2,g2,b2))
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2184 equivalent to pencolor(colorstring1) and fillcolor(colorstring2)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2185 and analogously, if the other input format is used.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2186
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2187 If turtleshape is a polygon, outline and interior of that polygon
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2188 is drawn with the newly set colors.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2189 For mor info see: pencolor, fillcolor
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2190
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2191 Example (for a Turtle instance named turtle):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2192 >>> turtle.color('red', 'green')
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2193 >>> turtle.color()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2194 ('red', 'green')
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2195 >>> colormode(255)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2196 >>> color((40, 80, 120), (160, 200, 240))
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2197 >>> color()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2198 ('#285078', '#a0c8f0')
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2199 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2200 if args:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2201 l = len(args)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2202 if l == 1:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2203 pcolor = fcolor = args[0]
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2204 elif l == 2:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2205 pcolor, fcolor = args
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2206 elif l == 3:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2207 pcolor = fcolor = args
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2208 pcolor = self._colorstr(pcolor)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2209 fcolor = self._colorstr(fcolor)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2210 self.pen(pencolor=pcolor, fillcolor=fcolor)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2211 else:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2212 return self._color(self._pencolor), self._color(self._fillcolor)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2213
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2214 def pencolor(self, *args):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2215 """ Return or set the pencolor.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2216
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2217 Arguments:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2218 Four input formats are allowed:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2219 - pencolor()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2220 Return the current pencolor as color specification string,
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2221 possibly in hex-number format (see example).
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2222 May be used as input to another color/pencolor/fillcolor call.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2223 - pencolor(colorstring)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2224 s is a Tk color specification string, such as "red" or "yellow"
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2225 - pencolor((r, g, b))
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2226 *a tuple* of r, g, and b, which represent, an RGB color,
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2227 and each of r, g, and b are in the range 0..colormode,
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2228 where colormode is either 1.0 or 255
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2229 - pencolor(r, g, b)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2230 r, g, and b represent an RGB color, and each of r, g, and b
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2231 are in the range 0..colormode
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2232
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2233 If turtleshape is a polygon, the outline of that polygon is drawn
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2234 with the newly set pencolor.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2235
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2236 Example (for a Turtle instance named turtle):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2237 >>> turtle.pencolor('brown')
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2238 >>> tup = (0.2, 0.8, 0.55)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2239 >>> turtle.pencolor(tup)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2240 >>> turtle.pencolor()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2241 '#33cc8c'
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2242 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2243 if args:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2244 color = self._colorstr(args)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2245 if color == self._pencolor:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2246 return
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2247 self.pen(pencolor=color)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2248 else:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2249 return self._color(self._pencolor)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2250
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2251 def fillcolor(self, *args):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2252 """ Return or set the fillcolor.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2253
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2254 Arguments:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2255 Four input formats are allowed:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2256 - fillcolor()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2257 Return the current fillcolor as color specification string,
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2258 possibly in hex-number format (see example).
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2259 May be used as input to another color/pencolor/fillcolor call.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2260 - fillcolor(colorstring)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2261 s is a Tk color specification string, such as "red" or "yellow"
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2262 - fillcolor((r, g, b))
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2263 *a tuple* of r, g, and b, which represent, an RGB color,
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2264 and each of r, g, and b are in the range 0..colormode,
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2265 where colormode is either 1.0 or 255
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2266 - fillcolor(r, g, b)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2267 r, g, and b represent an RGB color, and each of r, g, and b
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2268 are in the range 0..colormode
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2269
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2270 If turtleshape is a polygon, the interior of that polygon is drawn
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2271 with the newly set fillcolor.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2272
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2273 Example (for a Turtle instance named turtle):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2274 >>> turtle.fillcolor('violet')
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2275 >>> col = turtle.pencolor()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2276 >>> turtle.fillcolor(col)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2277 >>> turtle.fillcolor(0, .5, 0)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2278 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2279 if args:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2280 color = self._colorstr(args)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2281 if color == self._fillcolor:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2282 return
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2283 self.pen(fillcolor=color)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2284 else:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2285 return self._color(self._fillcolor)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2286
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2287 def showturtle(self):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2288 """Makes the turtle visible.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2289
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2290 Aliases: showturtle | st
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2291
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2292 No argument.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2293
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2294 Example (for a Turtle instance named turtle):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2295 >>> turtle.hideturtle()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2296 >>> turtle.showturtle()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2297 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2298 self.pen(shown=True)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2299
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2300 def hideturtle(self):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2301 """Makes the turtle invisible.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2302
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2303 Aliases: hideturtle | ht
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2304
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2305 No argument.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2306
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2307 It's a good idea to do this while you're in the
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2308 middle of a complicated drawing, because hiding
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2309 the turtle speeds up the drawing observably.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2310
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2311 Example (for a Turtle instance named turtle):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2312 >>> turtle.hideturtle()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2313 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2314 self.pen(shown=False)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2315
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2316 def isvisible(self):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2317 """Return True if the Turtle is shown, False if it's hidden.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2318
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2319 No argument.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2320
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2321 Example (for a Turtle instance named turtle):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2322 >>> turtle.hideturtle()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2323 >>> print turtle.isvisible():
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2324 False
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2325 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2326 return self._shown
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2327
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2328 def pen(self, pen=None, **pendict):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2329 """Return or set the pen's attributes.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2330
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2331 Arguments:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2332 pen -- a dictionary with some or all of the below listed keys.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2333 **pendict -- one or more keyword-arguments with the below
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2334 listed keys as keywords.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2335
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2336 Return or set the pen's attributes in a 'pen-dictionary'
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2337 with the following key/value pairs:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2338 "shown" : True/False
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2339 "pendown" : True/False
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2340 "pencolor" : color-string or color-tuple
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2341 "fillcolor" : color-string or color-tuple
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2342 "pensize" : positive number
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2343 "speed" : number in range 0..10
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2344 "resizemode" : "auto" or "user" or "noresize"
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2345 "stretchfactor": (positive number, positive number)
53772
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2346 "shearfactor": number
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2347 "outline" : positive number
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2348 "tilt" : number
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2349
51765
37e4df9fdaa6 Merged revisions 69821 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 51763
diff changeset
2350 This dictionary can be used as argument for a subsequent
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2351 pen()-call to restore the former pen-state. Moreover one
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2352 or more of these attributes can be provided as keyword-arguments.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2353 This can be used to set several pen attributes in one statement.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2354
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2355
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2356 Examples (for a Turtle instance named turtle):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2357 >>> turtle.pen(fillcolor="black", pencolor="red", pensize=10)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2358 >>> turtle.pen()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2359 {'pensize': 10, 'shown': True, 'resizemode': 'auto', 'outline': 1,
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2360 'pencolor': 'red', 'pendown': True, 'fillcolor': 'black',
53772
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2361 'stretchfactor': (1,1), 'speed': 3, 'shearfactor': 0.0}
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2362 >>> penstate=turtle.pen()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2363 >>> turtle.color("yellow","")
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2364 >>> turtle.penup()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2365 >>> turtle.pen()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2366 {'pensize': 10, 'shown': True, 'resizemode': 'auto', 'outline': 1,
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2367 'pencolor': 'yellow', 'pendown': False, 'fillcolor': '',
53772
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2368 'stretchfactor': (1,1), 'speed': 3, 'shearfactor': 0.0}
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2369 >>> p.pen(penstate, fillcolor="green")
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2370 >>> p.pen()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2371 {'pensize': 10, 'shown': True, 'resizemode': 'auto', 'outline': 1,
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2372 'pencolor': 'red', 'pendown': True, 'fillcolor': 'green',
53772
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2373 'stretchfactor': (1,1), 'speed': 3, 'shearfactor': 0.0}
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2374 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2375 _pd = {"shown" : self._shown,
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2376 "pendown" : self._drawing,
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2377 "pencolor" : self._pencolor,
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2378 "fillcolor" : self._fillcolor,
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2379 "pensize" : self._pensize,
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2380 "speed" : self._speed,
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2381 "resizemode" : self._resizemode,
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2382 "stretchfactor" : self._stretchfactor,
53772
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2383 "shearfactor" : self._shearfactor,
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2384 "outline" : self._outlinewidth,
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2385 "tilt" : self._tilt
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2386 }
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2387
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2388 if not (pen or pendict):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2389 return _pd
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2390
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2391 if isinstance(pen, dict):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2392 p = pen
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2393 else:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2394 p = {}
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2395 p.update(pendict)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2396
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2397 _p_buf = {}
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2398 for key in p:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2399 _p_buf[key] = _pd[key]
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2400
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2401 if self.undobuffer:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2402 self.undobuffer.push(("pen", _p_buf))
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2403
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2404 newLine = False
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2405 if "pendown" in p:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2406 if self._drawing != p["pendown"]:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2407 newLine = True
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2408 if "pencolor" in p:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2409 if isinstance(p["pencolor"], tuple):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2410 p["pencolor"] = self._colorstr((p["pencolor"],))
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2411 if self._pencolor != p["pencolor"]:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2412 newLine = True
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2413 if "pensize" in p:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2414 if self._pensize != p["pensize"]:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2415 newLine = True
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2416 if newLine:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2417 self._newLine()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2418 if "pendown" in p:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2419 self._drawing = p["pendown"]
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2420 if "pencolor" in p:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2421 self._pencolor = p["pencolor"]
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2422 if "pensize" in p:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2423 self._pensize = p["pensize"]
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2424 if "fillcolor" in p:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2425 if isinstance(p["fillcolor"], tuple):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2426 p["fillcolor"] = self._colorstr((p["fillcolor"],))
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2427 self._fillcolor = p["fillcolor"]
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2428 if "speed" in p:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2429 self._speed = p["speed"]
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2430 if "resizemode" in p:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2431 self._resizemode = p["resizemode"]
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2432 if "stretchfactor" in p:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2433 sf = p["stretchfactor"]
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2434 if isinstance(sf, (int, float)):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2435 sf = (sf, sf)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2436 self._stretchfactor = sf
53772
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2437 if "shearfactor" in p:
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2438 self._shearfactor = p["shearfactor"]
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2439 if "outline" in p:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2440 self._outlinewidth = p["outline"]
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2441 if "shown" in p:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2442 self._shown = p["shown"]
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2443 if "tilt" in p:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2444 self._tilt = p["tilt"]
53772
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2445 if "stretchfactor" in p or "tilt" in p or "shearfactor" in p:
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2446 scx, scy = self._stretchfactor
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2447 shf = self._shearfactor
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2448 sa, ca = math.sin(self._tilt), math.cos(self._tilt)
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2449 self._shapetrafo = ( scx*ca, scy*(shf*ca + sa),
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2450 -scx*sa, scy*(ca - shf*sa))
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2451 self._update()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2452
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2453 ## three dummy methods to be implemented by child class:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2454
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2455 def _newLine(self, usePos = True):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2456 """dummy method - to be overwritten by child class"""
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2457 def _update(self, count=True, forced=False):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2458 """dummy method - to be overwritten by child class"""
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2459 def _color(self, args):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2460 """dummy method - to be overwritten by child class"""
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2461 def _colorstr(self, args):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2462 """dummy method - to be overwritten by child class"""
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2463
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2464 width = pensize
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2465 up = penup
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2466 pu = penup
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2467 pd = pendown
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2468 down = pendown
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2469 st = showturtle
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2470 ht = hideturtle
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2471
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2472
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2473 class _TurtleImage(object):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2474 """Helper class: Datatype to store Turtle attributes
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2475 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2476
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2477 def __init__(self, screen, shapeIndex):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2478 self.screen = screen
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2479 self._type = None
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2480 self._setshape(shapeIndex)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2481
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2482 def _setshape(self, shapeIndex):
53772
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2483 screen = self.screen
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2484 self.shapeIndex = shapeIndex
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2485 if self._type == "polygon" == screen._shapes[shapeIndex]._type:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2486 return
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2487 if self._type == "image" == screen._shapes[shapeIndex]._type:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2488 return
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2489 if self._type in ["image", "polygon"]:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2490 screen._delete(self._item)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2491 elif self._type == "compound":
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2492 for item in self._item:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2493 screen._delete(item)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2494 self._type = screen._shapes[shapeIndex]._type
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2495 if self._type == "polygon":
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2496 self._item = screen._createpoly()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2497 elif self._type == "image":
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2498 self._item = screen._createimage(screen._shapes["blank"]._data)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2499 elif self._type == "compound":
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2500 self._item = [screen._createpoly() for item in
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2501 screen._shapes[shapeIndex]._data]
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2502
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2503
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2504 class RawTurtle(TPen, TNavigator):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2505 """Animation part of the RawTurtle.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2506 Puts RawTurtle upon a TurtleScreen and provides tools for
51765
37e4df9fdaa6 Merged revisions 69821 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 51763
diff changeset
2507 its animation.
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2508 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2509 screens = []
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2510
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2511 def __init__(self, canvas=None,
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2512 shape=_CFG["shape"],
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2513 undobuffersize=_CFG["undobuffersize"],
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2514 visible=_CFG["visible"]):
49345
ab34a647e22f Merged revisions 66686 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 49249
diff changeset
2515 if isinstance(canvas, _Screen):
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2516 self.screen = canvas
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2517 elif isinstance(canvas, TurtleScreen):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2518 if canvas not in RawTurtle.screens:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2519 RawTurtle.screens.append(canvas)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2520 self.screen = canvas
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2521 elif isinstance(canvas, (ScrolledCanvas, Canvas)):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2522 for screen in RawTurtle.screens:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2523 if screen.cv == canvas:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2524 self.screen = screen
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2525 break
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 31515
diff changeset
2526 else:
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2527 self.screen = TurtleScreen(canvas)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2528 RawTurtle.screens.append(self.screen)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2529 else:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2530 raise TurtleGraphicsError("bad cavas argument %s" % canvas)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2531
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2532 screen = self.screen
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2533 TNavigator.__init__(self, screen.mode())
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2534 TPen.__init__(self)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2535 screen._turtles.append(self)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2536 self.drawingLineItem = screen._createline()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2537 self.turtle = _TurtleImage(screen, shape)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2538 self._poly = None
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2539 self._creatingPoly = False
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2540 self._fillitem = self._fillpath = None
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2541 self._shown = visible
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2542 self._hidden_from_screen = False
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2543 self.currentLineItem = screen._createline()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2544 self.currentLine = [self._position]
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2545 self.items = [self.currentLineItem]
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2546 self.stampItems = []
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2547 self._undobuffersize = undobuffersize
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2548 self.undobuffer = Tbuffer(undobuffersize)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2549 self._update()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2550
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2551 def reset(self):
51765
37e4df9fdaa6 Merged revisions 69821 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 51763
diff changeset
2552 """Delete the turtle's drawings and restore its default values.
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2553
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2554 No argument.
73816
cc559e1e3bd8 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72985
diff changeset
2555
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2556 Delete the turtle's drawings from the screen, re-center the turtle
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2557 and set variables to the default values.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2558
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2559 Example (for a Turtle instance named turtle):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2560 >>> turtle.position()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2561 (0.00,-22.00)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2562 >>> turtle.heading()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2563 100.0
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2564 >>> turtle.reset()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2565 >>> turtle.position()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2566 (0.00,0.00)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2567 >>> turtle.heading()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2568 0.0
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2569 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2570 TNavigator.reset(self)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2571 TPen._reset(self)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2572 self._clear()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2573 self._drawturtle()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2574 self._update()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2575
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2576 def setundobuffer(self, size):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2577 """Set or disable undobuffer.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2578
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2579 Argument:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2580 size -- an integer or None
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2581
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2582 If size is an integer an empty undobuffer of given size is installed.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2583 Size gives the maximum number of turtle-actions that can be undone
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2584 by the undo() function.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2585 If size is None, no undobuffer is present.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2586
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2587 Example (for a Turtle instance named turtle):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2588 >>> turtle.setundobuffer(42)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2589 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2590 if size is None:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2591 self.undobuffer = None
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2592 else:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2593 self.undobuffer = Tbuffer(size)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2594
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2595 def undobufferentries(self):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2596 """Return count of entries in the undobuffer.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2597
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2598 No argument.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2599
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2600 Example (for a Turtle instance named turtle):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2601 >>> while undobufferentries():
73816
cc559e1e3bd8 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72985
diff changeset
2602 ... undo()
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2603 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2604 if self.undobuffer is None:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2605 return 0
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2606 return self.undobuffer.nr_of_items()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2607
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2608 def _clear(self):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2609 """Delete all of pen's drawings"""
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2610 self._fillitem = self._fillpath = None
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2611 for item in self.items:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2612 self.screen._delete(item)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2613 self.currentLineItem = self.screen._createline()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2614 self.currentLine = []
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2615 if self._drawing:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2616 self.currentLine.append(self._position)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2617 self.items = [self.currentLineItem]
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2618 self.clearstamps()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2619 self.setundobuffer(self._undobuffersize)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2620
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2621
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2622 def clear(self):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2623 """Delete the turtle's drawings from the screen. Do not move turtle.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2624
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2625 No arguments.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2626
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2627 Delete the turtle's drawings from the screen. Do not move turtle.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2628 State and position of the turtle as well as drawings of other
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2629 turtles are not affected.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2630
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2631 Examples (for a Turtle instance named turtle):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2632 >>> turtle.clear()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2633 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2634 self._clear()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2635 self._update()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2636
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2637 def _update_data(self):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2638 self.screen._incrementudc()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2639 if self.screen._updatecounter != 0:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2640 return
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2641 if len(self.currentLine)>1:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2642 self.screen._drawline(self.currentLineItem, self.currentLine,
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2643 self._pencolor, self._pensize)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2644
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2645 def _update(self):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2646 """Perform a Turtle-data update.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2647 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2648 screen = self.screen
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2649 if screen._tracing == 0:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2650 return
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2651 elif screen._tracing == 1:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2652 self._update_data()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2653 self._drawturtle()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2654 screen._update() # TurtleScreenBase
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2655 screen._delay(screen._delayvalue) # TurtleScreenBase
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2656 else:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2657 self._update_data()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2658 if screen._updatecounter == 0:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2659 for t in screen.turtles():
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2660 t._drawturtle()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2661 screen._update()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2662
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2663 def _tracer(self, flag=None, delay=None):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2664 """Turns turtle animation on/off and set delay for update drawings.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2665
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2666 Optional arguments:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2667 n -- nonnegative integer
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2668 delay -- nonnegative integer
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2669
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2670 If n is given, only each n-th regular screen update is really performed.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2671 (Can be used to accelerate the drawing of complex graphics.)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2672 Second arguments sets delay value (see RawTurtle.delay())
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2673
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2674 Example (for a Turtle instance named turtle):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2675 >>> turtle.tracer(8, 25)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2676 >>> dist = 2
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2677 >>> for i in range(200):
73816
cc559e1e3bd8 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72985
diff changeset
2678 ... turtle.fd(dist)
cc559e1e3bd8 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72985
diff changeset
2679 ... turtle.rt(90)
cc559e1e3bd8 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72985
diff changeset
2680 ... dist += 2
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2681 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2682 return self.screen.tracer(flag, delay)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2683
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2684 def _color(self, args):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2685 return self.screen._color(args)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2686
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2687 def _colorstr(self, args):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2688 return self.screen._colorstr(args)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2689
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2690 def _cc(self, args):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2691 """Convert colortriples to hexstrings.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2692 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2693 if isinstance(args, str):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2694 return args
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2695 try:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2696 r, g, b = args
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2697 except:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2698 raise TurtleGraphicsError("bad color arguments: %s" % str(args))
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2699 if self.screen._colormode == 1.0:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2700 r, g, b = [round(255.0*x) for x in (r, g, b)]
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2701 if not ((0 <= r <= 255) and (0 <= g <= 255) and (0 <= b <= 255)):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2702 raise TurtleGraphicsError("bad color sequence: %s" % str(args))
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2703 return "#%02x%02x%02x" % (r, g, b)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2704
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2705 def clone(self):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2706 """Create and return a clone of the turtle.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2707
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2708 No argument.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2709
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2710 Create and return a clone of the turtle with same position, heading
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2711 and turtle properties.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2712
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2713 Example (for a Turtle instance named mick):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2714 mick = Turtle()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2715 joe = mick.clone()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2716 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2717 screen = self.screen
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2718 self._newLine(self._drawing)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2719
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2720 turtle = self.turtle
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2721 self.screen = None
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2722 self.turtle = None # too make self deepcopy-able
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2723
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2724 q = deepcopy(self)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2725
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2726 self.screen = screen
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2727 self.turtle = turtle
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2728
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2729 q.screen = screen
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2730 q.turtle = _TurtleImage(screen, self.turtle.shapeIndex)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2731
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2732 screen._turtles.append(q)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2733 ttype = screen._shapes[self.turtle.shapeIndex]._type
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2734 if ttype == "polygon":
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2735 q.turtle._item = screen._createpoly()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2736 elif ttype == "image":
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2737 q.turtle._item = screen._createimage(screen._shapes["blank"]._data)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2738 elif ttype == "compound":
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2739 q.turtle._item = [screen._createpoly() for item in
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2740 screen._shapes[self.turtle.shapeIndex]._data]
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2741 q.currentLineItem = screen._createline()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2742 q._update()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2743 return q
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2744
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2745 def shape(self, name=None):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2746 """Set turtle shape to shape with given name / return current shapename.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2747
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2748 Optional argument:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2749 name -- a string, which is a valid shapename
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2750
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2751 Set turtle shape to shape with given name or, if name is not given,
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2752 return name of current shape.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2753 Shape with name must exist in the TurtleScreen's shape dictionary.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2754 Initially there are the following polygon shapes:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2755 'arrow', 'turtle', 'circle', 'square', 'triangle', 'classic'.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2756 To learn about how to deal with shapes see Screen-method register_shape.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2757
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2758 Example (for a Turtle instance named turtle):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2759 >>> turtle.shape()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2760 'arrow'
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2761 >>> turtle.shape("turtle")
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2762 >>> turtle.shape()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2763 'turtle'
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2764 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2765 if name is None:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2766 return self.turtle.shapeIndex
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2767 if not name in self.screen.getshapes():
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2768 raise TurtleGraphicsError("There is no shape named %s" % name)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2769 self.turtle._setshape(name)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2770 self._update()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2771
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2772 def shapesize(self, stretch_wid=None, stretch_len=None, outline=None):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2773 """Set/return turtle's stretchfactors/outline. Set resizemode to "user".
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2774
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2775 Optinonal arguments:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2776 stretch_wid : positive number
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2777 stretch_len : positive number
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2778 outline : positive number
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2779
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2780 Return or set the pen's attributes x/y-stretchfactors and/or outline.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2781 Set resizemode to "user".
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2782 If and only if resizemode is set to "user", the turtle will be displayed
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2783 stretched according to its stretchfactors:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2784 stretch_wid is stretchfactor perpendicular to orientation
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2785 stretch_len is stretchfactor in direction of turtles orientation.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2786 outline determines the width of the shapes's outline.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2787
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2788 Examples (for a Turtle instance named turtle):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2789 >>> turtle.resizemode("user")
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2790 >>> turtle.shapesize(5, 5, 12)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2791 >>> turtle.shapesize(outline=8)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2792 """
60058
40f787b151ac Merged revisions 79297,79310,79382,79425-79427,79450 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58218
diff changeset
2793 if stretch_wid is stretch_len is outline is None:
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2794 stretch_wid, stretch_len = self._stretchfactor
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2795 return stretch_wid, stretch_len, self._outlinewidth
53772
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2796 if stretch_wid == 0 or stretch_len == 0:
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2797 raise TurtleGraphicsError("stretch_wid/stretch_len must not be zero")
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2798 if stretch_wid is not None:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2799 if stretch_len is None:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2800 stretchfactor = stretch_wid, stretch_wid
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2801 else:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2802 stretchfactor = stretch_wid, stretch_len
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2803 elif stretch_len is not None:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2804 stretchfactor = self._stretchfactor[0], stretch_len
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2805 else:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2806 stretchfactor = self._stretchfactor
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2807 if outline is None:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2808 outline = self._outlinewidth
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2809 self.pen(resizemode="user",
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2810 stretchfactor=stretchfactor, outline=outline)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2811
53772
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2812 def shearfactor(self, shear=None):
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2813 """Set or return the current shearfactor.
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2814
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2815 Optional argument: shear -- number, tangent of the shear angle
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2816
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2817 Shear the turtleshape according to the given shearfactor shear,
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2818 which is the tangent of the shear angle. DO NOT change the
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2819 turtle's heading (direction of movement).
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2820 If shear is not given: return the current shearfactor, i. e. the
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2821 tangent of the shear angle, by which lines parallel to the
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2822 heading of the turtle are sheared.
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2823
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2824 Examples (for a Turtle instance named turtle):
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2825 >>> turtle.shape("circle")
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2826 >>> turtle.shapesize(5,2)
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2827 >>> turtle.shearfactor(0.5)
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2828 >>> turtle.shearfactor()
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2829 >>> 0.5
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2830 """
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2831 if shear is None:
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2832 return self._shearfactor
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2833 self.pen(resizemode="user", shearfactor=shear)
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2834
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2835 def settiltangle(self, angle):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2836 """Rotate the turtleshape to point in the specified direction
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2837
53772
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2838 Argument: angle -- number
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2839
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2840 Rotate the turtleshape to point in the direction specified by angle,
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2841 regardless of its current tilt-angle. DO NOT change the turtle's
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2842 heading (direction of movement).
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2843
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2844
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2845 Examples (for a Turtle instance named turtle):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2846 >>> turtle.shape("circle")
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2847 >>> turtle.shapesize(5,2)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2848 >>> turtle.settiltangle(45)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2849 >>> stamp()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2850 >>> turtle.fd(50)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2851 >>> turtle.settiltangle(-45)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2852 >>> stamp()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2853 >>> turtle.fd(50)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2854 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2855 tilt = -angle * self._degreesPerAU * self._angleOrient
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2856 tilt = (tilt * math.pi / 180.0) % (2*math.pi)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2857 self.pen(resizemode="user", tilt=tilt)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2858
53772
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2859 def tiltangle(self, angle=None):
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2860 """Set or return the current tilt-angle.
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2861
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2862 Optional argument: angle -- number
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2863
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2864 Rotate the turtleshape to point in the direction specified by angle,
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2865 regardless of its current tilt-angle. DO NOT change the turtle's
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2866 heading (direction of movement).
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2867 If angle is not given: return the current tilt-angle, i. e. the angle
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2868 between the orientation of the turtleshape and the heading of the
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2869 turtle (its direction of movement).
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2870
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2871 Deprecated since Python 3.1
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2872
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2873 Examples (for a Turtle instance named turtle):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2874 >>> turtle.shape("circle")
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2875 >>> turtle.shapesize(5,2)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2876 >>> turtle.tilt(45)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2877 >>> turtle.tiltangle()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2878 """
53772
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2879 if angle is None:
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2880 tilt = -self._tilt * (180.0/math.pi) * self._angleOrient
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2881 return (tilt / self._degreesPerAU) % self._fullcircle
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2882 else:
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2883 self.settiltangle(angle)
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2884
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2885 def tilt(self, angle):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2886 """Rotate the turtleshape by angle.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2887
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2888 Argument:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2889 angle - a number
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2890
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2891 Rotate the turtleshape by angle from its current tilt-angle,
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2892 but do NOT change the turtle's heading (direction of movement).
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2893
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2894 Examples (for a Turtle instance named turtle):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2895 >>> turtle.shape("circle")
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2896 >>> turtle.shapesize(5,2)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2897 >>> turtle.tilt(30)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2898 >>> turtle.fd(50)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2899 >>> turtle.tilt(30)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2900 >>> turtle.fd(50)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2901 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2902 self.settiltangle(angle + self.tiltangle())
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2903
53772
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2904 def shapetransform(self, t11=None, t12=None, t21=None, t22=None):
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2905 """Set or return the current transformation matrix of the turtle shape.
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2906
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2907 Optional arguments: t11, t12, t21, t22 -- numbers.
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2908
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2909 If none of the matrix elements are given, return the transformation
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2910 matrix.
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2911 Otherwise set the given elements and transform the turtleshape
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2912 according to the matrix consisting of first row t11, t12 and
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2913 second row t21, 22.
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2914 Modify stretchfactor, shearfactor and tiltangle according to the
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2915 given matrix.
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2916
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2917 Examples (for a Turtle instance named turtle):
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2918 >>> turtle.shape("square")
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2919 >>> turtle.shapesize(4,2)
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2920 >>> turtle.shearfactor(-0.5)
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2921 >>> turtle.shapetransform()
73816
cc559e1e3bd8 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72985
diff changeset
2922 (4.0, -1.0, -0.0, 2.0)
53772
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2923 """
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2924 if t11 is t12 is t21 is t22 is None:
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2925 return self._shapetrafo
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2926 m11, m12, m21, m22 = self._shapetrafo
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2927 if t11 is not None: m11 = t11
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2928 if t12 is not None: m12 = t12
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2929 if t21 is not None: m21 = t21
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2930 if t22 is not None: m22 = t22
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2931 if t11 * t22 - t12 * t21 == 0:
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2932 raise TurtleGraphicsError("Bad shape transform matrix: must not be singular")
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2933 self._shapetrafo = (m11, m12, m21, m22)
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2934 alfa = math.atan2(-m21, m11) % (2 * math.pi)
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2935 sa, ca = math.sin(alfa), math.cos(alfa)
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2936 a11, a12, a21, a22 = (ca*m11 - sa*m21, ca*m12 - sa*m22,
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2937 sa*m11 + ca*m21, sa*m12 + ca*m22)
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2938 self._stretchfactor = a11, a22
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2939 self._shearfactor = a12/a22
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2940 self._tilt = alfa
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2941 self._update()
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2942
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2943
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2944 def _polytrafo(self, poly):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2945 """Computes transformed polygon shapes from a shape
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2946 according to current position and heading.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2947 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2948 screen = self.screen
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2949 p0, p1 = self._position
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2950 e0, e1 = self._orient
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2951 e = Vec2D(e0, e1 * screen.yscale / screen.xscale)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2952 e0, e1 = (1.0 / abs(e)) * e
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2953 return [(p0+(e1*x+e0*y)/screen.xscale, p1+(-e0*x+e1*y)/screen.yscale)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2954 for (x, y) in poly]
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2955
53772
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2956 def get_shapepoly(self):
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2957 """Return the current shape polygon as tuple of coordinate pairs.
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2958
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2959 No argument.
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2960
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2961 Examples (for a Turtle instance named turtle):
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2962 >>> turtle.shape("square")
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2963 >>> turtle.shapetransform(4, -1, 0, 2)
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2964 >>> turtle.get_shapepoly()
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2965 ((50, -20), (30, 20), (-50, 20), (-30, -20))
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2966
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2967 """
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2968 shape = self.screen._shapes[self.turtle.shapeIndex]
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2969 if shape._type == "polygon":
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2970 return self._getshapepoly(shape._data, shape._type == "compound")
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2971 # else return None
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2972
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2973 def _getshapepoly(self, polygon, compound=False):
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2974 """Calculate transformed shape polygon according to resizemode
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2975 and shapetransform.
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2976 """
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2977 if self._resizemode == "user" or compound:
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2978 t11, t12, t21, t22 = self._shapetrafo
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2979 elif self._resizemode == "auto":
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2980 l = max(1, self._pensize/5.0)
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2981 t11, t12, t21, t22 = l, 0, 0, l
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2982 elif self._resizemode == "noresize":
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2983 return polygon
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2984 return tuple([(t11*x + t12*y, t21*x + t22*y) for (x, y) in polygon])
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2985
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2986 def _drawturtle(self):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2987 """Manages the correct rendering of the turtle with respect to
51776
d06a3ebf306f Merged revisions 69846 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 51765
diff changeset
2988 its shape, resizemode, stretch and tilt etc."""
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2989 screen = self.screen
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2990 shape = screen._shapes[self.turtle.shapeIndex]
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2991 ttype = shape._type
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2992 titem = self.turtle._item
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2993 if self._shown and screen._updatecounter == 0 and screen._tracing > 0:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2994 self._hidden_from_screen = False
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2995 tshape = shape._data
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2996 if ttype == "polygon":
53772
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2997 if self._resizemode == "noresize": w = 1
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2998 elif self._resizemode == "auto": w = self._pensize
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2999 else: w =self._outlinewidth
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
3000 shape = self._polytrafo(self._getshapepoly(tshape))
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3001 fc, oc = self._fillcolor, self._pencolor
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3002 screen._drawpoly(titem, shape, fill=fc, outline=oc,
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3003 width=w, top=True)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3004 elif ttype == "image":
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3005 screen._drawimage(titem, self._position, tshape)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3006 elif ttype == "compound":
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3007 for item, (poly, fc, oc) in zip(titem, tshape):
53772
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
3008 poly = self._polytrafo(self._getshapepoly(poly, True))
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3009 screen._drawpoly(item, poly, fill=self._cc(fc),
53772
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
3010 outline=self._cc(oc), width=self._outlinewidth, top=True)
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3011 else:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3012 if self._hidden_from_screen:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3013 return
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3014 if ttype == "polygon":
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3015 screen._drawpoly(titem, ((0, 0), (0, 0), (0, 0)), "", "")
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3016 elif ttype == "image":
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3017 screen._drawimage(titem, self._position,
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3018 screen._shapes["blank"]._data)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3019 elif ttype == "compound":
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3020 for item in titem:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3021 screen._drawpoly(item, ((0, 0), (0, 0), (0, 0)), "", "")
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3022 self._hidden_from_screen = True
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3023
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3024 ############################## stamp stuff ###############################
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3025
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3026 def stamp(self):
51765
37e4df9fdaa6 Merged revisions 69821 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 51763
diff changeset
3027 """Stamp a copy of the turtleshape onto the canvas and return its id.
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3028
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3029 No argument.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3030
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3031 Stamp a copy of the turtle shape onto the canvas at the current
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3032 turtle position. Return a stamp_id for that stamp, which can be
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3033 used to delete it by calling clearstamp(stamp_id).
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3034
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3035 Example (for a Turtle instance named turtle):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3036 >>> turtle.color("blue")
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3037 >>> turtle.stamp()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3038 13
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3039 >>> turtle.fd(50)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3040 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3041 screen = self.screen
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3042 shape = screen._shapes[self.turtle.shapeIndex]
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3043 ttype = shape._type
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3044 tshape = shape._data
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3045 if ttype == "polygon":
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3046 stitem = screen._createpoly()
53772
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
3047 if self._resizemode == "noresize": w = 1
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
3048 elif self._resizemode == "auto": w = self._pensize
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
3049 else: w =self._outlinewidth
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
3050 shape = self._polytrafo(self._getshapepoly(tshape))
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3051 fc, oc = self._fillcolor, self._pencolor
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3052 screen._drawpoly(stitem, shape, fill=fc, outline=oc,
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3053 width=w, top=True)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3054 elif ttype == "image":
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3055 stitem = screen._createimage("")
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3056 screen._drawimage(stitem, self._position, tshape)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3057 elif ttype == "compound":
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3058 stitem = []
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3059 for element in tshape:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3060 item = screen._createpoly()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3061 stitem.append(item)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3062 stitem = tuple(stitem)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3063 for item, (poly, fc, oc) in zip(stitem, tshape):
53772
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
3064 poly = self._polytrafo(self._getshapepoly(poly, True))
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3065 screen._drawpoly(item, poly, fill=self._cc(fc),
53772
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
3066 outline=self._cc(oc), width=self._outlinewidth, top=True)
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3067 self.stampItems.append(stitem)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3068 self.undobuffer.push(("stamp", stitem))
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3069 return stitem
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3070
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3071 def _clearstamp(self, stampid):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3072 """does the work for clearstamp() and clearstamps()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3073 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3074 if stampid in self.stampItems:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3075 if isinstance(stampid, tuple):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3076 for subitem in stampid:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3077 self.screen._delete(subitem)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3078 else:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3079 self.screen._delete(stampid)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3080 self.stampItems.remove(stampid)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3081 # Delete stampitem from undobuffer if necessary
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3082 # if clearstamp is called directly.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3083 item = ("stamp", stampid)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3084 buf = self.undobuffer
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3085 if item not in buf.buffer:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3086 return
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3087 index = buf.buffer.index(item)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3088 buf.buffer.remove(item)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3089 if index <= buf.ptr:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3090 buf.ptr = (buf.ptr - 1) % buf.bufsize
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3091 buf.buffer.insert((buf.ptr+1)%buf.bufsize, [None])
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3092
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3093 def clearstamp(self, stampid):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3094 """Delete stamp with given stampid
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3095
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3096 Argument:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3097 stampid - an integer, must be return value of previous stamp() call.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3098
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3099 Example (for a Turtle instance named turtle):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3100 >>> turtle.color("blue")
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3101 >>> astamp = turtle.stamp()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3102 >>> turtle.fd(50)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3103 >>> turtle.clearstamp(astamp)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3104 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3105 self._clearstamp(stampid)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3106 self._update()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3107
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3108 def clearstamps(self, n=None):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3109 """Delete all or first/last n of turtle's stamps.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3110
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3111 Optional argument:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3112 n -- an integer
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3113
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3114 If n is None, delete all of pen's stamps,
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3115 else if n > 0 delete first n stamps
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3116 else if n < 0 delete last n stamps.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3117
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3118 Example (for a Turtle instance named turtle):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3119 >>> for i in range(8):
73816
cc559e1e3bd8 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72985
diff changeset
3120 ... turtle.stamp(); turtle.fd(30)
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3121 ...
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3122 >>> turtle.clearstamps(2)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3123 >>> turtle.clearstamps(-2)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3124 >>> turtle.clearstamps()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3125 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3126 if n is None:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3127 toDelete = self.stampItems[:]
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3128 elif n >= 0:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3129 toDelete = self.stampItems[:n]
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3130 else:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3131 toDelete = self.stampItems[n:]
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3132 for item in toDelete:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3133 self._clearstamp(item)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3134 self._update()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3135
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3136 def _goto(self, end):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3137 """Move the pen to the point end, thereby drawing a line
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3138 if pen is down. All other methodes for turtle movement depend
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3139 on this one.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3140 """
65687
613c7b596fe7 Fixed a typo in a comment.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 65656
diff changeset
3141 ## Version with undo-stuff
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3142 go_modes = ( self._drawing,
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3143 self._pencolor,
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3144 self._pensize,
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3145 isinstance(self._fillpath, list))
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3146 screen = self.screen
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3147 undo_entry = ("go", self._position, end, go_modes,
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3148 (self.currentLineItem,
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3149 self.currentLine[:],
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3150 screen._pointlist(self.currentLineItem),
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3151 self.items[:])
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3152 )
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3153 if self.undobuffer:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3154 self.undobuffer.push(undo_entry)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3155 start = self._position
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3156 if self._speed and screen._tracing == 1:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3157 diff = (end-start)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3158 diffsq = (diff[0]*screen.xscale)**2 + (diff[1]*screen.yscale)**2
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3159 nhops = 1+int((diffsq**0.5)/(3*(1.1**self._speed)*self._speed))
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3160 delta = diff * (1.0/nhops)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3161 for n in range(1, nhops):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3162 if n == 1:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3163 top = True
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3164 else:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3165 top = False
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3166 self._position = start + delta * n
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3167 if self._drawing:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3168 screen._drawline(self.drawingLineItem,
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3169 (start, self._position),
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3170 self._pencolor, self._pensize, top)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3171 self._update()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3172 if self._drawing:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3173 screen._drawline(self.drawingLineItem, ((0, 0), (0, 0)),
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3174 fill="", width=self._pensize)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3175 # Turtle now at end,
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3176 if self._drawing: # now update currentLine
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3177 self.currentLine.append(end)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3178 if isinstance(self._fillpath, list):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3179 self._fillpath.append(end)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3180 ###### vererbung!!!!!!!!!!!!!!!!!!!!!!
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3181 self._position = end
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3182 if self._creatingPoly:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3183 self._poly.append(end)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3184 if len(self.currentLine) > 42: # 42! answer to the ultimate question
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3185 # of life, the universe and everything
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3186 self._newLine()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3187 self._update() #count=True)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3188
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3189 def _undogoto(self, entry):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3190 """Reverse a _goto. Used for undo()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3191 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3192 old, new, go_modes, coodata = entry
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3193 drawing, pc, ps, filling = go_modes
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3194 cLI, cL, pl, items = coodata
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3195 screen = self.screen
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3196 if abs(self._position - new) > 0.5:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3197 print ("undogoto: HALLO-DA-STIMMT-WAS-NICHT!")
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3198 # restore former situation
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3199 self.currentLineItem = cLI
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3200 self.currentLine = cL
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3201
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3202 if pl == [(0, 0), (0, 0)]:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3203 usepc = ""
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3204 else:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3205 usepc = pc
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3206 screen._drawline(cLI, pl, fill=usepc, width=ps)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3207
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3208 todelete = [i for i in self.items if (i not in items) and
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3209 (screen._type(i) == "line")]
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3210 for i in todelete:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3211 screen._delete(i)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3212 self.items.remove(i)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3213
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3214 start = old
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3215 if self._speed and screen._tracing == 1:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3216 diff = old - new
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3217 diffsq = (diff[0]*screen.xscale)**2 + (diff[1]*screen.yscale)**2
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3218 nhops = 1+int((diffsq**0.5)/(3*(1.1**self._speed)*self._speed))
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3219 delta = diff * (1.0/nhops)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3220 for n in range(1, nhops):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3221 if n == 1:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3222 top = True
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3223 else:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3224 top = False
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3225 self._position = new + delta * n
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3226 if drawing:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3227 screen._drawline(self.drawingLineItem,
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3228 (start, self._position),
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3229 pc, ps, top)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3230 self._update()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3231 if drawing:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3232 screen._drawline(self.drawingLineItem, ((0, 0), (0, 0)),
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3233 fill="", width=ps)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3234 # Turtle now at position old,
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3235 self._position = old
68585
bf94b6a73fc8 #11565: Fix several typos. Patch by Piotr Kasprzyk.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 68488
diff changeset
3236 ## if undo is done during creating a polygon, the last vertex
bf94b6a73fc8 #11565: Fix several typos. Patch by Piotr Kasprzyk.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 68488
diff changeset
3237 ## will be deleted. if the polygon is entirely deleted,
bf94b6a73fc8 #11565: Fix several typos. Patch by Piotr Kasprzyk.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 68488
diff changeset
3238 ## creatingPoly will be set to False.
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3239 ## Polygons created before the last one will not be affected by undo()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3240 if self._creatingPoly:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3241 if len(self._poly) > 0:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3242 self._poly.pop()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3243 if self._poly == []:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3244 self._creatingPoly = False
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3245 self._poly = None
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3246 if filling:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3247 if self._fillpath == []:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3248 self._fillpath = None
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3249 print("Unwahrscheinlich in _undogoto!")
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3250 elif self._fillpath is not None:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3251 self._fillpath.pop()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3252 self._update() #count=True)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3253
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3254 def _rotate(self, angle):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3255 """Turns pen clockwise by angle.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3256 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3257 if self.undobuffer:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3258 self.undobuffer.push(("rot", angle, self._degreesPerAU))
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3259 angle *= self._degreesPerAU
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3260 neworient = self._orient.rotate(angle)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3261 tracing = self.screen._tracing
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3262 if tracing == 1 and self._speed > 0:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3263 anglevel = 3.0 * self._speed
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3264 steps = 1 + int(abs(angle)/anglevel)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3265 delta = 1.0*angle/steps
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3266 for _ in range(steps):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3267 self._orient = self._orient.rotate(delta)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3268 self._update()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3269 self._orient = neworient
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3270 self._update()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3271
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3272 def _newLine(self, usePos=True):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3273 """Closes current line item and starts a new one.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3274 Remark: if current line became too long, animation
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3275 performance (via _drawline) slowed down considerably.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3276 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3277 if len(self.currentLine) > 1:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3278 self.screen._drawline(self.currentLineItem, self.currentLine,
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3279 self._pencolor, self._pensize)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3280 self.currentLineItem = self.screen._createline()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3281 self.items.append(self.currentLineItem)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3282 else:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3283 self.screen._drawline(self.currentLineItem, top=True)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3284 self.currentLine = []
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3285 if usePos:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3286 self.currentLine = [self._position]
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3287
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3288 def filling(self):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3289 """Return fillstate (True if filling, False else).
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3290
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3291 No argument.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3292
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3293 Example (for a Turtle instance named turtle):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3294 >>> turtle.begin_fill()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3295 >>> if turtle.filling():
73816
cc559e1e3bd8 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72985
diff changeset
3296 ... turtle.pensize(5)
cc559e1e3bd8 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72985
diff changeset
3297 ... else:
cc559e1e3bd8 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72985
diff changeset
3298 ... turtle.pensize(3)
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3299 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3300 return isinstance(self._fillpath, list)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3301
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3302 def begin_fill(self):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3303 """Called just before drawing a shape to be filled.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3304
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3305 No argument.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3306
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3307 Example (for a Turtle instance named turtle):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3308 >>> turtle.color("black", "red")
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3309 >>> turtle.begin_fill()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3310 >>> turtle.circle(60)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3311 >>> turtle.end_fill()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3312 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3313 if not self.filling():
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3314 self._fillitem = self.screen._createpoly()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3315 self.items.append(self._fillitem)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3316 self._fillpath = [self._position]
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3317 self._newLine()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3318 if self.undobuffer:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3319 self.undobuffer.push(("beginfill", self._fillitem))
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3320 self._update()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3321
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3322
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3323 def end_fill(self):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3324 """Fill the shape drawn after the call begin_fill().
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3325
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3326 No argument.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3327
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3328 Example (for a Turtle instance named turtle):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3329 >>> turtle.color("black", "red")
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3330 >>> turtle.begin_fill()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3331 >>> turtle.circle(60)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3332 >>> turtle.end_fill()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3333 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3334 if self.filling():
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3335 if len(self._fillpath) > 2:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3336 self.screen._drawpoly(self._fillitem, self._fillpath,
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3337 fill=self._fillcolor)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3338 if self.undobuffer:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3339 self.undobuffer.push(("dofill", self._fillitem))
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3340 self._fillitem = self._fillpath = None
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3341 self._update()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3342
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3343 def dot(self, size=None, *color):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3344 """Draw a dot with diameter size, using color.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3345
68488
0991b40e895d #11515: fix several typos. Patch by Piotr Kasprzyk.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 66407
diff changeset
3346 Optional arguments:
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3347 size -- an integer >= 1 (if given)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3348 color -- a colorstring or a numeric color tuple
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3349
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3350 Draw a circular dot with diameter size, using color.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3351 If size is not given, the maximum of pensize+4 and 2*pensize is used.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3352
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3353 Example (for a Turtle instance named turtle):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3354 >>> turtle.dot()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3355 >>> turtle.fd(50); turtle.dot(20, "blue"); turtle.fd(50)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3356 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3357 if not color:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3358 if isinstance(size, (str, tuple)):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3359 color = self._colorstr(size)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3360 size = self._pensize + max(self._pensize, 4)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3361 else:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3362 color = self._pencolor
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3363 if not size:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3364 size = self._pensize + max(self._pensize, 4)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3365 else:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3366 if size is None:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3367 size = self._pensize + max(self._pensize, 4)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3368 color = self._colorstr(color)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3369 if hasattr(self.screen, "_dot"):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3370 item = self.screen._dot(self._position, size, color)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3371 self.items.append(item)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3372 if self.undobuffer:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3373 self.undobuffer.push(("dot", item))
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3374 else:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3375 pen = self.pen()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3376 if self.undobuffer:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3377 self.undobuffer.push(["seq"])
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3378 self.undobuffer.cumulate = True
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3379 try:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3380 if self.resizemode() == 'auto':
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3381 self.ht()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3382 self.pendown()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3383 self.pensize(size)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3384 self.pencolor(color)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3385 self.forward(0)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3386 finally:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3387 self.pen(pen)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3388 if self.undobuffer:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3389 self.undobuffer.cumulate = False
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3390
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3391 def _write(self, txt, align, font):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3392 """Performs the writing for write()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3393 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3394 item, end = self.screen._write(self._position, txt, align, font,
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3395 self._pencolor)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3396 self.items.append(item)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3397 if self.undobuffer:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3398 self.undobuffer.push(("wri", item))
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3399 return end
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3400
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3401 def write(self, arg, move=False, align="left", font=("Arial", 8, "normal")):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3402 """Write text at the current turtle position.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3403
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3404 Arguments:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3405 arg -- info, which is to be written to the TurtleScreen
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3406 move (optional) -- True/False
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3407 align (optional) -- one of the strings "left", "center" or right"
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3408 font (optional) -- a triple (fontname, fontsize, fonttype)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3409
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3410 Write text - the string representation of arg - at the current
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3411 turtle position according to align ("left", "center" or right")
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3412 and with the given font.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3413 If move is True, the pen is moved to the bottom-right corner
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3414 of the text. By default, move is False.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3415
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3416 Example (for a Turtle instance named turtle):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3417 >>> turtle.write('Home = ', True, align="center")
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3418 >>> turtle.write((0,0), True)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3419 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3420 if self.undobuffer:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3421 self.undobuffer.push(["seq"])
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3422 self.undobuffer.cumulate = True
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3423 end = self._write(str(arg), align.lower(), font)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3424 if move:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3425 x, y = self.pos()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3426 self.setpos(end, y)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3427 if self.undobuffer:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3428 self.undobuffer.cumulate = False
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3429
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3430 def begin_poly(self):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3431 """Start recording the vertices of a polygon.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3432
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3433 No argument.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3434
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3435 Start recording the vertices of a polygon. Current turtle position
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3436 is first point of polygon.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3437
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3438 Example (for a Turtle instance named turtle):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3439 >>> turtle.begin_poly()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3440 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3441 self._poly = [self._position]
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3442 self._creatingPoly = True
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3443
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3444 def end_poly(self):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3445 """Stop recording the vertices of a polygon.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3446
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3447 No argument.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3448
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3449 Stop recording the vertices of a polygon. Current turtle position is
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3450 last point of polygon. This will be connected with the first point.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3451
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3452 Example (for a Turtle instance named turtle):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3453 >>> turtle.end_poly()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3454 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3455 self._creatingPoly = False
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3456
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3457 def get_poly(self):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3458 """Return the lastly recorded polygon.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3459
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3460 No argument.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3461
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3462 Example (for a Turtle instance named turtle):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3463 >>> p = turtle.get_poly()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3464 >>> turtle.register_shape("myFavouriteShape", p)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3465 """
53772
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
3466 ## check if there is any poly?
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3467 if self._poly is not None:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3468 return tuple(self._poly)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3469
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3470 def getscreen(self):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3471 """Return the TurtleScreen object, the turtle is drawing on.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3472
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3473 No argument.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3474
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3475 Return the TurtleScreen object, the turtle is drawing on.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3476 So TurtleScreen-methods can be called for that object.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3477
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3478 Example (for a Turtle instance named turtle):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3479 >>> ts = turtle.getscreen()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3480 >>> ts
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3481 <turtle.TurtleScreen object at 0x0106B770>
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3482 >>> ts.bgcolor("pink")
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3483 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3484 return self.screen
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3485
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3486 def getturtle(self):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3487 """Return the Turtleobject itself.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3488
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3489 No argument.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3490
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3491 Only reasonable use: as a function to return the 'anonymous turtle':
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 31515
diff changeset
3492
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 31515
diff changeset
3493 Example:
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3494 >>> pet = getturtle()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3495 >>> pet.fd(50)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3496 >>> pet
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3497 <turtle.Turtle object at 0x0187D810>
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3498 >>> turtles()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3499 [<turtle.Turtle object at 0x0187D810>]
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3500 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3501 return self
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3502
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3503 getpen = getturtle
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3504
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3505
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3506 ################################################################
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3507 ### screen oriented methods recurring to methods of TurtleScreen
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3508 ################################################################
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3509
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3510 def _delay(self, delay=None):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3511 """Set delay value which determines speed of turtle animation.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3512 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3513 return self.screen.delay(delay)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3514
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3515 def onclick(self, fun, btn=1, add=None):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3516 """Bind fun to mouse-click event on this turtle on canvas.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3517
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3518 Arguments:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3519 fun -- a function with two arguments, to which will be assigned
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3520 the coordinates of the clicked point on the canvas.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3521 num -- number of the mouse-button defaults to 1 (left mouse button).
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3522 add -- True or False. If True, new binding will be added, otherwise
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3523 it will replace a former binding.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3524
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3525 Example for the anonymous turtle, i. e. the procedural way:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3526
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3527 >>> def turn(x, y):
73816
cc559e1e3bd8 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72985
diff changeset
3528 ... left(360)
cc559e1e3bd8 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72985
diff changeset
3529 ...
cc559e1e3bd8 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72985
diff changeset
3530 >>> onclick(turn) # Now clicking into the turtle will turn it.
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3531 >>> onclick(None) # event-binding will be removed
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 31515
diff changeset
3532 """
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3533 self.screen._onclick(self.turtle._item, fun, btn, add)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3534 self._update()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3535
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3536 def onrelease(self, fun, btn=1, add=None):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3537 """Bind fun to mouse-button-release event on this turtle on canvas.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3539 Arguments:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3540 fun -- a function with two arguments, to which will be assigned
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3541 the coordinates of the clicked point on the canvas.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3542 num -- number of the mouse-button defaults to 1 (left mouse button).
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3543
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3544 Example (for a MyTurtle instance named joe):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3545 >>> class MyTurtle(Turtle):
73816
cc559e1e3bd8 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72985
diff changeset
3546 ... def glow(self,x,y):
cc559e1e3bd8 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72985
diff changeset
3547 ... self.fillcolor("red")
cc559e1e3bd8 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72985
diff changeset
3548 ... def unglow(self,x,y):
cc559e1e3bd8 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72985
diff changeset
3549 ... self.fillcolor("")
cc559e1e3bd8 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72985
diff changeset
3550 ...
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3551 >>> joe = MyTurtle()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3552 >>> joe.onclick(joe.glow)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3553 >>> joe.onrelease(joe.unglow)
73816
cc559e1e3bd8 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72985
diff changeset
3554
cc559e1e3bd8 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72985
diff changeset
3555 Clicking on joe turns fillcolor red, unclicking turns it to
cc559e1e3bd8 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72985
diff changeset
3556 transparent.
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3557 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3558 self.screen._onrelease(self.turtle._item, fun, btn, add)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3559 self._update()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3560
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3561 def ondrag(self, fun, btn=1, add=None):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3562 """Bind fun to mouse-move event on this turtle on canvas.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3563
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3564 Arguments:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3565 fun -- a function with two arguments, to which will be assigned
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3566 the coordinates of the clicked point on the canvas.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3567 num -- number of the mouse-button defaults to 1 (left mouse button).
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3568
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3569 Every sequence of mouse-move-events on a turtle is preceded by a
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3570 mouse-click event on that turtle.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3571
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3572 Example (for a Turtle instance named turtle):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3573 >>> turtle.ondrag(turtle.goto)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3574
73816
cc559e1e3bd8 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72985
diff changeset
3575 Subsequently clicking and dragging a Turtle will move it
cc559e1e3bd8 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72985
diff changeset
3576 across the screen thereby producing handdrawings (if pen is
cc559e1e3bd8 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72985
diff changeset
3577 down).
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3578 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3579 self.screen._ondrag(self.turtle._item, fun, btn, add)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3580
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3581
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3582 def _undo(self, action, data):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3583 """Does the main part of the work for undo()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3584 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3585 if self.undobuffer is None:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3586 return
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3587 if action == "rot":
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3588 angle, degPAU = data
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3589 self._rotate(-angle*degPAU/self._degreesPerAU)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3590 dummy = self.undobuffer.pop()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3591 elif action == "stamp":
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3592 stitem = data[0]
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3593 self.clearstamp(stitem)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3594 elif action == "go":
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3595 self._undogoto(data)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3596 elif action in ["wri", "dot"]:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3597 item = data[0]
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3598 self.screen._delete(item)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3599 self.items.remove(item)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3600 elif action == "dofill":
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3601 item = data[0]
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3602 self.screen._drawpoly(item, ((0, 0),(0, 0),(0, 0)),
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3603 fill="", outline="")
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3604 elif action == "beginfill":
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3605 item = data[0]
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3606 self._fillitem = self._fillpath = None
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3607 if item in self.items:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3608 self.screen._delete(item)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3609 self.items.remove(item)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3610 elif action == "pen":
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3611 TPen.pen(self, data[0])
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3612 self.undobuffer.pop()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3613
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3614 def undo(self):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3615 """undo (repeatedly) the last turtle action.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3616
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3617 No argument.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3618
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3619 undo (repeatedly) the last turtle action.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3620 Number of available undo actions is determined by the size of
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3621 the undobuffer.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3622
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3623 Example (for a Turtle instance named turtle):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3624 >>> for i in range(4):
73816
cc559e1e3bd8 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72985
diff changeset
3625 ... turtle.fd(50); turtle.lt(80)
cc559e1e3bd8 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72985
diff changeset
3626 ...
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3627 >>> for i in range(8):
73816
cc559e1e3bd8 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72985
diff changeset
3628 ... turtle.undo()
cc559e1e3bd8 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72985
diff changeset
3629 ...
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3630 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3631 if self.undobuffer is None:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3632 return
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3633 item = self.undobuffer.pop()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3634 action = item[0]
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3635 data = item[1:]
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3636 if action == "seq":
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3637 while data:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3638 item = data.pop()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3639 self._undo(item[0], item[1:])
9585
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
3640 else:
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3641 self._undo(action, data)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3642
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3643 turtlesize = shapesize
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3644
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3645 RawPen = RawTurtle
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3646
49345
ab34a647e22f Merged revisions 66686 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 49249
diff changeset
3647 ### Screen - Singleton ########################
ab34a647e22f Merged revisions 66686 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 49249
diff changeset
3648
ab34a647e22f Merged revisions 66686 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 49249
diff changeset
3649 def Screen():
ab34a647e22f Merged revisions 66686 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 49249
diff changeset
3650 """Return the singleton screen object.
ab34a647e22f Merged revisions 66686 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 49249
diff changeset
3651 If none exists at the moment, create a new one and return it,
ab34a647e22f Merged revisions 66686 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 49249
diff changeset
3652 else return the existing one."""
ab34a647e22f Merged revisions 66686 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 49249
diff changeset
3653 if Turtle._screen is None:
ab34a647e22f Merged revisions 66686 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 49249
diff changeset
3654 Turtle._screen = _Screen()
ab34a647e22f Merged revisions 66686 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 49249
diff changeset
3655 return Turtle._screen
ab34a647e22f Merged revisions 66686 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 49249
diff changeset
3656
ab34a647e22f Merged revisions 66686 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 49249
diff changeset
3657 class _Screen(TurtleScreen):
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3658
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3659 _root = None
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3660 _canvas = None
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3661 _title = _CFG["title"]
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3662
9585
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
3663 def __init__(self):
49345
ab34a647e22f Merged revisions 66686 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 49249
diff changeset
3664 # XXX there is no need for this code to be conditional,
ab34a647e22f Merged revisions 66686 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 49249
diff changeset
3665 # as there will be only a single _Screen instance, anyway
ab34a647e22f Merged revisions 66686 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 49249
diff changeset
3666 # XXX actually, the turtle demo is injecting root window,
ab34a647e22f Merged revisions 66686 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 49249
diff changeset
3667 # so perhaps the conditional creation of a root should be
ab34a647e22f Merged revisions 66686 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 49249
diff changeset
3668 # preserved (perhaps by passing it as an optional parameter)
ab34a647e22f Merged revisions 66686 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 49249
diff changeset
3669 if _Screen._root is None:
ab34a647e22f Merged revisions 66686 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 49249
diff changeset
3670 _Screen._root = self._root = _Root()
ab34a647e22f Merged revisions 66686 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 49249
diff changeset
3671 self._root.title(_Screen._title)
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3672 self._root.ondestroy(self._destroy)
49345
ab34a647e22f Merged revisions 66686 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 49249
diff changeset
3673 if _Screen._canvas is None:
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3674 width = _CFG["width"]
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3675 height = _CFG["height"]
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3676 canvwidth = _CFG["canvwidth"]
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3677 canvheight = _CFG["canvheight"]
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3678 leftright = _CFG["leftright"]
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3679 topbottom = _CFG["topbottom"]
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3680 self._root.setupcanvas(width, height, canvwidth, canvheight)
49345
ab34a647e22f Merged revisions 66686 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 49249
diff changeset
3681 _Screen._canvas = self._root._getcanvas()
53772
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
3682 TurtleScreen.__init__(self, _Screen._canvas)
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3683 self.setup(width, height, leftright, topbottom)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3684
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3685 def setup(self, width=_CFG["width"], height=_CFG["height"],
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3686 startx=_CFG["leftright"], starty=_CFG["topbottom"]):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3687 """ Set the size and position of the main window.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3688
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3689 Arguments:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3690 width: as integer a size in pixels, as float a fraction of the screen.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3691 Default is 50% of screen.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3692 height: as integer the height in pixels, as float a fraction of the
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3693 screen. Default is 75% of screen.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3694 startx: if positive, starting position in pixels from the left
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3695 edge of the screen, if negative from the right edge
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3696 Default, startx=None is to center window horizontally.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3697 starty: if positive, starting position in pixels from the top
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3698 edge of the screen, if negative from the bottom edge
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3699 Default, starty=None is to center window vertically.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3700
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3701 Examples (for a Screen instance named screen):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3702 >>> screen.setup (width=200, height=200, startx=0, starty=0)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3703
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3704 sets window to 200x200 pixels, in upper left of screen
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3705
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3706 >>> screen.setup(width=.75, height=0.5, startx=None, starty=None)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3707
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3708 sets window to 75% of screen by 50% of screen and centers
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3709 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3710 if not hasattr(self._root, "set_geometry"):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3711 return
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3712 sw = self._root.win_width()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3713 sh = self._root.win_height()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3714 if isinstance(width, float) and 0 <= width <= 1:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3715 width = sw*width
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3716 if startx is None:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3717 startx = (sw - width) / 2
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3718 if isinstance(height, float) and 0 <= height <= 1:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3719 height = sh*height
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3720 if starty is None:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3721 starty = (sh - height) / 2
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3722 self._root.set_geometry(width, height, startx, starty)
53772
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
3723 self.update()
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3724
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3725 def title(self, titlestring):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3726 """Set title of turtle-window
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3727
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3728 Argument:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3729 titlestring -- a string, to appear in the titlebar of the
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3730 turtle graphics window.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3731
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3732 This is a method of Screen-class. Not available for TurtleScreen-
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3733 objects.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3734
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3735 Example (for a Screen instance named screen):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3736 >>> screen.title("Welcome to the turtle-zoo!")
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3737 """
49345
ab34a647e22f Merged revisions 66686 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 49249
diff changeset
3738 if _Screen._root is not None:
ab34a647e22f Merged revisions 66686 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 49249
diff changeset
3739 _Screen._root.title(titlestring)
ab34a647e22f Merged revisions 66686 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 49249
diff changeset
3740 _Screen._title = titlestring
9585
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
3741
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
3742 def _destroy(self):
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3743 root = self._root
49345
ab34a647e22f Merged revisions 66686 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 49249
diff changeset
3744 if root is _Screen._root:
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3745 Turtle._pen = None
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3746 Turtle._screen = None
49345
ab34a647e22f Merged revisions 66686 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 49249
diff changeset
3747 _Screen._root = None
ab34a647e22f Merged revisions 66686 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 49249
diff changeset
3748 _Screen._canvas = None
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3749 TurtleScreen._RUNNING = True
9585
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
3750 root.destroy()
15308
847ae40074d4 Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents: 14957
diff changeset
3751
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3752 def bye(self):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3753 """Shut the turtlegraphics window.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3754
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3755 Example (for a TurtleScreen instance named screen):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3756 >>> screen.bye()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3757 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3758 self._destroy()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3759
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3760 def exitonclick(self):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3761 """Go into mainloop until the mouse is clicked.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3762
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3763 No arguments.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3764
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3765 Bind bye() method to mouseclick on TurtleScreen.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3766 If "using_IDLE" - value in configuration dictionary is False
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3767 (default value), enter mainloop.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3768 If IDLE with -n switch (no subprocess) is used, this value should be
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3769 set to True in turtle.cfg. In this case IDLE's mainloop
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3770 is active also for the client script.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3771
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3772 This is a method of the Screen-class and not available for
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3773 TurtleScreen instances.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3774
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3775 Example (for a Screen instance named screen):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3776 >>> screen.exitonclick()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3777
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3778 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3779 def exitGracefully(x, y):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3780 """Screen.bye() with two dummy-parameters"""
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3781 self.bye()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3782 self.onclick(exitGracefully)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3783 if _CFG["using_IDLE"]:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3784 return
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3785 try:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3786 mainloop()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3787 except AttributeError:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3788 exit(0)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3789
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3790
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3791 class Turtle(RawTurtle):
68585
bf94b6a73fc8 #11565: Fix several typos. Patch by Piotr Kasprzyk.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 68488
diff changeset
3792 """RawTurtle auto-creating (scrolled) canvas.
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3793
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3794 When a Turtle object is created or a function derived from some
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3795 Turtle method is called a TurtleScreen object is automatically created.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3796 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3797 _pen = None
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3798 _screen = None
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3799
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3800 def __init__(self,
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3801 shape=_CFG["shape"],
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3802 undobuffersize=_CFG["undobuffersize"],
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3803 visible=_CFG["visible"]):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3804 if Turtle._screen is None:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3805 Turtle._screen = Screen()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3806 RawTurtle.__init__(self, Turtle._screen,
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3807 shape=shape,
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3808 undobuffersize=undobuffersize,
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3809 visible=visible)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3810
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3811 Pen = Turtle
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3812
9585
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
3813 def _getpen():
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3814 """Create the 'anonymous' turtle if not already present."""
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3815 if Turtle._pen is None:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3816 Turtle._pen = Turtle()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3817 return Turtle._pen
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3818
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3819 def _getscreen():
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3820 """Create a TurtleScreen if not already present."""
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3821 if Turtle._screen is None:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3822 Turtle._screen = Screen()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3823 return Turtle._screen
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3824
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3825 def write_docstringdict(filename="turtle_docstringdict"):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3826 """Create and write docstring-dictionary to file.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3827
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3828 Optional argument:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3829 filename -- a string, used as filename
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3830 default value is turtle_docstringdict
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3831
68585
bf94b6a73fc8 #11565: Fix several typos. Patch by Piotr Kasprzyk.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 68488
diff changeset
3832 Has to be called explicitly, (not used by the turtle-graphics classes)
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3833 The docstring dictionary will be written to the Python script <filname>.py
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3834 It is intended to serve as a template for translation of the docstrings
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3835 into different languages.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3836 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3837 docsdict = {}
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3838
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3839 for methodname in _tg_screen_functions:
49345
ab34a647e22f Merged revisions 66686 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 49249
diff changeset
3840 key = "_Screen."+methodname
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3841 docsdict[key] = eval(key).__doc__
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3842 for methodname in _tg_turtle_functions:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3843 key = "Turtle."+methodname
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3844 docsdict[key] = eval(key).__doc__
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3845
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3846 f = open("%s.py" % filename,"w")
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3847 keys = sorted([x for x in docsdict.keys()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3848 if x.split('.')[1] not in _alias_list])
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3849 f.write('docsdict = {\n\n')
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3850 for key in keys[:-1]:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3851 f.write('%s :\n' % repr(key))
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3852 f.write(' """%s\n""",\n\n' % docsdict[key])
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3853 key = keys[-1]
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3854 f.write('%s :\n' % repr(key))
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3855 f.write(' """%s\n"""\n\n' % docsdict[key])
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3856 f.write("}\n")
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3857 f.close()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3858
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3859 def read_docstrings(lang):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3860 """Read in docstrings from lang-specific docstring dictionary.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3861
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3862 Transfer docstrings, translated to lang, from a dictionary-file
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3863 to the methods of classes Screen and Turtle and - in revised form -
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3864 to the corresponding functions.
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 31515
diff changeset
3865 """
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3866 modname = "turtle_docstringdict_%(language)s" % {'language':lang.lower()}
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3867 module = __import__(modname)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3868 docsdict = module.docsdict
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3869 for key in docsdict:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3870 try:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3871 # eval(key).im_func.__doc__ = docsdict[key]
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3872 eval(key).__doc__ = docsdict[key]
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3873 except:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3874 print("Bad docstring-entry: %s" % key)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3875
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3876 _LANGUAGE = _CFG["language"]
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3877
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3878 try:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3879 if _LANGUAGE != "english":
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3880 read_docstrings(_LANGUAGE)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3881 except ImportError:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3882 print("Cannot find docsdict for", _LANGUAGE)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3883 except:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3884 print ("Unknown Error when trying to import %s-docstring-dictionary" %
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3885 _LANGUAGE)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3886
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3887
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3888 def getmethparlist(ob):
65656
4b542217e93e Issue #10193: Simplified instrospection used by turtle module
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 64405
diff changeset
3889 """Get strings describing the arguments for the given object
4b542217e93e Issue #10193: Simplified instrospection used by turtle module
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 64405
diff changeset
3890
4b542217e93e Issue #10193: Simplified instrospection used by turtle module
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 64405
diff changeset
3891 Returns a pair of strings representing function parameter lists
4b542217e93e Issue #10193: Simplified instrospection used by turtle module
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 64405
diff changeset
3892 including parenthesis. The first string is suitable for use in
4b542217e93e Issue #10193: Simplified instrospection used by turtle module
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 64405
diff changeset
3893 function definition and the second is suitable for use in function
4b542217e93e Issue #10193: Simplified instrospection used by turtle module
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 64405
diff changeset
3894 call. The "self" parameter is not included.
4b542217e93e Issue #10193: Simplified instrospection used by turtle module
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 64405
diff changeset
3895 """
4b542217e93e Issue #10193: Simplified instrospection used by turtle module
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 64405
diff changeset
3896 defText = callText = ""
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3897 # bit of a hack for methods - turn it into a function
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3898 # but we drop the "self" param.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3899 # Try and build one for Python defined functions
65656
4b542217e93e Issue #10193: Simplified instrospection used by turtle module
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 64405
diff changeset
3900 args, varargs, varkw = inspect.getargs(ob.__code__)
4b542217e93e Issue #10193: Simplified instrospection used by turtle module
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 64405
diff changeset
3901 items2 = args[1:]
4b542217e93e Issue #10193: Simplified instrospection used by turtle module
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 64405
diff changeset
3902 realArgs = args[1:]
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3903 defaults = ob.__defaults__ or []
65656
4b542217e93e Issue #10193: Simplified instrospection used by turtle module
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 64405
diff changeset
3904 defaults = ["=%r" % (value,) for value in defaults]
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3905 defaults = [""] * (len(realArgs)-len(defaults)) + defaults
65656
4b542217e93e Issue #10193: Simplified instrospection used by turtle module
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 64405
diff changeset
3906 items1 = [arg + dflt for arg, dflt in zip(realArgs, defaults)]
4b542217e93e Issue #10193: Simplified instrospection used by turtle module
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 64405
diff changeset
3907 if varargs is not None:
4b542217e93e Issue #10193: Simplified instrospection used by turtle module
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 64405
diff changeset
3908 items1.append("*" + varargs)
4b542217e93e Issue #10193: Simplified instrospection used by turtle module
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 64405
diff changeset
3909 items2.append("*" + varargs)
4b542217e93e Issue #10193: Simplified instrospection used by turtle module
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 64405
diff changeset
3910 if varkw is not None:
4b542217e93e Issue #10193: Simplified instrospection used by turtle module
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 64405
diff changeset
3911 items1.append("**" + varkw)
4b542217e93e Issue #10193: Simplified instrospection used by turtle module
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 64405
diff changeset
3912 items2.append("**" + varkw)
4b542217e93e Issue #10193: Simplified instrospection used by turtle module
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 64405
diff changeset
3913 defText = ", ".join(items1)
4b542217e93e Issue #10193: Simplified instrospection used by turtle module
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 64405
diff changeset
3914 defText = "(%s)" % defText
4b542217e93e Issue #10193: Simplified instrospection used by turtle module
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 64405
diff changeset
3915 callText = ", ".join(items2)
4b542217e93e Issue #10193: Simplified instrospection used by turtle module
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 64405
diff changeset
3916 callText = "(%s)" % callText
4b542217e93e Issue #10193: Simplified instrospection used by turtle module
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 64405
diff changeset
3917 return defText, callText
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3918
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3919 def _turtle_docrevise(docstr):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3920 """To reduce docstrings from RawTurtle class for functions
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3921 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3922 import re
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3923 if docstr is None:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3924 return None
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3925 turtlename = _CFG["exampleturtle"]
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3926 newdocstr = docstr.replace("%s." % turtlename,"")
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3927 parexp = re.compile(r' \(.+ %s\):' % turtlename)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3928 newdocstr = parexp.sub(":", newdocstr)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3929 return newdocstr
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3930
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3931 def _screen_docrevise(docstr):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3932 """To reduce docstrings from TurtleScreen class for functions
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 31515
diff changeset
3933 """
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3934 import re
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3935 if docstr is None:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3936 return None
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3937 screenname = _CFG["examplescreen"]
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3938 newdocstr = docstr.replace("%s." % screenname,"")
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3939 parexp = re.compile(r' \(.+ %s\):' % screenname)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3940 newdocstr = parexp.sub(":", newdocstr)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3941 return newdocstr
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3942
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3943 ## The following mechanism makes all methods of RawTurtle and Turtle available
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3944 ## as functions. So we can enhance, change, add, delete methods to these
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3945 ## classes and do not need to change anything here.
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3946
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3947
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3948 for methodname in _tg_screen_functions:
49345
ab34a647e22f Merged revisions 66686 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 49249
diff changeset
3949 pl1, pl2 = getmethparlist(eval('_Screen.' + methodname))
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3950 if pl1 == "":
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3951 print(">>>>>>", pl1, pl2)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3952 continue
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3953 defstr = ("def %(key)s%(pl1)s: return _getscreen().%(key)s%(pl2)s" %
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3954 {'key':methodname, 'pl1':pl1, 'pl2':pl2})
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3955 exec(defstr)
49345
ab34a647e22f Merged revisions 66686 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 49249
diff changeset
3956 eval(methodname).__doc__ = _screen_docrevise(eval('_Screen.'+methodname).__doc__)
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3957
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3958 for methodname in _tg_turtle_functions:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3959 pl1, pl2 = getmethparlist(eval('Turtle.' + methodname))
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3960 if pl1 == "":
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3961 print(">>>>>>", pl1, pl2)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3962 continue
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3963 defstr = ("def %(key)s%(pl1)s: return _getpen().%(key)s%(pl2)s" %
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3964 {'key':methodname, 'pl1':pl1, 'pl2':pl2})
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3965 exec(defstr)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3966 eval(methodname).__doc__ = _turtle_docrevise(eval('Turtle.'+methodname).__doc__)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3967
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3968
53772
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
3969 done = mainloop
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3970
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3971 if __name__ == "__main__":
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3972 def switchpen():
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3973 if isdown():
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3974 pu()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3975 else:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3976 pd()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3977
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3978 def demo1():
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3979 """Demo of old turtle.py - module"""
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3980 reset()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3981 tracer(True)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3982 up()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3983 backward(100)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3984 down()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3985 # draw 3 squares; the last filled
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3986 width(3)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3987 for i in range(3):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3988 if i == 2:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3989 begin_fill()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3990 for _ in range(4):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3991 forward(20)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3992 left(90)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3993 if i == 2:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3994 color("maroon")
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3995 end_fill()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3996 up()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3997 forward(30)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3998 down()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3999 width(1)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
4000 color("black")
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
4001 # move out of the way
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
4002 tracer(False)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
4003 up()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
4004 right(90)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
4005 forward(100)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
4006 right(90)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
4007 forward(100)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
4008 right(180)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
4009 down()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
4010 # some text
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
4011 write("startstart", 1)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
4012 write("start", 1)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
4013 color("red")
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
4014 # staircase
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
4015 for i in range(5):
9585
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
4016 forward(20)
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
4017 left(90)
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
4018 forward(20)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
4019 right(90)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
4020 # filled staircase
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
4021 tracer(True)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
4022 begin_fill()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
4023 for i in range(5):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
4024 forward(20)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
4025 left(90)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
4026 forward(20)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
4027 right(90)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
4028 end_fill()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
4029 # more text
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
4030
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
4031 def demo2():
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
4032 """Demo of some new features."""
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
4033 speed(1)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
4034 st()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
4035 pensize(3)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
4036 setheading(towards(0, 0))
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
4037 radius = distance(0, 0)/2.0
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
4038 rt(90)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
4039 for _ in range(18):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
4040 switchpen()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
4041 circle(radius, 10)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
4042 write("wait a moment...")
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
4043 while undobufferentries():
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
4044 undo()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
4045 reset()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
4046 lt(90)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
4047 colormode(255)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
4048 laenge = 10
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
4049 pencolor("green")
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
4050 pensize(3)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
4051 lt(180)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
4052 for i in range(-2, 16):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
4053 if i > 0:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
4054 begin_fill()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
4055 fillcolor(255-15*i, 0, 15*i)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
4056 for _ in range(3):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
4057 fd(laenge)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
4058 lt(120)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
4059 end_fill()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
4060 laenge += 10
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
4061 lt(15)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
4062 speed((speed()+1)%12)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
4063 #end_fill()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
4064
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
4065 lt(120)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
4066 pu()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
4067 fd(70)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
4068 rt(30)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
4069 pd()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
4070 color("red","yellow")
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
4071 speed(0)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
4072 begin_fill()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
4073 for _ in range(4):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
4074 circle(50, 90)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
4075 rt(90)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
4076 fd(30)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
4077 rt(90)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
4078 end_fill()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
4079 lt(90)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
4080 pu()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
4081 fd(30)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
4082 pd()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
4083 shape("turtle")
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
4084
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
4085 tri = getturtle()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
4086 tri.resizemode("auto")
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
4087 turtle = Turtle()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
4088 turtle.resizemode("auto")
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
4089 turtle.shape("turtle")
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
4090 turtle.reset()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
4091 turtle.left(90)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
4092 turtle.speed(0)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
4093 turtle.up()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
4094 turtle.goto(280, 40)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
4095 turtle.lt(30)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
4096 turtle.down()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
4097 turtle.speed(6)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
4098 turtle.color("blue","orange")
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
4099 turtle.pensize(2)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
4100 tri.speed(6)
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 31515
diff changeset
4101 setheading(towards(turtle))
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
4102 count = 1
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
4103 while tri.distance(turtle) > 4:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
4104 turtle.fd(3.5)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
4105 turtle.lt(0.6)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
4106 tri.setheading(tri.towards(turtle))
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
4107 tri.fd(4)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
4108 if count % 20 == 0:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
4109 turtle.stamp()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
4110 tri.stamp()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
4111 switchpen()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
4112 count += 1
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
4113 tri.write("CAUGHT! ", font=("Arial", 16, "bold"), align="right")
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
4114 tri.pencolor("black")
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
4115 tri.pencolor("red")
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
4116
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
4117 def baba(xdummy, ydummy):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
4118 clearscreen()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
4119 bye()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
4120
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
4121 time.sleep(2)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
4122
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
4123 while undobufferentries():
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
4124 tri.undo()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
4125 turtle.undo()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
4126 tri.fd(50)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
4127 tri.write(" Click me!", font = ("Courier", 12, "bold") )
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
4128 tri.onclick(baba, 1)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
4129
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
4130 demo1()
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 31515
diff changeset
4131 demo2()
47538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
4132 exitonclick()