annotate Lib/turtle.py @ 78069:a8aa77458e12 3.2

Add c_size_t and c_ssize_t to the ctypes summary table.
author Antoine Pitrou <solipsis@pitrou.net>
date Thu, 12 Jul 2012 20:31:50 +0200
parents cc559e1e3bd8
children 3f5f961262ec 8d60c1c89105
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
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
111 import os
65656
4b542217e93e Issue #10193: Simplified instrospection used by turtle module
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 64405
diff changeset
112 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
113
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 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
115 from copy import deepcopy
53772
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
116 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
117
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 _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
119 '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
120 _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
121 'clearscreen', 'colormode', 'delay', 'exitonclick', 'getcanvas',
53772
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
122 'getshapes', 'listen', 'mainloop', 'mode', 'numinput',
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
123 '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
124 'register_shape', 'resetscreen', 'screensize', 'setup',
53772
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
125 '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
126 '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
127 _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
128 '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
129 '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
130 '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
131 '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
132 '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
133 '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
134 '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
135 'seth', 'setheading', 'setpos', 'setposition', 'settiltangle',
53772
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
136 '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
137 '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
138 '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
139 'write', 'xcor', 'ycor']
53772
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
140 _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
141
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 __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
143 _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
144
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 _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
146 '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
147 '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
148
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 _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
150 "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
151 "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
152 "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
153 "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
154 "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
155 "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
156 "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
157 "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
158 "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
159 "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
160 "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
161 "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
162 "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
163 "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
164 "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
165 "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
166 "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
167 "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
168 "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
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
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 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
172 """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
173 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
174 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
175 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
176 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
177 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
178 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
179 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
180 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
181 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
182 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
183 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
184 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
185 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
186 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
187 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
188 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
189 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
190 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
191 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
192 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
193 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
194 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
195 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
196 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
197 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
198 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
199
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 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
201 """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
202
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 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
204 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
205 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
206 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
207 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
208 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
209 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
210 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
211 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
212 """
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 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
214 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
215 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
216 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
217 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
218 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
219 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
220 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
221 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
222 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
223 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
224 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 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
226 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
227 _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
228 _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
229
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 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
231 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
232 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
233 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
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
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 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
237 """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
238 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
239 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
240 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
241
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 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
243 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
244 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
245 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
246 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
247 |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
248 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
249 """
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 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
251 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
252 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
253 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
254 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
255 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
256 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
257 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
258 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
259 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
260 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
261 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
262 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
263 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
264 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
265 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
266 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
267 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
268 """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
269 """
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 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
271 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
272 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
273 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
274 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
275 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
276 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
277 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
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 ##############################################################################
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
281 ### 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
282 ### 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
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
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 ## 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
286 ## 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
287
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 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
289 """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
290 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
291 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
292 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
293 __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
294 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
295 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
296 _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
297
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 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
299 """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
300 _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 __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
302 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
303
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 __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
305 '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
306 '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
307
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 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
309 ### 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
310 _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 __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
312 _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
313 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
314 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
315 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
316 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
317 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
318 _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
319
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 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
321 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
322 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
323 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
324 __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
325 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
326 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
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
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 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
330 """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
331
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 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
333 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
334 """
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 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
336 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
337 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
338 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
339 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
340 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
341 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
342 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
343 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
344 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
345 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
346 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
347 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
348 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
349 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
350 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
351 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
352 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
353 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
354 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
355 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
356 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
357 self.reset()
49759
8b9bda5b8d4a Merged revisions 67279 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 49345
diff changeset
358 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
359
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
360 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
361 """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
362 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
363 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
364 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
365 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
366 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
367 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
368 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
369 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
370 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
371 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
372 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
373 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
374 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
375 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
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
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 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
379 """ 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
380 """
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 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
382 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
383 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
384 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
385 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
386 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
387 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
388 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
389 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
390 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
391 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
392 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
393
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 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
395 """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
396 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
397
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 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
399 """ '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
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
401 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
402
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 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
404 """ '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
405 """
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 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
407
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 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
409 """ '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
410 """
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 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
412
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 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
414 """ '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
415 """
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 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
417
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 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
419 """ '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
420 """
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 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
422
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 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
424 """ '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
425 """
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 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
427
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 __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
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
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 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
432 """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
433 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
434 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
435
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 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
437 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
438 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
439
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 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
441 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
442
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 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
444 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
445
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 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
447 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
448
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 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
450 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
451
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 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
453 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
454
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 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
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
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 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
459 """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
460 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
461
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 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
463 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
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
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 @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
467 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
468 """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
469 """
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 = 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
471 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
472 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
473
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 @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
475 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
476 """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
477 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
478 """
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 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
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
481 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
482 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
483 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
484 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
485 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
486 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
487 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
488 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
489 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
490 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
491 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
492 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
493
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 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
495 """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
496 """
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 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
498
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 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
500 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
501 """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
502 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
503 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
504 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
505 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
506 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
507 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
508 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
509 """
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 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
511 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
512 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
513 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
514 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
515 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
516 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
517 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
518 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
519 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
520 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
521 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
522 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
523
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 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
525 """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
526 """
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 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
528 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
529
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 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
531 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
532 """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
533 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
534 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
535 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
536 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
537 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
538 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
539 """
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 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
541 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
542 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
543 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
544 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
545 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
546 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
547 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
548 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
549 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
550 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
551 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
552
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 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
554 """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
555 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
556 """
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 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
558
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 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
560 """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
561 """
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 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
563
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 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
565 """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
566 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
567
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 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
569 """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
570 """
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 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
572 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
573 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
574 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
575 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
576 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
577
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 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
579 """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
580 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
581 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
582 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
583 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
584 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
585 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
586
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 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
588 """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
589 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
590 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
591 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
592 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
593 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
594 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
595 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
596 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
597 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
598 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
599 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
600 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
601
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 ## 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
603 ## """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
604
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 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
606 """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
607 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
608 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
609 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
610 """
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 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
612 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
613 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
614 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
615 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
616 -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
617 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
618 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
619
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 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
621 """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
622 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
623 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
624 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
625
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 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
627 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
628 """
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 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
630 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
631 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
632 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
633 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
634 -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
635 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
636 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
637 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
638
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 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
640 """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
641 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
642 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
643 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
644
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 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
646 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
647 """
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 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
649 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
650 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
651 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
652 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
653 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
654 -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
655 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
656 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
657 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
658 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
659
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 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
661 """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
662 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
663 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
664 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
665
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 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
667 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
668 """
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 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
670 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
671 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
672 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
673 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
674 -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
675 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
676 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
677
53772
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
678 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
679 """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
680 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
681 """
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 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
683 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
684 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
685 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
686 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
687 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
688
53772
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
689 def _onkeypress(self, fun, key=None):
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
690 """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
691 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
692 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
693 """
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
694 if fun is None:
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
695 if key is None:
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
696 self.cv.unbind("<KeyPress>", None)
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
697 else:
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
698 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
699 else:
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
700 def eventfun(event):
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
701 fun()
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
702 if key is None:
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
703 self.cv.bind("<KeyPress>", eventfun)
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
704 else:
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
705 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
706
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
707 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
708 """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
709 """
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 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
711
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 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
713 """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
714 """
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 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
716 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
717 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
718 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
719
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 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
721 """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
722 """
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 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
724
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 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
726 """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
727 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
728 """
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 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
730 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
731 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
732
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 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
734 """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
735 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
736 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
737 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
738 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
739 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
740
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 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
742 """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
743 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
744 """
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 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
746
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 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
748 """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
749 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
750 >>> 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
751 >>> 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
752 [(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
753 (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
754 >>> """
66407
cc9c51f9524f Merged revisions 86697 via svnmerge from
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 65952
diff changeset
755 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
756 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
757 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
758
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 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
760 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
761
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 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
763 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
764 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
765 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
766 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
767 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
768 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
769 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
770 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
771 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
772 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
773
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
774 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
775 """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
776 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
777 """
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 # 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
779 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
780 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
781 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
782 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
783 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
784 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
785 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
786 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
787 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
788
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 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
790 """ 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
791 """
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 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
793 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
794 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
795 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
796 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
797 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
798 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
799
53772
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
800 def mainloop(self):
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
801 """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
802
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
803 No argument.
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
804
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
805 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
806 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
807 (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
808
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
809 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
810 >>> screen.mainloop()
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 """
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
813 TK.mainloop()
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
814
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
815 def textinput(self, title, prompt):
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
816 """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
817
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
818 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
819 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
820
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
821 Return the string input
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
822 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
823
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
824 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
825 >>> 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
826
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
827 """
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
828 return simpledialog.askstring(title, prompt)
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
829
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
830 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
831 """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
832
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
833 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
834 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
835 default: default value
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
836 minval: minimum value for imput
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
837 maxval: maximum value for input
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
838
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
839 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
840 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
841 correction. Return the number input.
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
842 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
843
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
844 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
845 >>> 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
846
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
847 """
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
848 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
849 minvalue=minval, maxvalue=maxval)
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
850
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
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 ##############################################################################
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 ### 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
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
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 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
858 """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
859
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 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
861 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
862 """
14957
db1cce1ffb60 Don't rename Tkinter to Tk; closes bug 115714
Martin v. Löwis <martin@v.loewis.de>
parents: 9585
diff changeset
863 pass
9585
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
864
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
865
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 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
867 """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
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
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 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
872 """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
873
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 _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
875 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
876 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
877 """
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 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
879 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
880 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
881 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
882 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
883 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
884 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
885 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
886 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
887 # 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
888 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
889 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
890 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
891 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
892 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
893
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 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
895 """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
896
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 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
898 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
899 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
900
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 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
902 -- 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
903
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 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
905 >>> 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
906 >>> 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
907 >>> s.addcomponent(poly, "red", "blue")
73816
cc559e1e3bd8 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72985
diff changeset
908 >>> # .. 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
909 """
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 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
911 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
912 % 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
913 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
914 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
915 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
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
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 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
919 """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
920 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
921 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
922 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
923 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
924 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
925 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
926 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
927 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
928 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
929 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
930 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
931 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
932 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
933 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
934 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
935 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
936 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
937 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
938 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
939 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
940 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
941 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
942 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
943 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
944 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
945 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
946 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
947 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
948 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
949 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
950 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
951 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
952 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
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
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 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
957 """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
958
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 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
960 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
961 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
962 """
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 _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
964
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 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
966 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
967 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
968 "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
969 "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
970 (-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
971 (-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
972 (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
973 (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
974 "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
975 (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
976 (-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
977 (-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
978 (-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
979 (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
980 "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
981 (-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
982 "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
983 (-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
984 "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
985 "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
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
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 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
989
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 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
991 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
992 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
993 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
994 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
995 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
996
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 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
998 """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
999
53772
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
1000 No argument.
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
1001
51765
37e4df9fdaa6 Merged revisions 69821 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 51763
diff changeset
1002 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
1003 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
1004
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1005 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
1006 >>> 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
1007
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 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
1009 """
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._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
1011 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
1012 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
1013 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
1014 self._bgpicname = "nopic"
9585
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
1015 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
1016 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
1017 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
1018 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
1019 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
1020 self.onclick(None, btn)
53772
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
1021 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
1022 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
1023 self.onkey(None, key)
53772
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
1024 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
1025 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
1026
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 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
1028 """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
1029
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 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
1031 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
1032
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 '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
1034 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
1035 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
1036 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
1037 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
1038
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 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
1040 ------------|-------------------------|-------------------
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 '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
1042 '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
1043
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 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
1045 >>> 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
1046 >>> 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
1047 '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
1048 """
60058
40f787b151ac Merged revisions 79297,79310,79382,79425-79427,79450 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58218
diff changeset
1049 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
1050 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
1051 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
1052 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
1053 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
1054 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
1055 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
1056 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
1057 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
1058 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
1059 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
1060
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 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
1062 """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
1063
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 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
1065 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
1066 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
1067 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
1068 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
1069
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 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
1071 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
1072 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
1073
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 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
1075 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
1076
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 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
1078 >>> 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
1079 >>> for _ in range(36):
73816
cc559e1e3bd8 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72985
diff changeset
1080 ... left(10)
cc559e1e3bd8 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72985
diff changeset
1081 ... 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
1082 """
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 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
1084 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
1085 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
1086 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
1087 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
1088 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
1089 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
1090 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
1091 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
1092 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
1093 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
1094 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
1095 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
1096 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
1097 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
1098 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
1099
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 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
1101 """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
1102
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 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
1104 (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
1105 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
1106 !! 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
1107 !! 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
1108 (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
1109 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
1110 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
1111 (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
1112 (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
1113 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
1114 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
1115
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 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
1117 --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
1118
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 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
1120 >>> 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
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 """
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 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
1124 # 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
1125 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
1126 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
1127 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
1128 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
1129 + "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
1130 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
1131 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
1132 ## 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
1133 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
1134
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 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
1136 """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
1137
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 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
1139 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
1140 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
1141
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 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
1143 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
1144 """
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 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
1146 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
1147 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
1148 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
1149 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
1150 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
1151 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
1152 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
1153 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
1154 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
1155 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
1156 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
1157 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
1158 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
1159 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
1160 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
1161
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 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
1163 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
1164 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
1165 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
1166 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
1167 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
1168 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
1169 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
1170 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
1171 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
1172
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 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
1174 """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
1175
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 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
1177 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
1178
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 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
1180
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 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
1182 >>> 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
1183 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
1184 >>> screen.colormode(255)
73816
cc559e1e3bd8 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72985
diff changeset
1185 >>> 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
1186 """
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 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
1188 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
1189 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
1190 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
1191 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
1192 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
1193
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 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
1195 """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
1196
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 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
1198
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 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
1200 >>> 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
1201 """
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 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
1203 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
1204 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
1205
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 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
1207 """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
1208
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 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
1210 >>> 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
1211 [<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
1212 """
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 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
1214
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 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
1216 """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
1217
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 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
1219 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
1220
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 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
1222 >>> 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
1223 >>> 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
1224 '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
1225 >>> 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
1226 >>> 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
1227 '#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
1228 """
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 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
1230 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
1231 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
1232 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
1233 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
1234 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
1235 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
1236 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
1237
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 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
1239 """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
1240
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 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
1242 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
1243 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
1244
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 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
1246 (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
1247 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
1248
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 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
1250 >>> 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
1251 >>> 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
1252 >>> for i in range(200):
73816
cc559e1e3bd8 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72985
diff changeset
1253 ... fd(dist)
cc559e1e3bd8 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72985
diff changeset
1254 ... rt(90)
cc559e1e3bd8 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72985
diff changeset
1255 ... 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
1256 """
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 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
1258 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
1259 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
1260 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
1261 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
1262 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
1263 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
1264 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
1265
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 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
1267 """ 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
1268
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 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
1270 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
1271
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 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
1273 >>> 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
1274 >>> 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
1275 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
1276 """
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 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
1278 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
1279 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
1280
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1281 def _incrementudc(self):
73816
cc559e1e3bd8 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72985
diff changeset
1282 """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
1283 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
1284 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
1285 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
1286 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
1287 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
1288 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
1289
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 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
1291 """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
1292 """
53772
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
1293 tracing = self._tracing
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
1294 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
1295 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
1296 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
1297 t._drawturtle()
53772
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
1298 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
1299 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
1300
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 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
1302 """ 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
1303
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 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
1305 >>> 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
1306 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
1307 """
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 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
1309
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 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
1311 """ 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
1312
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 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
1314 >>> 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
1315 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
1316 """
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 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
1318
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 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
1320 """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
1321
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 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
1323
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 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
1325 >>> 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
1326 >>> 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
1327 <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
1328 """
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 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
1330
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 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
1332 """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
1333
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 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
1335
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 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
1337 >>> 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
1338 ['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
1339 """
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 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
1341
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 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
1343 """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
1344
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 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
1346 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
1347 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
1348 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
1349
73816
cc559e1e3bd8 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72985
diff changeset
1350 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
1351
cc559e1e3bd8 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72985
diff changeset
1352 >>> screen.onclick(goto)
cc559e1e3bd8 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72985
diff changeset
1353 >>> # Subsequently clicking into the TurtleScreen will
cc559e1e3bd8 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72985
diff changeset
1354 >>> # 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
1355 >>> 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
1356 """
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 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
1358
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 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
1360 """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
1361
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 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 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
1364 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
1365
51765
37e4df9fdaa6 Merged revisions 69821 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 51763
diff changeset
1366 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
1367 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
1368
73816
cc559e1e3bd8 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72985
diff changeset
1369 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
1370
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1371 >>> def f():
73816
cc559e1e3bd8 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72985
diff changeset
1372 ... fd(50)
cc559e1e3bd8 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72985
diff changeset
1373 ... lt(60)
cc559e1e3bd8 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72985
diff changeset
1374 ...
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
1375 >>> 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
1376 >>> 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
1377
73816
cc559e1e3bd8 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72985
diff changeset
1378 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
1379 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
1380
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
1381 """
60058
40f787b151ac Merged revisions 79297,79310,79382,79425-79427,79450 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58218
diff changeset
1382 if fun is None:
53772
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
1383 if key in self._keys:
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
1384 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
1385 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
1386 self._keys.append(key)
53772
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
1387 self._onkeyrelease(fun, key)
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
1388
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
1389 def onkeypress(self, fun, key=None):
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
1390 """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
1391 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
1392
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
1393 Arguments:
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
1394 fun -- a function with no arguments
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
1395 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
1396
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
1397 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
1398 must have focus. (See method listen.)
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
1399
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
1400 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
1401 and a Turtle instance named turtle):
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
1402
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
1403 >>> def f():
73816
cc559e1e3bd8 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72985
diff changeset
1404 ... fd(50)
cc559e1e3bd8 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72985
diff changeset
1405 ... lt(60)
cc559e1e3bd8 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72985
diff changeset
1406 ...
cc559e1e3bd8 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72985
diff changeset
1407 >>> screen.onkeypress(f, "Up")
53772
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
1408 >>> screen.listen()
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
1409
73816
cc559e1e3bd8 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72985
diff changeset
1410 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
1411 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
1412 consequently drawing a hexagon.
53772
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
1413 """
60058
40f787b151ac Merged revisions 79297,79310,79382,79425-79427,79450 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58218
diff changeset
1414 if fun is None:
53772
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
1415 if key in self._keys:
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
1416 self._keys.remove(key)
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
1417 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
1418 self._keys.append(key)
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
1419 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
1420
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 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
1422 """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
1423
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 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
1425 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
1426 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
1427
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 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
1429 >>> 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
1430 """
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 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
1432
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 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
1434 """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
1435
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 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 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
1438 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
1439
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 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
1441
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 >>> 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
1443 >>> def f():
73816
cc559e1e3bd8 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72985
diff changeset
1444 ... if running:
cc559e1e3bd8 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72985
diff changeset
1445 ... fd(50)
cc559e1e3bd8 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72985
diff changeset
1446 ... lt(60)
cc559e1e3bd8 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72985
diff changeset
1447 ... screen.ontimer(f, 250)
cc559e1e3bd8 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72985
diff changeset
1448 ...
cc559e1e3bd8 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72985
diff changeset
1449 >>> 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
1450 >>> 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
1451 """
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 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
1453
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 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
1455 """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
1456
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 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
1458 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
1459
68488
0991b40e895d #11515: fix several typos. Patch by Piotr Kasprzyk.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 66407
diff changeset
1460 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
1461 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
1462 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
1463
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 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
1465 >>> 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
1466 '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
1467 >>> 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
1468 >>> 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
1469 '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
1470 """
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 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
1472 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
1473 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
1474 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
1475 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
1476 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
1477
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1478 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
1479 """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
1480
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 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
1482 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
1483 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
1484 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
1485 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
1486
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 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
1488 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
1489 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
1490
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 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
1492 >>> turtle.screensize(2000,1500)
73816
cc559e1e3bd8 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72985
diff changeset
1493 >>> # 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
1494 """
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 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
1496
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 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
1498 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
1499 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
1500 addshape = register_shape
53772
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
1501 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
1502
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 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
1504 """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
1505 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
1506 """
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 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
1508 "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
1509 "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
1510 "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
1511 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
1512 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
1513 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
1514
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 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
1516 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
1517 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
1518 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
1519 self.undobuffer = None
9585
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
1520 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
1521 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
1522 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
1523 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
1524
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 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
1526 """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
1527
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 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
1529 """
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._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
1531 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
1532
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 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
1534 """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
1535 """
60058
40f787b151ac Merged revisions 79297,79310,79382,79425-79427,79450 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58218
diff changeset
1536 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
1537 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
1538 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
1539 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
1540 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
1541 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
1542 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
1543 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
1544 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
1545 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
1546 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
1547
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 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
1549 """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
1550 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
1551 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
1552 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
1553 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
1554 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
1555 self._angleOffset = fullcircle/4.
9585
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
1556
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
1557 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
1558 """ 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
1559
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
1560 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
1561 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
1562
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 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
1564 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
1565 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
1566
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 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
1568 >>> 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
1569 >>> 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
1570 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
1571
7ab72ba38fe0 Merged revisions 85732,85778,85785,85853,85930,86008,86089 via svnmerge from
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 60058
diff changeset
1572 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
1573 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
1574 >>> 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
1575 >>> 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
1576 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
1577
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 31515
diff changeset
1578 """
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
1579 self._setDegreesPerAU(fullcircle)
9585
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
1580
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
1581 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
1582 """ 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
1583
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
1584 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
1585
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 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
1587 >>> 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
1588 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
1589 >>> 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
1590 >>> 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
1591 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
1592 """
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
1593 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
1594
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 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
1596 """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
1597 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
1598 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
1599
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 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
1601 """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
1602 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
1603 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
1604
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 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
1606 """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
1607 self._position = end
9585
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
1608
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
1609 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
1610 """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
1611
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 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
1613
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 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
1615 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
1616
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 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
1618 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
1619
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1620 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
1621 >>> 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
1622 (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
1623 >>> 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
1624 >>> 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
1625 (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
1626 >>> 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
1627 >>> 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
1628 (-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
1629 """
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
1630 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
1631
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 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
1633 """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
1634
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 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
1636
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 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
1638 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
1639
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 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
1641 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
1642
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1643 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
1644 >>> 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
1645 (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
1646 >>> 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
1647 >>> 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
1648 (-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
1649 """
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
1650 self._go(-distance)
9585
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
1651
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
1652 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
1653 """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
1654
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 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
1656
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 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
1658 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
1659
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1660 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
1661 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
1662 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
1663
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1664 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
1665 >>> 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
1666 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
1667 >>> 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
1668 >>> 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
1669 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
1670 """
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
1671 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
1672
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 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
1674 """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
1675
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 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
1677
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 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
1679 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
1680
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 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
1682 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
1683 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
1684
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 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
1686 >>> 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
1687 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
1688 >>> 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
1689 >>> 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
1690 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
1691 """
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
1692 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
1693
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 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
1695 """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
1696
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 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
1698
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 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
1700
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 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
1702 >>> 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
1703 (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
1704 """
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
1705 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
1706
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 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
1708 """ 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
1709
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 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
1711
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 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
1713 >>> 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
1714 >>> 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
1715 >>> 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
1716 >>> 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
1717 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
1718 """
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 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
1720
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 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
1722 """ 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
1723 ---
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 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
1725
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 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
1727 >>> 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
1728 >>> 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
1729 >>> 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
1730 >>> 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
1731 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
1732 """
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
1733 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
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
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 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
1737 """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
1738
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 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
1740
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 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
1742 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
1743 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
1744
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 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
1746 --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
1747 --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
1748
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 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
1750 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
1751
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 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
1753 >>> 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
1754 >>> 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
1755 (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
1756 >>> 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
1757 >>> 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
1758 (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
1759 >>> 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
1760 >>> 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
1761 (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
1762 >>> 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
1763 >>> 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
1764 (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
1765 """
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
1766 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
1767 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
1768 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
1769 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
1770
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 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
1772 """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
1773
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 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
1775
51765
37e4df9fdaa6 Merged revisions 69821 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 51763
diff changeset
1776 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
1777 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
1778
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 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
1780 >>> 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
1781 """
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.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
1783 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
1784
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 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
1786 """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
1787
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 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
1789 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
1790
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 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
1792 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
1793
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1794 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
1795 >>> 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
1796 (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
1797 >>> 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
1798 >>> 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
1799 (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
1800 """
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
1801 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
1802
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 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
1804 """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
1805
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 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
1807 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
1808
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 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
1810 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
1811
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1812 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
1813 >>> 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
1814 (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
1815 >>> 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
1816 >>> 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
1817 (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
1818 """
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 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
1820
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 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
1822 """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
1823
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 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
1825 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
1826 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
1827
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 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
1829 --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
1830 --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
1831 --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
1832
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 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
1834 >>> 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
1835 (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
1836 >>> 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
1837 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
1838 >>> 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
1839 >>> 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
1840 >>> 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
1841 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
1842 """
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
1843 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
1844 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
1845 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
1846 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
1847 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
1848 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
1849 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
1850 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
1851 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
1852
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 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
1854 """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
1855
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 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
1857 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
1858 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
1859
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 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
1861 --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
1862 --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
1863 --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
1864
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 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
1866 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
1867 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
1868
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 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
1870 >>> 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
1871 (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
1872 >>> 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
1873 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
1874 """
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
1875 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
1876 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
1877 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
1878 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
1879 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
1880 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
1881 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
1882 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
1883 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
1884 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
1885 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
1886 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
1887
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 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
1889 """ 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
1890
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 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
1892
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 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
1894 >>> 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
1895 >>> 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
1896 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
1897 """
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 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
1899 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
1900 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
1901 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
1902
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 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
1904 """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
1905
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 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
1907
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 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
1909 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
1910
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 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
1912 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
1913
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 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
1915 -------------------|--------------------
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 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
1917 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
1918 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
1919 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
1920
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 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
1922 >>> 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
1923 >>> 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
1924 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
1925 """
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 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
1927 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
1928 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
1929 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
1930
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 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
1932 """ 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
1933
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 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
1935 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
1936 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
1937 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
1938
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 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
1940 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
1941 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
1942 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
1943 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
1944 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
1945 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
1946
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 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
1948 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
1949 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
1950 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
1951
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 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
1953 --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
1954 --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
1955 --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
1956
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 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
1958 >>> 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
1959 >>> 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
1960 """
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 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
1962 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
1963 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
1964 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
1965 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
1966 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
1967 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
1968 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
1969 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
1970 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
1971 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
1972 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
1973 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
1974 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
1975 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
1976 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
1977 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
1978 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
1979 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
1980 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
1981 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
1982 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
1983 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
1984 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
1985 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
1986 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
1987 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
1988 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
1989 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
1990 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
1991 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
1992 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
1993
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 ## 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
1995
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 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
1997 """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
1998 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
1999 """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
2000 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
2001 """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
2002
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 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
2004 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
2005 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
2006 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
2007 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
2008 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
2009 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
2010 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
2011 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
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
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 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
2015 """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
2016 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
2017 """
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 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
2019 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
2020 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
2021 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
2022
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 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
2024 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
2025 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
2026 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
2027 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
2028 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
2029 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
2030 self._speed = 3
53772
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2031 self._stretchfactor = (1., 1.)
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2032 self._shearfactor = 0.
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2033 self._tilt = 0.
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2034 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
2035 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
2036
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 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
2038 """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
2039
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 (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
2041 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
2042
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 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
2044 - "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
2045 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
2046 - "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
2047 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
2048 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
2049 - "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
2050 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
2051 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
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
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 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
2055 >>> 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
2056 >>> 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
2057 '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
2058 """
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 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
2060 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
2061 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
2062 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
2063 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
2064
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 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
2066 """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
2067
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 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
2069
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 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
2071 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
2072
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 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
2074 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
2075 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
2076 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
2077
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 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
2079 >>> 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
2080 1
73816
cc559e1e3bd8 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72985
diff changeset
2081 >>> 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
2082 """
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 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
2084 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
2085 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
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
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 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
2089 """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
2090
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 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
2092
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 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
2094
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 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
2096 >>> 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
2097 """
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 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
2099 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
2100 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
2101
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 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
2103 """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
2104
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 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
2106
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 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
2108
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 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
2110 >>> 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
2111 """
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 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
2113 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
2114 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
2115
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 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
2117 """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
2118
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 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
2120
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 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
2122 >>> 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
2123 >>> 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
2124 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
2125 >>> 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
2126 >>> 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
2127 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
2128 """
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 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
2130
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 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
2132 """ 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
2133
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 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
2135 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
2136
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 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
2138 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
2139
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 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
2141 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
2142 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
2143 '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
2144 '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
2145 '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
2146 '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
2147 '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
2148 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
2149 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
2150
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 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
2152 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
2153 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
2154
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 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
2156 >>> 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
2157 """
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 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
2159 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
2160 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
2161 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
2162 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
2163 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
2164 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
2165 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
2166 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
2167 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
2168
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 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
2170 """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
2171
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 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
2173 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
2174 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
2175
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 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
2177 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
2178 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
2179 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
2180 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
2181 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
2182 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
2183 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
2184 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
2185 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
2186 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
2187
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 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
2189 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
2190 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
2191
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 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
2193 >>> 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
2194 >>> 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
2195 ('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
2196 >>> 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
2197 >>> 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
2198 >>> 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
2199 ('#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
2200 """
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 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
2202 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
2203 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
2204 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
2205 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
2206 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
2207 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
2208 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
2209 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
2210 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
2211 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
2212 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
2213 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
2214
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 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
2216 """ 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
2217
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 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
2219 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
2220 - 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
2221 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
2222 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
2223 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
2224 - 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
2225 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
2226 - 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
2227 *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
2228 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
2229 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
2230 - 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
2231 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
2232 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
2233
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 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
2235 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
2236
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 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
2238 >>> 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
2239 >>> 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
2240 >>> 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
2241 >>> 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
2242 '#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
2243 """
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 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
2245 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
2246 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
2247 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
2248 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
2249 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
2250 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
2251
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 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
2253 """ 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
2254
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 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
2256 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
2257 - 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
2258 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
2259 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
2260 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
2261 - 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
2262 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
2263 - 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
2264 *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
2265 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
2266 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
2267 - 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
2268 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
2269 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
2270
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 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
2272 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
2273
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 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
2275 >>> 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
2276 >>> 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
2277 >>> 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
2278 >>> 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
2279 """
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 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
2281 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
2282 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
2283 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
2284 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
2285 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
2286 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
2287
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 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
2289 """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
2290
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 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
2292
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 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
2294
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 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
2296 >>> 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
2297 >>> 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
2298 """
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 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
2300
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 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
2302 """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
2303
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 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
2305
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 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
2307
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 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
2309 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
2310 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
2311
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 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
2313 >>> 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
2314 """
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 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
2316
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 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
2318 """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
2319
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 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
2321
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 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
2323 >>> 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
2324 >>> 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
2325 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
2326 """
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 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
2328
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 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
2330 """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
2331
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 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
2333 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
2334 **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
2335 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
2336
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 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
2338 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
2339 "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
2340 "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
2341 "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
2342 "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
2343 "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
2344 "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
2345 "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
2346 "stretchfactor": (positive number, positive number)
53772
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2347 "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
2348 "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
2349 "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
2350
51765
37e4df9fdaa6 Merged revisions 69821 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 51763
diff changeset
2351 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
2352 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
2353 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
2354 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
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
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 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
2358 >>> 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
2359 >>> 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
2360 {'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
2361 '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
2362 '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
2363 >>> 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
2364 >>> 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
2365 >>> 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
2366 >>> 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
2367 {'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
2368 'pencolor': 'yellow', 'pendown': False, 'fillcolor': '',
53772
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2369 '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
2370 >>> 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
2371 >>> 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
2372 {'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
2373 '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
2374 '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
2375 """
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 _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
2377 "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
2378 "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
2379 "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
2380 "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
2381 "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
2382 "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
2383 "stretchfactor" : self._stretchfactor,
53772
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2384 "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
2385 "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
2386 "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
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
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 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
2390 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
2391
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 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
2393 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
2394 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
2395 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
2396 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
2397
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 _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
2399 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
2400 _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
2401
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 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
2403 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
2404
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 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
2406 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
2407 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
2408 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
2409 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
2410 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
2411 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
2412 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
2413 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
2414 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
2415 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
2416 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
2417 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
2418 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
2419 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
2420 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
2421 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
2422 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
2423 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
2424 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
2425 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
2426 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
2427 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
2428 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
2429 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
2430 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
2431 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
2432 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
2433 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
2434 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
2435 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
2436 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
2437 self._stretchfactor = sf
53772
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2438 if "shearfactor" in p:
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2439 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
2440 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
2441 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
2442 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
2443 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
2444 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
2445 self._tilt = p["tilt"]
53772
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2446 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
2447 scx, scy = self._stretchfactor
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2448 shf = self._shearfactor
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2449 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
2450 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
2451 -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
2452 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
2453
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 ## 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
2455
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 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
2457 """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
2458 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
2459 """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
2460 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
2461 """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
2462 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
2463 """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
2464
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 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
2466 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
2467 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
2468 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
2469 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
2470 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
2471 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
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
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 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
2475 """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
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
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 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
2479 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
2480 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
2481 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
2482
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2483 def _setshape(self, shapeIndex):
53772
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2484 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
2485 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
2486 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
2487 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
2488 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
2489 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
2490 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
2491 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
2492 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
2493 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
2494 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
2495 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
2496 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
2497 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
2498 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
2499 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
2500 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
2501 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
2502 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
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
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 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
2506 """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
2507 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
2508 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
2509 """
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 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
2511
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 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
2513 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
2514 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
2515 visible=_CFG["visible"]):
49345
ab34a647e22f Merged revisions 66686 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 49249
diff changeset
2516 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
2517 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
2518 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
2519 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
2520 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
2521 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
2522 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
2523 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
2524 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
2525 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
2526 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
2527 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
2528 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
2529 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
2530 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
2531 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
2532
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 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
2534 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
2535 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
2536 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
2537 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
2538 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
2539 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
2540 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
2541 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
2542 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
2543 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
2544 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
2545 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
2546 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
2547 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
2548 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
2549 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
2550 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
2551
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2552 def reset(self):
51765
37e4df9fdaa6 Merged revisions 69821 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 51763
diff changeset
2553 """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
2554
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2555 No argument.
73816
cc559e1e3bd8 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72985
diff changeset
2556
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
2557 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
2558 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
2559
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 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
2561 >>> 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
2562 (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
2563 >>> 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
2564 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
2565 >>> 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
2566 >>> 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
2567 (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
2568 >>> 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
2569 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
2570 """
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 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
2572 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
2573 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
2574 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
2575 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
2576
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 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
2578 """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
2579
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 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
2581 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
2582
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 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
2584 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
2585 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
2586 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
2587
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 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
2589 >>> 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
2590 """
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 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
2592 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
2593 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
2594 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
2595
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 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
2597 """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
2598
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 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
2600
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 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
2602 >>> while undobufferentries():
73816
cc559e1e3bd8 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72985
diff changeset
2603 ... 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
2604 """
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 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
2606 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
2607 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
2608
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 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
2610 """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
2611 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
2612 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
2613 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
2614 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
2615 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
2616 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
2617 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
2618 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
2619 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
2620 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
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
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 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
2624 """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
2625
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 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
2627
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 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
2629 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
2630 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
2631
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 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
2633 >>> 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
2634 """
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._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
2636 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
2637
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 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
2639 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
2640 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
2641 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
2642 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
2643 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
2644 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
2645
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 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
2647 """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
2648 """
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 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
2650 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
2651 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
2652 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
2653 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
2654 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
2655 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
2656 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
2657 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
2658 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
2659 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
2660 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
2661 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
2662 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
2663
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 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
2665 """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
2666
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 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
2668 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
2669 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
2670
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 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
2672 (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
2673 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
2674
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 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
2676 >>> 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
2677 >>> 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
2678 >>> for i in range(200):
73816
cc559e1e3bd8 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72985
diff changeset
2679 ... turtle.fd(dist)
cc559e1e3bd8 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72985
diff changeset
2680 ... turtle.rt(90)
cc559e1e3bd8 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72985
diff changeset
2681 ... 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
2682 """
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 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
2684
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 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
2686 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
2687
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 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
2689 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
2690
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 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
2692 """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
2693 """
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 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
2695 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
2696 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
2697 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
2698 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
2699 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
2700 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
2701 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
2702 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
2703 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
2704 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
2705
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 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
2707 """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
2708
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 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
2710
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 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
2712 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
2713
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 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
2715 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
2716 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
2717 """
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 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
2719 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
2720
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 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
2722 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
2723 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
2724
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 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
2726
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.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
2728 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
2729
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.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
2731 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
2732
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 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
2734 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
2735 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
2736 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
2737 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
2738 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
2739 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
2740 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
2741 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
2742 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
2743 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
2744 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
2745
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 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
2747 """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
2748
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 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
2750 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
2751
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 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
2753 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
2754 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
2755 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
2756 '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
2757 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
2758
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 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
2760 >>> 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
2761 '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
2762 >>> 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
2763 >>> 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
2764 '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
2765 """
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 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
2767 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
2768 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
2769 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
2770 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
2771 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
2772
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 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
2774 """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
2775
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 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
2777 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
2778 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
2779 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
2780
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 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
2782 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
2783 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
2784 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
2785 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
2786 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
2787 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
2788
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 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
2790 >>> 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
2791 >>> 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
2792 >>> 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
2793 """
60058
40f787b151ac Merged revisions 79297,79310,79382,79425-79427,79450 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58218
diff changeset
2794 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
2795 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
2796 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
2797 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
2798 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
2799 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
2800 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
2801 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
2802 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
2803 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
2804 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
2805 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
2806 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
2807 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
2808 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
2809 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
2810 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
2811 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
2812
53772
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2813 def shearfactor(self, shear=None):
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2814 """Set or return the current shearfactor.
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2815
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2816 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
2817
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2818 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
2819 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
2820 turtle's heading (direction of movement).
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2821 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
2822 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
2823 heading of the turtle are sheared.
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2824
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2825 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
2826 >>> turtle.shape("circle")
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2827 >>> turtle.shapesize(5,2)
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2828 >>> turtle.shearfactor(0.5)
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2829 >>> turtle.shearfactor()
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2830 >>> 0.5
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2831 """
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2832 if shear is None:
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2833 return self._shearfactor
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2834 self.pen(resizemode="user", shearfactor=shear)
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2835
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
2836 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
2837 """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
2838
53772
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2839 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
2840
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 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
2842 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
2843 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
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
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 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
2847 >>> 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
2848 >>> 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
2849 >>> 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
2850 >>> 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
2851 >>> 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
2852 >>> 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
2853 >>> 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
2854 >>> 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
2855 """
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 = -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
2857 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
2858 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
2859
53772
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2860 def tiltangle(self, angle=None):
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2861 """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
2862
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2863 Optional argument: angle -- number
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2864
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2865 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
2866 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
2867 heading (direction of movement).
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2868 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
2869 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
2870 turtle (its direction of movement).
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2871
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2872 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
2873
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 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
2875 >>> 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
2876 >>> 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
2877 >>> 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
2878 >>> 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
2879 """
53772
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2880 if angle is None:
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2881 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
2882 return (tilt / self._degreesPerAU) % self._fullcircle
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2883 else:
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2884 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
2885
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 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
2887 """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
2888
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 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
2890 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
2891
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 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
2893 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
2894
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 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
2896 >>> 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
2897 >>> 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
2898 >>> 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
2899 >>> 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
2900 >>> 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
2901 >>> 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
2902 """
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 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
2904
53772
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2905 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
2906 """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
2907
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2908 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
2909
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2910 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
2911 matrix.
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2912 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
2913 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
2914 second row t21, 22.
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2915 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
2916 given matrix.
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2917
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2918 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
2919 >>> turtle.shape("square")
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2920 >>> turtle.shapesize(4,2)
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2921 >>> turtle.shearfactor(-0.5)
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2922 >>> turtle.shapetransform()
73816
cc559e1e3bd8 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72985
diff changeset
2923 (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
2924 """
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2925 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
2926 return self._shapetrafo
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2927 m11, m12, m21, m22 = self._shapetrafo
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2928 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
2929 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
2930 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
2931 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
2932 if t11 * t22 - t12 * t21 == 0:
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2933 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
2934 self._shapetrafo = (m11, m12, m21, m22)
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2935 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
2936 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
2937 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
2938 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
2939 self._stretchfactor = a11, a22
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2940 self._shearfactor = a12/a22
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2941 self._tilt = alfa
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2942 self._update()
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2943
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2944
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
2945 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
2946 """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
2947 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
2948 """
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 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
2950 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
2951 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
2952 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
2953 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
2954 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
2955 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
2956
53772
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2957 def get_shapepoly(self):
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2958 """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
2959
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2960 No argument.
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2961
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2962 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
2963 >>> turtle.shape("square")
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2964 >>> turtle.shapetransform(4, -1, 0, 2)
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2965 >>> turtle.get_shapepoly()
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2966 ((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
2967
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2968 """
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2969 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
2970 if shape._type == "polygon":
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2971 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
2972 # else return None
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2973
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2974 def _getshapepoly(self, polygon, compound=False):
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2975 """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
2976 and shapetransform.
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2977 """
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2978 if self._resizemode == "user" or compound:
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2979 t11, t12, t21, t22 = self._shapetrafo
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2980 elif self._resizemode == "auto":
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2981 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
2982 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
2983 elif self._resizemode == "noresize":
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2984 return polygon
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2985 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
2986
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
2987 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
2988 """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
2989 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
2990 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
2991 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
2992 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
2993 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
2994 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
2995 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
2996 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
2997 if ttype == "polygon":
53772
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2998 if self._resizemode == "noresize": w = 1
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2999 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
3000 else: w =self._outlinewidth
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
3001 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
3002 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
3003 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
3004 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
3005 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
3006 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
3007 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
3008 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
3009 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
3010 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
3011 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
3012 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
3013 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
3014 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
3015 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
3016 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
3017 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
3018 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
3019 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
3020 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
3021 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
3022 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
3023 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
3024
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 ############################## 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
3026
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3027 def stamp(self):
51765
37e4df9fdaa6 Merged revisions 69821 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 51763
diff changeset
3028 """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
3029
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 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
3031
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 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
3033 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
3034 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
3035
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 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
3037 >>> 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
3038 >>> 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
3039 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
3040 >>> 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
3041 """
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 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
3043 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
3044 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
3045 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
3046 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
3047 stitem = screen._createpoly()
53772
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
3048 if self._resizemode == "noresize": w = 1
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
3049 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
3050 else: w =self._outlinewidth
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
3051 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
3052 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
3053 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
3054 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
3055 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
3056 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
3057 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
3058 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
3059 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
3060 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
3061 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
3062 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
3063 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
3064 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
3065 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
3066 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
3067 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
3068 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
3069 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
3070 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
3071
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 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
3073 """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
3074 """
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 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
3076 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
3077 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
3078 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
3079 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
3080 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
3081 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
3082 # 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
3083 # 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
3084 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
3085 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
3086 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
3087 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
3088 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
3089 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
3090 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
3091 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
3092 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
3093
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 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
3095 """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
3096
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 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
3098 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
3099
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 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
3101 >>> 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
3102 >>> 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
3103 >>> 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
3104 >>> 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
3105 """
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._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
3107 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
3108
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 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
3110 """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
3111
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 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
3113 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
3114
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 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
3116 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
3117 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
3118
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 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
3120 >>> for i in range(8):
73816
cc559e1e3bd8 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72985
diff changeset
3121 ... 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
3122 ...
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(-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
3125 >>> 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
3126 """
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 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
3128 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
3129 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
3130 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
3131 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
3132 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
3133 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
3134 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
3135 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
3136
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 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
3138 """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
3139 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
3140 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
3141 """
65687
613c7b596fe7 Fixed a typo in a comment.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 65656
diff changeset
3142 ## 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
3143 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
3144 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
3145 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
3146 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
3147 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
3148 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
3149 (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
3150 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
3151 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
3152 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
3153 )
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 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
3155 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
3156 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
3157 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
3158 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
3159 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
3160 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
3161 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
3162 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
3163 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
3164 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
3165 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
3166 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
3167 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
3168 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
3169 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
3170 (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
3171 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
3172 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
3173 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
3174 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
3175 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
3176 # 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
3177 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
3178 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
3179 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
3180 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
3181 ###### 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
3182 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
3183 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
3184 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
3185 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
3186 # 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
3187 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
3188 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
3189
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 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
3191 """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
3192 """
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 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
3194 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
3195 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
3196 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
3197 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
3198 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
3199 # 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
3200 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
3201 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
3202
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 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
3204 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
3205 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
3206 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
3207 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
3208
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 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
3210 (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
3211 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
3212 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
3213 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
3214
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 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
3216 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
3217 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
3218 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
3219 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
3220 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
3221 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
3222 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
3223 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
3224 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
3225 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
3226 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
3227 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
3228 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
3229 (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
3230 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
3231 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
3232 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
3233 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
3234 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
3235 # 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
3236 self._position = old
68585
bf94b6a73fc8 #11565: Fix several typos. Patch by Piotr Kasprzyk.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 68488
diff changeset
3237 ## 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
3238 ## 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
3239 ## 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
3240 ## 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
3241 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
3242 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
3243 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
3244 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
3245 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
3246 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
3247 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
3248 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
3249 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
3250 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
3251 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
3252 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
3253 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
3254
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 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
3256 """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
3257 """
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 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
3259 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
3260 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
3261 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
3262 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
3263 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
3264 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
3265 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
3266 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
3267 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
3268 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
3269 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
3270 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
3271 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
3272
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 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
3274 """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
3275 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
3276 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
3277 """
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 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
3279 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
3280 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
3281 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
3282 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
3283 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
3284 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
3285 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
3286 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
3287 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
3288
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 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
3290 """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
3291
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 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
3293
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 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
3295 >>> 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
3296 >>> if turtle.filling():
73816
cc559e1e3bd8 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72985
diff changeset
3297 ... turtle.pensize(5)
cc559e1e3bd8 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72985
diff changeset
3298 ... else:
cc559e1e3bd8 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72985
diff changeset
3299 ... 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
3300 """
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 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
3302
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 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
3304 """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
3305
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 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
3307
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 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
3309 >>> 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
3310 >>> 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
3311 >>> 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
3312 >>> 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
3313 """
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 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
3315 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
3316 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
3317 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
3318 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
3319 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
3320 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
3321 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
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
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 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
3325 """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
3326
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 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
3328
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 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
3330 >>> 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
3331 >>> 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
3332 >>> 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
3333 >>> 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
3334 """
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 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
3336 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
3337 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
3338 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
3339 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
3340 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
3341 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
3342 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
3343
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 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
3345 """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
3346
68488
0991b40e895d #11515: fix several typos. Patch by Piotr Kasprzyk.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 66407
diff changeset
3347 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
3348 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
3349 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
3350
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 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
3352 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
3353
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 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
3355 >>> 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
3356 >>> 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
3357 """
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 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
3359 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
3360 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
3361 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
3362 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
3363 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
3364 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
3365 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
3366 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
3367 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
3368 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
3369 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
3370 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
3371 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
3372 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
3373 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
3374 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
3375 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
3376 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
3377 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
3378 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
3379 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
3380 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
3381 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
3382 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
3383 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
3384 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
3385 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
3386 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
3387 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
3388 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
3389 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
3390 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
3391
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 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
3393 """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
3394 """
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 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
3396 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
3397 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
3398 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
3399 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
3400 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
3401
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 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
3403 """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
3404
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 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
3406 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
3407 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
3408 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
3409 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
3410
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 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
3412 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
3413 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
3414 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
3415 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
3416
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 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
3418 >>> 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
3419 >>> 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
3420 """
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 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
3422 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
3423 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
3424 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
3425 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
3426 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
3427 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
3428 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
3429 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
3430
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 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
3432 """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
3433
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 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
3435
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 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
3437 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
3438
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 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
3440 >>> 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
3441 """
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._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
3443 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
3444
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 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
3446 """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
3447
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 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
3449
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 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
3451 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
3452
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 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
3454 >>> 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
3455 """
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 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
3457
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 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
3459 """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
3460
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 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
3462
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 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
3464 >>> 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
3465 >>> 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
3466 """
53772
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
3467 ## 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
3468 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
3469 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
3470
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 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
3472 """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
3473
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 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
3475
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 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
3477 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
3478
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 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
3480 >>> 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
3481 >>> 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
3482 <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
3483 >>> 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
3484 """
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 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
3486
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 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
3488 """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
3489
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 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
3491
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3492 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
3493
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 31515
diff changeset
3494 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
3495 >>> 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
3496 >>> 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
3497 >>> 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
3498 <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
3499 >>> 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
3500 [<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
3501 """
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 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
3503
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 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
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 ################################################################
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 ### 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
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
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 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
3512 """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
3513 """
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 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
3515
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 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
3517 """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
3518
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 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
3520 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
3521 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
3522 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
3523 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
3524 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
3525
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 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
3527
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3528 >>> def turn(x, y):
73816
cc559e1e3bd8 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72985
diff changeset
3529 ... left(360)
cc559e1e3bd8 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72985
diff changeset
3530 ...
cc559e1e3bd8 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72985
diff changeset
3531 >>> 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
3532 >>> 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
3533 """
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
3534 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
3535 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
3536
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 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
3538 """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
3539
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 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
3541 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
3542 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
3543 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
3544
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 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
3546 >>> class MyTurtle(Turtle):
73816
cc559e1e3bd8 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72985
diff changeset
3547 ... def glow(self,x,y):
cc559e1e3bd8 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72985
diff changeset
3548 ... self.fillcolor("red")
cc559e1e3bd8 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72985
diff changeset
3549 ... def unglow(self,x,y):
cc559e1e3bd8 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72985
diff changeset
3550 ... self.fillcolor("")
cc559e1e3bd8 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72985
diff changeset
3551 ...
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
3552 >>> 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
3553 >>> 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
3554 >>> joe.onrelease(joe.unglow)
73816
cc559e1e3bd8 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72985
diff changeset
3555
cc559e1e3bd8 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72985
diff changeset
3556 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
3557 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
3558 """
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.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
3560 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
3561
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 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
3563 """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
3564
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 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
3566 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
3567 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
3568 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
3569
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 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
3571 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
3572
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 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
3574 >>> 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
3575
73816
cc559e1e3bd8 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72985
diff changeset
3576 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
3577 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
3578 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
3579 """
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 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
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
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 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
3584 """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
3585 """
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 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
3587 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
3588 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
3589 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
3590 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
3591 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
3592 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
3593 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
3594 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
3595 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
3596 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
3597 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
3598 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
3599 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
3600 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
3601 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
3602 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
3603 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
3604 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
3605 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
3606 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
3607 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
3608 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
3609 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
3610 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
3611 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
3612 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
3613 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
3614
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 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
3616 """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
3617
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 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
3619
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 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
3621 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
3622 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
3623
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 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
3625 >>> for i in range(4):
73816
cc559e1e3bd8 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72985
diff changeset
3626 ... turtle.fd(50); turtle.lt(80)
cc559e1e3bd8 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72985
diff changeset
3627 ...
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
3628 >>> for i in range(8):
73816
cc559e1e3bd8 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72985
diff changeset
3629 ... turtle.undo()
cc559e1e3bd8 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72985
diff changeset
3630 ...
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
3631 """
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 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
3633 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
3634 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
3635 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
3636 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
3637 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
3638 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
3639 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
3640 self._undo(item[0], item[1:])
9585
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
3641 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
3642 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
3643
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 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
3645
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 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
3647
49345
ab34a647e22f Merged revisions 66686 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 49249
diff changeset
3648 ### Screen - Singleton ########################
ab34a647e22f Merged revisions 66686 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 49249
diff changeset
3649
ab34a647e22f Merged revisions 66686 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 49249
diff changeset
3650 def Screen():
ab34a647e22f Merged revisions 66686 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 49249
diff changeset
3651 """Return the singleton screen object.
ab34a647e22f Merged revisions 66686 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 49249
diff changeset
3652 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
3653 else return the existing one."""
ab34a647e22f Merged revisions 66686 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 49249
diff changeset
3654 if Turtle._screen is None:
ab34a647e22f Merged revisions 66686 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 49249
diff changeset
3655 Turtle._screen = _Screen()
ab34a647e22f Merged revisions 66686 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 49249
diff changeset
3656 return Turtle._screen
ab34a647e22f Merged revisions 66686 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 49249
diff changeset
3657
ab34a647e22f Merged revisions 66686 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 49249
diff changeset
3658 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
3659
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 _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
3661 _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
3662 _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
3663
9585
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
3664 def __init__(self):
49345
ab34a647e22f Merged revisions 66686 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 49249
diff changeset
3665 # 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
3666 # 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
3667 # 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
3668 # 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
3669 # 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
3670 if _Screen._root is None:
ab34a647e22f Merged revisions 66686 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 49249
diff changeset
3671 _Screen._root = self._root = _Root()
ab34a647e22f Merged revisions 66686 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 49249
diff changeset
3672 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
3673 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
3674 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
3675 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
3676 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
3677 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
3678 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
3679 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
3680 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
3681 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
3682 _Screen._canvas = self._root._getcanvas()
53772
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
3683 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
3684 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
3685
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 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
3687 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
3688 """ 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
3689
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 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
3691 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
3692 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
3693 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
3694 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
3695 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
3696 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
3697 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
3698 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
3699 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
3700 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
3701
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 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
3703 >>> 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
3704
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 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
3706
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 >>> 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
3708
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 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
3710 """
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 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
3712 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
3713 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
3714 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
3715 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
3716 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
3717 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
3718 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
3719 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
3720 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
3721 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
3722 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
3723 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
3724 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
3725
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 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
3727 """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
3728
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 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
3730 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
3731 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
3732
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 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
3734 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
3735
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 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
3737 >>> 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
3738 """
49345
ab34a647e22f Merged revisions 66686 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 49249
diff changeset
3739 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
3740 _Screen._root.title(titlestring)
ab34a647e22f Merged revisions 66686 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 49249
diff changeset
3741 _Screen._title = titlestring
9585
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
3742
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
3743 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
3744 root = self._root
49345
ab34a647e22f Merged revisions 66686 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 49249
diff changeset
3745 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
3746 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
3747 Turtle._screen = None
49345
ab34a647e22f Merged revisions 66686 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 49249
diff changeset
3748 _Screen._root = None
ab34a647e22f Merged revisions 66686 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 49249
diff changeset
3749 _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
3750 TurtleScreen._RUNNING = True
9585
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
3751 root.destroy()
15308
847ae40074d4 Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents: 14957
diff changeset
3752
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
3753 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
3754 """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
3755
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 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
3757 >>> 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
3758 """
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 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
3760
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 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
3762 """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
3763
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 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
3765
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 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
3767 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
3768 (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
3769 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
3770 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
3771 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
3772
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 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
3774 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
3775
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 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
3777 >>> 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
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 """
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 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
3781 """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
3782 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
3783 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
3784 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
3785 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
3786 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
3787 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
3788 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
3789 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
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
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3792 class Turtle(RawTurtle):
68585
bf94b6a73fc8 #11565: Fix several typos. Patch by Piotr Kasprzyk.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 68488
diff changeset
3793 """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
3794
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 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
3796 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
3797 """
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 _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
3799 _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
3800
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 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
3802 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
3803 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
3804 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
3805 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
3806 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
3807 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
3808 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
3809 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
3810 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
3811
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 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
3813
9585
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
3814 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
3815 """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
3816 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
3817 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
3818 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
3819
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 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
3821 """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
3822 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
3823 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
3824 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
3825
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 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
3827 """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
3828
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 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
3830 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
3831 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
3832
68585
bf94b6a73fc8 #11565: Fix several typos. Patch by Piotr Kasprzyk.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 68488
diff changeset
3833 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
3834 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
3835 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
3836 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
3837 """
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 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
3839
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3840 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
3841 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
3842 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
3843 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
3844 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
3845 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
3846
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 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
3848 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
3849 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
3850 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
3851 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
3852 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
3853 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
3854 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
3855 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
3856 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
3857 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
3858 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
3859
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 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
3861 """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
3862
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 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
3864 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
3865 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
3866 """
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
3867 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
3868 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
3869 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
3870 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
3871 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
3872 # 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
3873 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
3874 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
3875 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
3876
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 _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
3878
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 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
3880 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
3881 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
3882 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
3883 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
3884 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
3885 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
3886 _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
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
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3889 def getmethparlist(ob):
65656
4b542217e93e Issue #10193: Simplified instrospection used by turtle module
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 64405
diff changeset
3890 """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
3891
4b542217e93e Issue #10193: Simplified instrospection used by turtle module
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 64405
diff changeset
3892 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
3893 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
3894 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
3895 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
3896 """
4b542217e93e Issue #10193: Simplified instrospection used by turtle module
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 64405
diff changeset
3897 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
3898 # 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
3899 # 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
3900 # 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
3901 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
3902 items2 = args[1:]
4b542217e93e Issue #10193: Simplified instrospection used by turtle module
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 64405
diff changeset
3903 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
3904 defaults = ob.__defaults__ or []
65656
4b542217e93e Issue #10193: Simplified instrospection used by turtle module
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 64405
diff changeset
3905 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
3906 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
3907 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
3908 if varargs is not None:
4b542217e93e Issue #10193: Simplified instrospection used by turtle module
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 64405
diff changeset
3909 items1.append("*" + varargs)
4b542217e93e Issue #10193: Simplified instrospection used by turtle module
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 64405
diff changeset
3910 items2.append("*" + varargs)
4b542217e93e Issue #10193: Simplified instrospection used by turtle module
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 64405
diff changeset
3911 if varkw is not None:
4b542217e93e Issue #10193: Simplified instrospection used by turtle module
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 64405
diff changeset
3912 items1.append("**" + varkw)
4b542217e93e Issue #10193: Simplified instrospection used by turtle module
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 64405
diff changeset
3913 items2.append("**" + varkw)
4b542217e93e Issue #10193: Simplified instrospection used by turtle module
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 64405
diff changeset
3914 defText = ", ".join(items1)
4b542217e93e Issue #10193: Simplified instrospection used by turtle module
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 64405
diff changeset
3915 defText = "(%s)" % defText
4b542217e93e Issue #10193: Simplified instrospection used by turtle module
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 64405
diff changeset
3916 callText = ", ".join(items2)
4b542217e93e Issue #10193: Simplified instrospection used by turtle module
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 64405
diff changeset
3917 callText = "(%s)" % callText
4b542217e93e Issue #10193: Simplified instrospection used by turtle module
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 64405
diff changeset
3918 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
3919
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 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
3921 """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
3922 """
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 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
3924 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
3925 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
3926 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
3927 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
3928 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
3929 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
3930 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
3931
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 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
3933 """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
3934 """
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
3935 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
3936 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
3937 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
3938 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
3939 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
3940 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
3941 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
3942 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
3943
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 ## 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
3945 ## 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
3946 ## 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
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
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3949 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
3950 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
3951 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
3952 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
3953 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
3954 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
3955 {'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
3956 exec(defstr)
49345
ab34a647e22f Merged revisions 66686 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 49249
diff changeset
3957 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
3958
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 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
3960 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
3961 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
3962 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
3963 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
3964 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
3965 {'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
3966 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
3967 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
3968
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3969
53772
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
3970 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
3971
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 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
3973 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
3974 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
3975 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
3976 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
3977 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
3978
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 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
3980 """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
3981 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
3982 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
3983 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
3984 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
3985 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
3986 # 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
3987 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
3988 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
3989 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
3990 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
3991 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
3992 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
3993 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
3994 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
3995 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
3996 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
3997 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
3998 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
3999 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
4000 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
4001 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
4002 # 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
4003 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
4004 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
4005 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
4006 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
4007 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
4008 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
4009 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
4010 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
4011 # 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
4012 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
4013 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
4014 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
4015 # 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
4016 for i in range(5):
9585
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
4017 forward(20)
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
4018 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
4019 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
4020 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
4021 # 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
4022 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
4023 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
4024 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
4025 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
4026 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
4027 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
4028 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
4029 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
4030 # 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
4031
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 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
4033 """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
4034 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
4035 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
4036 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
4037 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
4038 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
4039 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
4040 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
4041 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
4042 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
4043 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
4044 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
4045 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
4046 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
4047 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
4048 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
4049 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
4050 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
4051 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
4052 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
4053 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
4054 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
4055 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
4056 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
4057 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
4058 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
4059 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
4060 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
4061 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
4062 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
4063 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
4064 #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
4065
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 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
4067 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
4068 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
4069 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
4070 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
4071 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
4072 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
4073 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
4074 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
4075 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
4076 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
4077 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
4078 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
4079 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
4080 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
4081 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
4082 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
4083 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
4084 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
4085
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 = 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
4087 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
4088 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
4089 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
4090 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
4091 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
4092 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
4093 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
4094 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
4095 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
4096 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
4097 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
4098 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
4099 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
4100 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
4101 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
4102 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
4103 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
4104 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
4105 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
4106 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
4107 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
4108 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
4109 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
4110 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
4111 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
4112 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
4113 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
4114 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
4115 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
4116 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
4117
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 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
4119 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
4120 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
4121
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 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
4123
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 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
4125 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
4126 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
4127 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
4128 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
4129 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
4130
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
4131 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
4132 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
4133 exitonclick()