annotate Lib/turtle.py @ 70817:dd775b1561ba

Fix markup.
author Georg Brandl <georg@python.org>
date Tue, 14 Jun 2011 21:09:55 +0200
parents dc245d681901
children bf4f65043d7d 8c17e898e0e8
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
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 31515
diff changeset
30 Imagine a robotic turtle starting at (0, 0) in the x-y plane. Give it
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
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 31515
diff changeset
33 command turtle.left(25), and it rotates in-place 25 degrees clockwise.
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
51765
37e4df9fdaa6 Merged revisions 69821 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 51763
diff changeset
99 extensions in 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")
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
908 ### .. add more components and then use 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
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):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1006 screen.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
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):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1080 left(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
1081 forward(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
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)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1185 >>> turtle.pencolor(240,160,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
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):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1253 fd(dist)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1254 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
1255 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
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):
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1282 "Increment upadate counter."""
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
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1350 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
1351 and 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
1352
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1353 >>> screen.onclick(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
1354
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 ### Subsequently clicking into the TurtleScreen will
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 ### make the turtle move to the clicked 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
1357 >>> 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
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 ### event-binding will be removed
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1360 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1361 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
1362
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 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
1364 """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
1365
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1366 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
1367 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
1368 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
1369
51765
37e4df9fdaa6 Merged revisions 69821 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 51763
diff changeset
1370 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
1371 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
1372
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1373 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
1374 and 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
1375
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 >>> def f():
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 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
1378 lt(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
1379
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1380
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 >>> 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
1382 >>> 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
1383
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1384 ### Subsequently the turtle can be moved by
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 ### repeatedly pressing the up-arrow 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
1386 ### consequently drawing a hexagon
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1387 """
60058
40f787b151ac Merged revisions 79297,79310,79382,79425-79427,79450 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58218
diff changeset
1388 if fun is None:
53772
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
1389 if key in self._keys:
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
1390 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
1391 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
1392 self._keys.append(key)
53772
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
1393 self._onkeyrelease(fun, key)
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
1394
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
1395 def onkeypress(self, fun, key=None):
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
1396 """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
1397 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
1398
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
1399 Arguments:
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
1400 fun -- a function with no arguments
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
1401 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
1402
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
1403 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
1404 must have focus. (See method listen.)
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
1405
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
1406 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
1407 and a Turtle instance named turtle):
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
1408
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
1409 >>> def f():
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
1410 fd(50)
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
1411
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
1412
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
1413 >>> screen.onkey(f, "Up")
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
1414 >>> screen.listen()
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
1415
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
1416 ### Subsequently the turtle can be moved by
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
1417 ### repeatedly pressing the up-arrow key,
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
1418 ### or by keeping pressed the up-arrow key.
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
1419 ### consequently drawing a hexagon.
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
1420 """
60058
40f787b151ac Merged revisions 79297,79310,79382,79425-79427,79450 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58218
diff changeset
1421 if fun is None:
53772
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
1422 if key in self._keys:
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
1423 self._keys.remove(key)
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
1424 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
1425 self._keys.append(key)
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
1426 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
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 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
1429 """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
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 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
1432 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
1433 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
1434
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1435 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
1436 >>> 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
1437 """
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 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
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 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
1441 """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
1442
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 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
1444 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
1445 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
1446
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1447 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
1448
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1449 >>> running = 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
1450 >>> def f():
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 if 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
1452 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
1453 lt(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
1454 screen.ontimer(f, 250)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1455
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1456 >>> f() ### makes the turtle marching around
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 >>> 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
1458 """
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 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
1460
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 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
1462 """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
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 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
1465 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
1466
68488
0991b40e895d #11515: fix several typos. Patch by Piotr Kasprzyk.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 66407
diff changeset
1467 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
1468 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
1469 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
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 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
1472 >>> 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
1473 '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
1474 >>> 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
1475 >>> 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
1476 '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
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 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
1479 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
1480 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
1481 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
1482 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
1483 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
1484
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 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
1486 """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
1487
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 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
1489 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
1490 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
1491 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
1492 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
1493
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1494 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
1495 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
1496 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
1497
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 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
1499 >>> turtle.screensize(2000,1500)
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 ### e. g. to search for an erroneously escaped 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
1501 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1502 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
1503
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 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
1505 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
1506 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
1507 addshape = register_shape
53772
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
1508 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
1509
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 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
1511 """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
1512 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
1513 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1514 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
1515 "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
1516 "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
1517 "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
1518 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
1519 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
1520 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
1521
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 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
1523 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
1524 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
1525 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
1526 self.undobuffer = None
9585
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
1527 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
1528 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
1529 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
1530 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
1531
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1532 def 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
1533 """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
1534
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 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
1536 """
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 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
1538 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
1539
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 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
1541 """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
1542 """
60058
40f787b151ac Merged revisions 79297,79310,79382,79425-79427,79450 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58218
diff changeset
1543 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
1544 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
1545 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
1546 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
1547 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
1548 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
1549 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
1550 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
1551 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
1552 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
1553 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
1554
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 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
1556 """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
1557 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
1558 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
1559 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
1560 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
1561 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
1562 self._angleOffset = fullcircle/4.
9585
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
1563
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
1564 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
1565 """ 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
1566
47538
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 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
1568 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
1569
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 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
1571 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
1572 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
1573
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1574 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
1575 >>> 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
1576 >>> 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
1577 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
1578
7ab72ba38fe0 Merged revisions 85732,85778,85785,85853,85930,86008,86089 via svnmerge from
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 60058
diff changeset
1579 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
1580 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
1581 >>> 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
1582 >>> 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
1583 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
1584
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 31515
diff changeset
1585 """
47538
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 self._setDegreesPerAU(fullcircle)
9585
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
1587
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
1588 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
1589 """ 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
1590
47538
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 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
1592
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 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
1594 >>> 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
1595 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
1596 >>> 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
1597 >>> 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
1598 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
1599 """
47538
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 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
1601
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 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
1603 """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
1604 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
1605 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
1606
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 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
1608 """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
1609 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
1610 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
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 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
1613 """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
1614 self._position = end
9585
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
1615
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
1616 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
1617 """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
1618
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1619 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
1620
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1621 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
1622 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
1623
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1624 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
1625 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
1626
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1627 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
1628 >>> 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
1629 (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
1630 >>> 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
1631 >>> 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
1632 (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
1633 >>> 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
1634 >>> 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
1635 (-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
1636 """
47538
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 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
1638
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1639 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
1640 """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
1641
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1642 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
1643
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1644 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
1645 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
1646
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1647 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
1648 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
1649
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 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
1651 >>> 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
1652 (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
1653 >>> 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
1654 >>> 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
1655 (-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
1656 """
47538
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 self._go(-distance)
9585
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
1658
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
1659 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
1660 """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
1661
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 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
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 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
1665 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
1666
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1667 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
1668 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
1669 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
1670
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 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
1672 >>> 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
1673 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
1674 >>> 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
1675 >>> 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
1676 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
1677 """
47538
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 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
1679
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1680 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
1681 """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
1682
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 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
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 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
1686 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
1687
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 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
1689 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
1690 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
1691
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 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
1693 >>> 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
1694 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
1695 >>> 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
1696 >>> 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
1697 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
1698 """
47538
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 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
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 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
1702 """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
1703
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1704 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
1705
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1706 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
1707
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 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
1709 >>> 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
1710 (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
1711 """
47538
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 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
1713
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 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
1715 """ 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
1716
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 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
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 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
1720 >>> 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
1721 >>> 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
1722 >>> 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
1723 >>> 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
1724 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
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 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
1727
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 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
1729 """ 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
1730 ---
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 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
1732
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 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
1734 >>> 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
1735 >>> 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
1736 >>> 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
1737 >>> 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
1738 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
1739 """
47538
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 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
1741
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
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 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
1744 """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
1745
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 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
1747
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1748 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
1749 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
1750 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
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 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
1753 --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
1754 --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
1755
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 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
1757 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
1758
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 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
1760 >>> 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
1761 >>> tp
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1762 (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
1763 >>> 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
1764 >>> 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
1765 (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
1766 >>> 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
1767 >>> 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
1768 (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
1769 >>> 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
1770 >>> 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
1771 (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
1772 """
47538
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 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
1774 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
1775 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
1776 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
1777
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1778 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
1779 """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
1780
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1781 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
1782
51765
37e4df9fdaa6 Merged revisions 69821 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 51763
diff changeset
1783 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
1784 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
1785
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 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
1787 >>> 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
1788 """
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 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
1790 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
1791
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 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
1793 """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
1794
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1795 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
1796 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
1797
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1798 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
1799 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
1800
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 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
1802 >>> 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
1803 (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
1804 >>> 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
1805 >>> 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
1806 (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
1807 """
47538
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 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
1809
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 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
1811 """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
1812
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1813 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
1814 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
1815
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1816 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
1817 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
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 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
1820 >>> 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
1821 (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
1822 >>> 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
1823 >>> 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
1824 (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
1825 """
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 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
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 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
1829 """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
1830
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 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
1832 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
1833 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
1834
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 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
1836 --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
1837 --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
1838 --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
1839
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 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
1841 >>> 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
1842 (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
1843 >>> 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
1844 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
1845 >>> 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
1846 >>> 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
1847 >>> 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
1848 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
1849 """
47538
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 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
1851 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
1852 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
1853 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
1854 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
1855 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
1856 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
1857 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
1858 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
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 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
1861 """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
1862
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 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
1864 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
1865 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
1866
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 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
1868 --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
1869 --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
1870 --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
1871
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1872 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
1873 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
1874 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
1875
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 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
1877 >>> 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
1878 (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
1879 >>> 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
1880 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
1881 """
47538
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 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
1883 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
1884 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
1885 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
1886 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
1887 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
1888 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
1889 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
1890 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
1891 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
1892 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
1893 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
1894
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 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
1896 """ 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
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 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
1899
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 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
1901 >>> 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
1902 >>> 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
1903 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
1904 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1905 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
1906 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
1907 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
1908 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
1909
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1910 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
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
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 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
1914
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1915 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
1916 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
1917
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 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
1919 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
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 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
1922 -------------------|--------------------
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 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
1924 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
1925 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
1926 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
1927
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 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
1929 >>> 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
1930 >>> 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
1931 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
1932 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1933 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
1934 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
1935 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
1936 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
1937
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1938 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
1939 """ 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
1940
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 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
1942 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
1943 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
1944 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
1945
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
1946 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
1947 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
1948 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
1949 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
1950 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
1951 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
1952 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
1953
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 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
1955 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
1956 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
1957 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
1958
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 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
1960 --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
1961 --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
1962 --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
1963
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 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
1965 >>> 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
1966 >>> 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
1967 """
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 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
1969 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
1970 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
1971 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
1972 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
1973 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
1974 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
1975 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
1976 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
1977 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
1978 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
1979 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
1980 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
1981 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
1982 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
1983 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
1984 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
1985 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
1986 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
1987 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
1988 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
1989 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
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 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
1992 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
1993 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
1994 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
1995 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
1996 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
1997 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
1998 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
1999 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
2000
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 ## 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
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 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
2004 """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
2005 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
2006 """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
2007 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
2008 """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
2009
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 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
2011 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
2012 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
2013 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
2014 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
2015 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
2016 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
2017 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
2018 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
2019
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
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 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
2022 """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
2023 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
2024 """
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 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
2026 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
2027 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
2028 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
2029
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 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
2031 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
2032 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
2033 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
2034 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
2035 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
2036 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
2037 self._speed = 3
53772
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2038 self._stretchfactor = (1., 1.)
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2039 self._shearfactor = 0.
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2040 self._tilt = 0.
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2041 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
2042 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
2043
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 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
2045 """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
2046
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 (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
2048 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
2049
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 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
2051 - "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
2052 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
2053 - "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
2054 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
2055 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
2056 - "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
2057 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
2058 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
2059
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
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 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
2062 >>> 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
2063 >>> 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
2064 '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
2065 """
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 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
2067 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
2068 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
2069 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
2070 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
2071
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2072 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
2073 """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
2074
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 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
2076
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2077 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
2078 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
2079
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 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
2081 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
2082 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
2083 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
2084
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 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
2086 >>> 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
2087 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
2088 turtle.pensize(10) # from here on lines of width 10 are 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
2089 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2090 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
2091 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
2092 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
2093
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2094
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 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
2096 """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
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 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
2099
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 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
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 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
2103 >>> 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
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 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
2106 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
2107 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
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 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
2110 """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
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 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
2113
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 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
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 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
2117 >>> 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
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 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
2120 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
2121 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
2122
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 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
2124 """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
2125
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 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
2127
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2128 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
2129 >>> 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
2130 >>> 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
2131 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
2132 >>> 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
2133 >>> 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
2134 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
2135 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2136 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
2137
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 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
2139 """ 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
2140
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 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
2142 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
2143
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 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
2145 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
2146
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 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
2148 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
2149 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
2150 '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
2151 '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
2152 '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
2153 '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
2154 '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
2155 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
2156 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
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 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
2159 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
2160 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
2161
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 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
2163 >>> 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
2164 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2165 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
2166 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
2167 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
2168 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
2169 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
2170 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
2171 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
2172 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
2173 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
2174 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
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 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
2177 """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
2178
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 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
2180 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
2181 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
2182
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()
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 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
2185 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
2186 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
2187 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
2188 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
2189 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
2190 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
2191 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
2192 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
2193 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
2194
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 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
2196 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
2197 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
2198
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 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
2200 >>> 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
2201 >>> 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
2202 ('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
2203 >>> 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
2204 >>> 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
2205 >>> 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
2206 ('#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
2207 """
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 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
2209 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
2210 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
2211 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
2212 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
2213 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
2214 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
2215 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
2216 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
2217 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
2218 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
2219 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
2220 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
2221
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 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
2223 """ 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
2224
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 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
2226 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
2227 - 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
2228 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
2229 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
2230 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
2231 - 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
2232 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
2233 - 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
2234 *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
2235 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
2236 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
2237 - 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
2238 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
2239 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
2240
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 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
2242 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
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 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
2245 >>> 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
2246 >>> 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
2247 >>> 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
2248 >>> 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
2249 '#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
2250 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2251 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
2252 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
2253 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
2254 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
2255 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
2256 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
2257 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
2258
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 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
2260 """ 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
2261
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 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
2263 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
2264 - 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
2265 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
2266 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
2267 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
2268 - 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
2269 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
2270 - 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
2271 *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
2272 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
2273 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
2274 - 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
2275 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
2276 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
2277
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 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
2279 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
2280
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 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
2282 >>> 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
2283 >>> 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
2284 >>> 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
2285 >>> 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
2286 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2287 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
2288 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
2289 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
2290 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
2291 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
2292 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
2293 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
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 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
2296 """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
2297
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2298 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
2299
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2300 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
2301
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 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
2303 >>> 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
2304 >>> 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
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 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
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 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
2309 """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
2310
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2311 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
2312
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 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
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 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
2316 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
2317 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
2318
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2319 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
2320 >>> 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
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 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
2323
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 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
2325 """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
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 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
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 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
2330 >>> 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
2331 >>> 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
2332 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
2333 """
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 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
2335
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2336 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
2337 """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
2338
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 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
2340 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
2341 **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
2342 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
2343
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 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
2345 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
2346 "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
2347 "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
2348 "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
2349 "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
2350 "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
2351 "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
2352 "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
2353 "stretchfactor": (positive number, positive number)
53772
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2354 "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
2355 "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
2356 "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
2357
51765
37e4df9fdaa6 Merged revisions 69821 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 51763
diff changeset
2358 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
2359 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
2360 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
2361 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
2362
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
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 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
2365 >>> 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
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': 'red', 'pendown': True, 'fillcolor': 'black',
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 >>> 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
2371 >>> 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
2372 >>> 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
2373 >>> 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
2374 {'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
2375 'pencolor': 'yellow', 'pendown': False, 'fillcolor': '',
53772
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2376 '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
2377 >>> 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
2378 >>> 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
2379 {'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
2380 '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
2381 '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
2382 """
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 _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
2384 "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
2385 "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
2386 "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
2387 "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
2388 "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
2389 "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
2390 "stretchfactor" : self._stretchfactor,
53772
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2391 "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
2392 "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
2393 "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
2394 }
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
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 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
2397 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
2398
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 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
2400 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
2401 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
2402 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
2403 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
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 _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
2406 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
2407 _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
2408
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 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
2410 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
2411
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2412 newLine = 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
2413 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
2414 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
2415 newLine = True
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2416 if "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
2417 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
2418 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
2419 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
2420 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
2421 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
2422 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
2423 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
2424 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
2425 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
2426 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
2427 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
2428 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
2429 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
2430 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
2431 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
2432 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
2433 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
2434 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
2435 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
2436 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
2437 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
2438 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
2439 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
2440 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
2441 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
2442 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
2443 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
2444 self._stretchfactor = sf
53772
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2445 if "shearfactor" in p:
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2446 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
2447 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
2448 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
2449 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
2450 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
2451 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
2452 self._tilt = p["tilt"]
53772
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2453 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
2454 scx, scy = self._stretchfactor
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2455 shf = self._shearfactor
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2456 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
2457 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
2458 -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
2459 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
2460
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 ## 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
2462
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 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
2464 """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
2465 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
2466 """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
2467 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
2468 """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
2469 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
2470 """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
2471
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2472 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
2473 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
2474 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
2475 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
2476 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
2477 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
2478 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
2479
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
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 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
2482 """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
2483 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2484
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 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
2486 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
2487 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
2488 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
2489
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 def _setshape(self, shapeIndex):
53772
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2491 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
2492 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
2493 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
2494 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
2495 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
2496 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
2497 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
2498 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
2499 elif self._type == "compound":
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2500 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
2501 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
2502 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
2503 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
2504 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
2505 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
2506 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
2507 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
2508 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
2509 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
2510
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2511
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 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
2513 """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
2514 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
2515 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
2516 """
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 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
2518
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 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
2520 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
2521 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
2522 visible=_CFG["visible"]):
49345
ab34a647e22f Merged revisions 66686 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 49249
diff changeset
2523 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
2524 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
2525 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
2526 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
2527 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
2528 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
2529 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
2530 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
2531 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
2532 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
2533 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
2534 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
2535 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
2536 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
2537 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
2538 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
2539
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 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
2541 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
2542 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
2543 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
2544 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
2545 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
2546 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
2547 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
2548 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
2549 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
2550 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
2551 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
2552 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
2553 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
2554 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
2555 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
2556 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
2557 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
2558
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2559 def reset(self):
51765
37e4df9fdaa6 Merged revisions 69821 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 51763
diff changeset
2560 """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
2561
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 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
2563 ,
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 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
2565 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
2566
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 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
2568 >>> 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
2569 (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
2570 >>> 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
2571 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
2572 >>> 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
2573 >>> 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
2574 (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
2575 >>> 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
2576 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
2577 """
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 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
2579 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
2580 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
2581 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
2582 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
2583
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 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
2585 """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
2586
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2587 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
2588 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
2589
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2590 If size is 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
2591 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
2592 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
2593 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
2594
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2595 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
2596 >>> 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
2597 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2598 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
2599 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
2600 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
2601 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
2602
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2603 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
2604 """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
2605
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 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
2607
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2608 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
2609 >>> 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
2610 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
2611 """
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 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
2613 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
2614 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
2615
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 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
2617 """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
2618 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
2619 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
2620 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
2621 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
2622 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
2623 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
2624 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
2625 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
2626 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
2627 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
2628
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
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 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
2631 """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
2632
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 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
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 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
2636 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
2637 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
2638
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 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
2640 >>> 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
2641 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2642 self._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
2643 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
2644
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2645 def _update_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
2646 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
2647 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
2648 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
2649 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
2650 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
2651 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
2652
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 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
2654 """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
2655 """
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 = 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
2657 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
2658 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
2659 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
2660 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
2661 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
2662 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
2663 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
2664 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
2665 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
2666 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
2667 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
2668 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
2669 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
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 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
2672 """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
2673
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2674 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
2675 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
2676 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
2677
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 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
2679 (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
2680 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
2681
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2682 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
2683 >>> 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
2684 >>> 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
2685 >>> for i in range(200):
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 turtle.fd(dist)
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 turtle.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
2688 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
2689 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2690 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
2691
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 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
2693 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
2694
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 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
2696 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
2697
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 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
2699 """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
2700 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2701 if 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
2702 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
2703 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
2704 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
2705 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
2706 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
2707 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
2708 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
2709 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
2710 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
2711 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
2712
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2713 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
2714 """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
2715
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 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
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 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
2719 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
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 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
2722 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
2723 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
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 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
2726 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
2727
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 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
2729 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
2730 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
2731
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2732 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
2733
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 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
2735 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
2736
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2737 q.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
2738 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
2739
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2740 screen._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
2741 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
2742 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
2743 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
2744 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
2745 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
2746 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
2747 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
2748 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
2749 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
2750 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
2751 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
2752
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 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
2754 """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
2755
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 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
2757 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
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 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
2760 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
2761 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
2762 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
2763 '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
2764 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
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 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
2767 >>> 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
2768 '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
2769 >>> 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
2770 >>> 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
2771 '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
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 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
2774 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
2775 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
2776 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
2777 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
2778 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
2779
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2780 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
2781 """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
2782
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 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
2784 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
2785 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
2786 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
2787
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2788 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
2789 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
2790 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
2791 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
2792 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
2793 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
2794 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
2795
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 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
2797 >>> 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
2798 >>> 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
2799 >>> 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
2800 """
60058
40f787b151ac Merged revisions 79297,79310,79382,79425-79427,79450 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58218
diff changeset
2801 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
2802 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
2803 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
2804 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
2805 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
2806 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
2807 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
2808 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
2809 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
2810 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
2811 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
2812 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
2813 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
2814 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
2815 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
2816 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
2817 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
2818 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
2819
53772
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2820 def shearfactor(self, shear=None):
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2821 """Set or return the current shearfactor.
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2822
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2823 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
2824
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2825 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
2826 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
2827 turtle's heading (direction of movement).
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2828 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
2829 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
2830 heading of the turtle are sheared.
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 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
2833 >>> turtle.shape("circle")
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2834 >>> turtle.shapesize(5,2)
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2835 >>> turtle.shearfactor(0.5)
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2836 >>> turtle.shearfactor()
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2837 >>> 0.5
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2838 """
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2839 if shear is None:
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2840 return self._shearfactor
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2841 self.pen(resizemode="user", shearfactor=shear)
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2842
47538
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 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
2844 """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
2845
53772
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2846 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
2847
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 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
2849 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
2850 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
2851
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
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 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
2854 >>> 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
2855 >>> 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
2856 >>> 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
2857 >>> 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
2858 >>> 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
2859 >>> 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
2860 >>> 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
2861 >>> 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
2862 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2863 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
2864 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
2865 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
2866
53772
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2867 def tiltangle(self, angle=None):
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2868 """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
2869
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2870 Optional argument: angle -- number
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 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
2873 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
2874 heading (direction of movement).
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2875 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
2876 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
2877 turtle (its direction of movement).
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2878
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2879 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
2880
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2881 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
2882 >>> 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
2883 >>> 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
2884 >>> 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
2885 >>> 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
2886 >>>
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 """
53772
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2888 if angle is None:
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2889 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
2890 return (tilt / self._degreesPerAU) % self._fullcircle
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2891 else:
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2892 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
2893
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2894 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
2895 """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
2896
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 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
2898 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
2899
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 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
2901 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
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 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
2904 >>> 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
2905 >>> 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
2906 >>> 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
2907 >>> 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
2908 >>> 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
2909 >>> 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
2910 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2911 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
2912
53772
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2913 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
2914 """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
2915
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2916 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
2917
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2918 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
2919 matrix.
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2920 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
2921 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
2922 second row t21, 22.
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2923 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
2924 given matrix.
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2925
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2926 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
2927 >>> turtle.shape("square")
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2928 >>> turtle.shapesize(4,2)
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2929 >>> turtle.shearfactor(-0.5)
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2930 >>> turtle.shapetransform()
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2931 >>> (4.0, -1.0, -0.0, 2.0)
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2932 """
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2933 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
2934 return self._shapetrafo
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2935 m11, m12, m21, m22 = self._shapetrafo
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2936 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
2937 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
2938 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
2939 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
2940 if t11 * t22 - t12 * t21 == 0:
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2941 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
2942 self._shapetrafo = (m11, m12, m21, m22)
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2943 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
2944 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
2945 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
2946 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
2947 self._stretchfactor = a11, a22
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2948 self._shearfactor = a12/a22
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2949 self._tilt = alfa
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2950 self._update()
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2951
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2952
47538
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 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
2954 """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
2955 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
2956 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
2957 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
2958 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
2959 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
2960 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
2961 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
2962 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
2963 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
2964
53772
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2965 def get_shapepoly(self):
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2966 """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
2967
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2968 No argument.
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2969
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2970 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
2971 >>> turtle.shape("square")
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2972 >>> turtle.shapetransform(4, -1, 0, 2)
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2973 >>> turtle.get_shapepoly()
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2974 ((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
2975
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2976 """
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2977 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
2978 if shape._type == "polygon":
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2979 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
2980 # else return None
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2981
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2982 def _getshapepoly(self, polygon, compound=False):
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2983 """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
2984 and shapetransform.
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2985 """
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2986 if self._resizemode == "user" or compound:
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2987 t11, t12, t21, t22 = self._shapetrafo
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2988 elif self._resizemode == "auto":
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2989 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
2990 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
2991 elif self._resizemode == "noresize":
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2992 return polygon
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
2993 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
2994
47538
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 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
2996 """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
2997 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
2998 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
2999 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
3000 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
3001 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
3002 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
3003 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
3004 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
3005 if ttype == "polygon":
53772
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
3006 if self._resizemode == "noresize": w = 1
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
3007 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
3008 else: w =self._outlinewidth
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
3009 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
3010 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
3011 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
3012 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
3013 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
3014 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
3015 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
3016 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
3017 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
3018 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
3019 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
3020 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
3021 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
3022 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
3023 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
3024 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
3025 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
3026 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
3027 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
3028 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
3029 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
3030 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
3031 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
3032
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 ############################## 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
3034
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3035 def stamp(self):
51765
37e4df9fdaa6 Merged revisions 69821 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 51763
diff changeset
3036 """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
3037
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 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
3039
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 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
3041 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
3042 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
3043
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 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
3045 >>> 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
3046 >>> 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
3047 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
3048 >>> 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
3049 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3050 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
3051 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
3052 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
3053 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
3054 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
3055 stitem = screen._createpoly()
53772
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
3056 if self._resizemode == "noresize": w = 1
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
3057 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
3058 else: w =self._outlinewidth
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
3059 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
3060 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
3061 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
3062 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
3063 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
3064 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
3065 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
3066 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
3067 stitem = []
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3068 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
3069 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
3070 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
3071 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
3072 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
3073 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
3074 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
3075 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
3076 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
3077 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
3078 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
3079
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 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
3081 """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
3082 """
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 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
3084 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
3085 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
3086 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
3087 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
3088 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
3089 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
3090 # 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
3091 # 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
3092 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
3093 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
3094 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
3095 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
3096 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
3097 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
3098 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
3099 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
3100 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
3101
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 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
3103 """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
3104
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3105 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
3106 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
3107
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3108 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
3109 >>> 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
3110 >>> 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
3111 >>> 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
3112 >>> 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
3113 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3114 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
3115 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
3116
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 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
3118 """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
3119
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 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
3121 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
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 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
3124 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
3125 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
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 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
3128 >>> for i in range(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
3129 turtle.stamp(); turtle.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
3130 ...
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 >>> 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
3132 >>> 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
3133 >>> 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
3134 """
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 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
3136 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
3137 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
3138 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
3139 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
3140 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
3141 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
3142 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
3143 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
3144
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 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
3146 """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
3147 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
3148 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
3149 """
65687
613c7b596fe7 Fixed a typo in a comment.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 65656
diff changeset
3150 ## 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
3151 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
3152 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
3153 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
3154 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
3155 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
3156 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
3157 (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
3158 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
3159 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
3160 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
3161 )
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3162 if 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
3163 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
3164 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
3165 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
3166 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
3167 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
3168 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
3169 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
3170 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
3171 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
3172 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
3173 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
3174 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
3175 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
3176 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
3177 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
3178 (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
3179 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
3180 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
3181 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
3182 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
3183 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
3184 # 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
3185 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
3186 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
3187 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
3188 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
3189 ###### 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
3190 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
3191 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
3192 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
3193 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
3194 # 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
3195 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
3196 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
3197
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 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
3199 """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
3200 """
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 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
3202 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
3203 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
3204 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
3205 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
3206 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
3207 # 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
3208 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
3209 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
3210
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 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
3212 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
3213 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
3214 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
3215 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
3216
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 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
3218 (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
3219 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
3220 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
3221 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
3222
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 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
3224 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
3225 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
3226 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
3227 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
3228 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
3229 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
3230 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
3231 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
3232 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
3233 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
3234 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
3235 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
3236 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
3237 (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
3238 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
3239 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
3240 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
3241 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
3242 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
3243 # 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
3244 self._position = old
68585
bf94b6a73fc8 #11565: Fix several typos. Patch by Piotr Kasprzyk.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 68488
diff changeset
3245 ## 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
3246 ## 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
3247 ## 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
3248 ## 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
3249 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
3250 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
3251 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
3252 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
3253 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
3254 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
3255 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
3256 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
3257 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
3258 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
3259 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
3260 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
3261 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
3262
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 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
3264 """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
3265 """
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 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
3267 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
3268 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
3269 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
3270 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
3271 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
3272 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
3273 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
3274 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
3275 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
3276 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
3277 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
3278 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
3279 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
3280
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 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
3282 """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
3283 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
3284 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
3285 """
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 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
3287 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
3288 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
3289 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
3290 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
3291 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
3292 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
3293 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
3294 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
3295 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
3296
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3297 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
3298 """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
3299
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3300 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
3301
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3302 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
3303 >>> 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
3304 >>> if turtle.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
3305 turtle.pensize(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
3306 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
3307 turtle.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
3308 """
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 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
3310
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 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
3312 """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
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 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
3315
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 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
3317 >>> 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
3318 >>> 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
3319 >>> 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
3320 >>> 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
3321 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3322 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
3323 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
3324 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
3325 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
3326 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
3327 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
3328 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
3329 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
3330
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
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 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
3333 """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
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 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
3336
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 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
3338 >>> 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
3339 >>> 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
3340 >>> 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
3341 >>> 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
3342 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3343 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
3344 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
3345 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
3346 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
3347 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
3348 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
3349 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
3350 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
3351
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 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
3353 """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
3354
68488
0991b40e895d #11515: fix several typos. Patch by Piotr Kasprzyk.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 66407
diff changeset
3355 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
3356 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
3357 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
3358
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 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
3360 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
3361
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 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
3363 >>> 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
3364 >>> 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
3365 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3366 if 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
3367 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
3368 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
3369 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
3370 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
3371 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
3372 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
3373 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
3374 else:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3375 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
3376 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
3377 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
3378 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
3379 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
3380 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
3381 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
3382 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
3383 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
3384 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
3385 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
3386 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
3387 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
3388 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
3389 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
3390 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
3391 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
3392 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
3393 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
3394 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
3395 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
3396 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
3397 if self.undobuffer:
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3398 self.undobuffer.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
3399
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 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
3401 """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
3402 """
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 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
3404 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
3405 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
3406 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
3407 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
3408 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
3409
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3410 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
3411 """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
3412
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 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
3414 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
3415 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
3416 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
3417 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
3418
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 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
3420 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
3421 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
3422 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
3423 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
3424
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 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
3426 >>> 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
3427 >>> 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
3428 """
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 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
3430 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
3431 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
3432 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
3433 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
3434 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
3435 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
3436 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
3437 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
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 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
3440 """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
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 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
3443
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3444 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
3445 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
3446
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3447 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
3448 >>> 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
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 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
3451 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
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 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
3454 """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
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 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
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 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
3459 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
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 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
3462 >>> 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
3463 """
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 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
3465
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 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
3467 """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
3468
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 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
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 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
3472 >>> 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
3473 >>> 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
3474 """
53772
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
3475 ## 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
3476 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
3477 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
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 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
3480 """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
3481
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 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
3483
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3484 Return 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
3485 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
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 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
3488 >>> 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
3489 >>> 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
3490 <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
3491 >>> 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
3492 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3493 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
3494
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 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
3496 """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
3497
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 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
3499
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 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
3501
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 31515
diff changeset
3502 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
3503 >>> 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
3504 >>> 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
3505 >>> 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
3506 <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
3507 >>> 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
3508 [<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
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 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
3511
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 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
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
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 ### 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
3517 ################################################################
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3518
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 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
3520 """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
3521 """
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 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
3523
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 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
3525 """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
3526
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3527 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
3528 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
3529 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
3530 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
3531 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
3532 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
3533
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 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
3535
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3536 >>> def turn(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
3537 left(360)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3538
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3539 >>> onclick(turn) # Now clicking into the turtle will turn 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
3540 >>> 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
3541 """
47538
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 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
3543 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
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 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
3546 """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
3547
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3548 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
3549 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
3550 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
3551 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
3552
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 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
3554 >>> class MyTurtle(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
3555 def glow(self,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
3556 self.fillcolor("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
3557 def unglow(self,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
3558 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
3559
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 >>> 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
3561 >>> 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
3562 >>> joe.onrelease(joe.unglow)
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 ### clicking on joe turns fillcolor 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
3564 ### unclicking turns it to transparent.
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 """
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 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
3567 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
3568
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3569 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
3570 """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
3571
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3572 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
3573 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
3574 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
3575 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
3576
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3577 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
3578 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
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 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
3581 >>> 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
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 ### Subsequently clicking and dragging a Turtle will
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 ### move it across the screen thereby producing handdrawings
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3585 ### (if 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
3586 """
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 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
3588
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
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 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
3591 """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
3592 """
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 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
3594 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
3595 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
3596 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
3597 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
3598 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
3599 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
3600 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
3601 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
3602 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
3603 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
3604 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
3605 item = data[0]
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3606 self.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
3607 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
3608 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
3609 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
3610 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
3611 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
3612 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
3613 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
3614 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
3615 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
3616 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
3617 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
3618 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
3619 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
3620 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
3621
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 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
3623 """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
3624
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 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
3626
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3627 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
3628 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
3629 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
3630
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3631 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
3632 >>> for i 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
3633 turtle.fd(50); turtle.lt(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
3634
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 >>> for i in range(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
3636 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
3637 """
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 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
3639 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
3640 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
3641 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
3642 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
3643 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
3644 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
3645 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
3646 self._undo(item[0], item[1:])
9585
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
3647 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
3648 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
3649
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3650 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
3651
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3652 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
3653
49345
ab34a647e22f Merged revisions 66686 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 49249
diff changeset
3654 ### Screen - Singleton ########################
ab34a647e22f Merged revisions 66686 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 49249
diff changeset
3655
ab34a647e22f Merged revisions 66686 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 49249
diff changeset
3656 def Screen():
ab34a647e22f Merged revisions 66686 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 49249
diff changeset
3657 """Return the singleton screen object.
ab34a647e22f Merged revisions 66686 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 49249
diff changeset
3658 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
3659 else return the existing one."""
ab34a647e22f Merged revisions 66686 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 49249
diff changeset
3660 if Turtle._screen is None:
ab34a647e22f Merged revisions 66686 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 49249
diff changeset
3661 Turtle._screen = _Screen()
ab34a647e22f Merged revisions 66686 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 49249
diff changeset
3662 return Turtle._screen
ab34a647e22f Merged revisions 66686 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 49249
diff changeset
3663
ab34a647e22f Merged revisions 66686 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 49249
diff changeset
3664 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
3665
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3666 _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
3667 _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
3668 _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
3669
9585
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
3670 def __init__(self):
49345
ab34a647e22f Merged revisions 66686 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 49249
diff changeset
3671 # 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
3672 # 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
3673 # 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
3674 # 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
3675 # 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
3676 if _Screen._root is None:
ab34a647e22f Merged revisions 66686 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 49249
diff changeset
3677 _Screen._root = self._root = _Root()
ab34a647e22f Merged revisions 66686 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 49249
diff changeset
3678 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
3679 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
3680 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
3681 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
3682 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
3683 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
3684 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
3685 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
3686 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
3687 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
3688 _Screen._canvas = self._root._getcanvas()
53772
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
3689 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
3690 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
3691
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 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
3693 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
3694 """ 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
3695
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 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
3697 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
3698 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
3699 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
3700 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
3701 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
3702 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
3703 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
3704 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
3705 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
3706 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
3707
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3708 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
3709 >>> 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
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 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
3712
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 >>> 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
3714
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 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
3716 """
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 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
3718 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
3719 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
3720 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
3721 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
3722 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
3723 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
3724 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
3725 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
3726 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
3727 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
3728 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
3729 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
3730 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
3731
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3732 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
3733 """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
3734
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3735 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
3736 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
3737 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
3738
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3739 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
3740 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
3741
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3742 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
3743 >>> 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
3744 """
49345
ab34a647e22f Merged revisions 66686 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 49249
diff changeset
3745 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
3746 _Screen._root.title(titlestring)
ab34a647e22f Merged revisions 66686 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 49249
diff changeset
3747 _Screen._title = titlestring
9585
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
3748
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
3749 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
3750 root = self._root
49345
ab34a647e22f Merged revisions 66686 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 49249
diff changeset
3751 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
3752 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
3753 Turtle._screen = None
49345
ab34a647e22f Merged revisions 66686 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 49249
diff changeset
3754 _Screen._root = None
ab34a647e22f Merged revisions 66686 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 49249
diff changeset
3755 _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
3756 TurtleScreen._RUNNING = True
9585
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
3757 root.destroy()
15308
847ae40074d4 Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents: 14957
diff changeset
3758
47538
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 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
3760 """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
3761
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 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
3763 >>> 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
3764 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3765 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
3766
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 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
3768 """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
3769
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 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
3771
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3772 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
3773 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
3774 (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
3775 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
3776 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
3777 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
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 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
3780 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
3781
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 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
3783 >>> 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
3784
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 """
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 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
3787 """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
3788 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
3789 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
3790 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
3791 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
3792 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
3793 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
3794 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
3795 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
3796
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3797
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 class Turtle(RawTurtle):
68585
bf94b6a73fc8 #11565: Fix several typos. Patch by Piotr Kasprzyk.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 68488
diff changeset
3799 """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
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 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
3802 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
3803 """
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 _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
3805 _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
3806
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 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
3808 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
3809 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
3810 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
3811 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
3812 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
3813 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
3814 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
3815 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
3816 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
3817
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 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
3819
9585
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
3820 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
3821 """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
3822 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
3823 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
3824 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
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 _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
3827 """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
3828 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
3829 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
3830 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
3831
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 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
3833 """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
3834
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 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
3836 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
3837 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
3838
68585
bf94b6a73fc8 #11565: Fix several typos. Patch by Piotr Kasprzyk.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 68488
diff changeset
3839 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
3840 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
3841 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
3842 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
3843 """
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3844 docsdict = {}
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3845
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3846 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
3847 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
3848 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
3849 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
3850 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
3851 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
3852
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 = 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
3854 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
3855 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
3856 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
3857 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
3858 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
3859 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
3860 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
3861 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
3862 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
3863 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
3864 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
3865
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3866 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
3867 """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
3868
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 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
3870 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
3871 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
3872 """
47538
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 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
3874 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
3875 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
3876 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
3877 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
3878 # 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
3879 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
3880 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
3881 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
3882
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 _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
3884
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 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
3886 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
3887 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
3888 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
3889 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
3890 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
3891 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
3892 _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
3893
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3894
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3895 def getmethparlist(ob):
65656
4b542217e93e Issue #10193: Simplified instrospection used by turtle module
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 64405
diff changeset
3896 """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
3897
4b542217e93e Issue #10193: Simplified instrospection used by turtle module
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 64405
diff changeset
3898 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
3899 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
3900 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
3901 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
3902 """
4b542217e93e Issue #10193: Simplified instrospection used by turtle module
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 64405
diff changeset
3903 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
3904 # 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
3905 # 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
3906 # 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
3907 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
3908 items2 = args[1:]
4b542217e93e Issue #10193: Simplified instrospection used by turtle module
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 64405
diff changeset
3909 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
3910 defaults = ob.__defaults__ or []
65656
4b542217e93e Issue #10193: Simplified instrospection used by turtle module
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 64405
diff changeset
3911 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
3912 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
3913 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
3914 if varargs is not None:
4b542217e93e Issue #10193: Simplified instrospection used by turtle module
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 64405
diff changeset
3915 items1.append("*" + varargs)
4b542217e93e Issue #10193: Simplified instrospection used by turtle module
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 64405
diff changeset
3916 items2.append("*" + varargs)
4b542217e93e Issue #10193: Simplified instrospection used by turtle module
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 64405
diff changeset
3917 if varkw is not None:
4b542217e93e Issue #10193: Simplified instrospection used by turtle module
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 64405
diff changeset
3918 items1.append("**" + varkw)
4b542217e93e Issue #10193: Simplified instrospection used by turtle module
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 64405
diff changeset
3919 items2.append("**" + varkw)
4b542217e93e Issue #10193: Simplified instrospection used by turtle module
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 64405
diff changeset
3920 defText = ", ".join(items1)
4b542217e93e Issue #10193: Simplified instrospection used by turtle module
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 64405
diff changeset
3921 defText = "(%s)" % defText
4b542217e93e Issue #10193: Simplified instrospection used by turtle module
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 64405
diff changeset
3922 callText = ", ".join(items2)
4b542217e93e Issue #10193: Simplified instrospection used by turtle module
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 64405
diff changeset
3923 callText = "(%s)" % callText
4b542217e93e Issue #10193: Simplified instrospection used by turtle module
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 64405
diff changeset
3924 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
3925
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 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
3927 """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
3928 """
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 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
3930 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
3931 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
3932 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
3933 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
3934 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
3935 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
3936 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
3937
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 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
3939 """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
3940 """
47538
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 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
3942 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
3943 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
3944 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
3945 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
3946 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
3947 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
3948 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
3949
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3950 ## 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
3951 ## 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
3952 ## 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
3953
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
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 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
3956 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
3957 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
3958 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
3959 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
3960 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
3961 {'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
3962 exec(defstr)
49345
ab34a647e22f Merged revisions 66686 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 49249
diff changeset
3963 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
3964
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 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
3966 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
3967 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
3968 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
3969 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
3970 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
3971 {'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
3972 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
3973 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
3974
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
53772
5dfd22906341 #5923: update turtle module to version 1.1.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
3976 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
3977
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
3978 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
3979 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
3980 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
3981 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
3982 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
3983 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
3984
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 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
3986 """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
3987 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
3988 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
3989 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
3990 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
3991 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
3992 # 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
3993 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
3994 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
3995 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
3996 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
3997 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
3998 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
3999 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
4000 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
4001 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
4002 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
4003 up()
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
4004 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
4005 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
4006 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
4007 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
4008 # 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
4009 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
4010 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
4011 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
4012 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
4013 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
4014 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
4015 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
4016 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
4017 # 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
4018 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
4019 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
4020 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
4021 # 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 for i in range(5):
9585
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
4023 forward(20)
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
4024 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
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 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
4027 # 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
4028 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
4029 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
4030 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
4031 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
4032 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
4033 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
4034 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
4035 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
4036 # 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
4037
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 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
4039 """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
4040 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
4041 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
4042 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
4043 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
4044 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
4045 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
4046 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
4047 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
4048 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
4049 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
4050 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
4051 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
4052 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
4053 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
4054 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
4055 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
4056 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
4057 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
4058 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
4059 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
4060 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
4061 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
4062 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
4063 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
4064 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
4065 lt(120)
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
4066 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
4067 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
4068 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
4069 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
4070 #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
4071
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 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
4073 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
4074 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
4075 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
4076 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
4077 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
4078 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
4079 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
4080 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
4081 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
4082 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
4083 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
4084 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
4085 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
4086 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
4087 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
4088 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
4089 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
4090 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
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 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
4093 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
4094 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
4095 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
4096 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
4097 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
4098 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
4099 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
4100 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
4101 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
4102 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
4103 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
4104 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
4105 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
4106 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
4107 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
4108 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
4109 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
4110 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
4111 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
4112 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
4113 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
4114 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
4115 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
4116 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
4117 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
4118 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
4119 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
4120 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
4121 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
4122 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
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 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
4125 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
4126 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
4127
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 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
4129
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
4130 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
4131 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
4132 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
4133 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
4134 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
4135 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
4136
c1aeb6c94af1 Patch #3064: Port new turtle module and demos to 3.0.
Martin v. Löwis <martin@v.loewis.de>
parents: 47130
diff changeset
4137 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
4138 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
4139 exitonclick()