annotate Lib/lib-tk/turtle.py @ 80661:94de77bd0b4b 2.7

Issue #16194: document imp.load_dynamic problems when read several extension modules from the same binary object file.
author Andrew Svetlov <andrew.svetlov@gmail.com>
date Thu, 29 Nov 2012 19:51:28 +0200
parents 6e03ab9950f6
children cd0191a9b5c9
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1 #
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2 # turtle.py: a Tkinter based turtle graphics module for Python
56267
56b530029bac Merged revisions 75115 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 55049
diff changeset
3 # Version 1.0.1 - 24. 9. 2009
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
4 #
59834
9eabba6ded09 Merged revisions 77120,77151,77155,77209,77229,77256,77317,77331,77333,77359-77360,77382,77561,77570 via svnmerge from
Georg Brandl <georg@python.org>
parents: 56267
diff changeset
5 # Copyright (C) 2006 - 2010 Gregor Lingl
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
6 # email: glingl@aon.at
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
7 #
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
8 # This software is provided 'as-is', without any express or implied
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
9 # warranty. In no event will the authors be held liable for any damages
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
10 # arising from the use of this software.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
11 #
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
12 # Permission is granted to anyone to use this software for any purpose,
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
13 # including commercial applications, and to alter it and redistribute it
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
14 # freely, subject to the following restrictions:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
15 #
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
16 # 1. The origin of this software must not be misrepresented; you must not
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
17 # claim that you wrote the original software. If you use this software
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
18 # in a product, an acknowledgment in the product documentation would be
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
19 # appreciated but is not required.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
20 # 2. Altered source versions must be plainly marked as such, and must not be
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
21 # misrepresented as being the original software.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
22 # 3. This notice may not be removed or altered from any source distribution.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
23
9585
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
24
38204
fa3c4ea99d95 Patch #1486962: Several bugs in the turtle Tk demo module were fixed
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
25 """
fa3c4ea99d95 Patch #1486962: Several bugs in the turtle Tk demo module were fixed
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
26 Turtle graphics is a popular way for introducing programming to
fa3c4ea99d95 Patch #1486962: Several bugs in the turtle Tk demo module were fixed
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
27 kids. It was part of the original Logo programming language developed
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
28 by Wally Feurzig and Seymour Papert in 1966.
38204
fa3c4ea99d95 Patch #1486962: Several bugs in the turtle Tk demo module were fixed
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
29
71765
a30e6c4bf486 #12677: correct turtle orientation in doc
Sandro Tosi <sandro.tosi@gmail.com>
parents: 68588
diff changeset
30 Imagine a robotic turtle starting at (0, 0) in the x-y plane. After an ``import turtle``, give it
38204
fa3c4ea99d95 Patch #1486962: Several bugs in the turtle Tk demo module were fixed
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
31 the command turtle.forward(15), and it moves (on-screen!) 15 pixels in
fa3c4ea99d95 Patch #1486962: Several bugs in the turtle Tk demo module were fixed
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
32 the direction it is facing, drawing a line as it moves. Give it the
71765
a30e6c4bf486 #12677: correct turtle orientation in doc
Sandro Tosi <sandro.tosi@gmail.com>
parents: 68588
diff changeset
33 command turtle.right(25), and it rotates in-place 25 degrees clockwise.
38204
fa3c4ea99d95 Patch #1486962: Several bugs in the turtle Tk demo module were fixed
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
34
fa3c4ea99d95 Patch #1486962: Several bugs in the turtle Tk demo module were fixed
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
35 By combining together these and similar commands, intricate shapes and
fa3c4ea99d95 Patch #1486962: Several bugs in the turtle Tk demo module were fixed
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
36 pictures can easily be drawn.
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
37
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
38 ----- turtle.py
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
39
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
40 This module is an extended reimplementation of turtle.py from the
51761
6ce96ce4d9af Merged revisions 69816 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 49758
diff changeset
41 Python standard distribution up to Python 2.5. (See: http://www.python.org)
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
42
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
43 It tries to keep the merits of turtle.py and to be (nearly) 100%
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
44 compatible with it. This means in the first place to enable the
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
45 learning programmer to use all the commands, classes and methods
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
46 interactively when using the module from within IDLE run with
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
47 the -n switch.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
48
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
49 Roughly it has the following features added:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
50
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
51 - Better animation of the turtle movements, especially of turning the
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
52 turtle. So the turtles can more easily be used as a visual feedback
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
53 instrument by the (beginning) programmer.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
54
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
55 - Different turtle shapes, gif-images as turtle shapes, user defined
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
56 and user controllable turtle shapes, among them compound
51761
6ce96ce4d9af Merged revisions 69816 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 49758
diff changeset
57 (multicolored) shapes. Turtle shapes can be stretched and tilted, which
6ce96ce4d9af Merged revisions 69816 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 49758
diff changeset
58 makes turtles very versatile geometrical objects.
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
59
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
60 - Fine control over turtle movement and screen updates via delay(),
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
61 and enhanced tracer() and speed() methods.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
62
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
63 - Aliases for the most commonly used commands, like fd for forward etc.,
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
64 following the early Logo traditions. This reduces the boring work of
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
65 typing long sequences of commands, which often occur in a natural way
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
66 when kids try to program fancy pictures on their first encounter with
51761
6ce96ce4d9af Merged revisions 69816 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 49758
diff changeset
67 turtle graphics.
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
68
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
69 - Turtles now have an undo()-method with configurable undo-buffer.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
70
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
71 - Some simple commands/methods for creating event driven programs
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
72 (mouse-, key-, timer-events). Especially useful for programming games.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
73
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
74 - A scrollable Canvas class. The default scrollable Canvas can be
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
75 extended interactively as needed while playing around with the turtle(s).
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
76
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
77 - A TurtleScreen class with methods controlling background color or
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
78 background image, window and canvas size and other properties of the
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
79 TurtleScreen.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
80
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
81 - There is a method, setworldcoordinates(), to install a user defined
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
82 coordinate-system for the TurtleScreen.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
83
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
84 - The implementation uses a 2-vector class named Vec2D, derived from tuple.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
85 This class is public, so it can be imported by the application programmer,
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
86 which makes certain types of computations very natural and compact.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
87
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
88 - Appearance of the TurtleScreen and the Turtles at startup/import can be
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
89 configured by means of a turtle.cfg configuration file.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
90 The default configuration mimics the appearance of the old turtle module.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
91
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
92 - If configured appropriately the module reads in docstrings from a docstring
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
93 dictionary in some different language, supplied separately and replaces
51761
6ce96ce4d9af Merged revisions 69816 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 49758
diff changeset
94 the English ones by those read in. There is a utility function
6ce96ce4d9af Merged revisions 69816 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 49758
diff changeset
95 write_docstringdict() to write a dictionary with the original (English)
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
96 docstrings to disc, so it can serve as a template for translations.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
97
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
98 Behind the scenes there are some features included with possible
72984
86e3943d0d5b Remove duplication.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 71765
diff changeset
99 extensions in mind. These will be commented and documented elsewhere.
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
100
38204
fa3c4ea99d95 Patch #1486962: Several bugs in the turtle Tk demo module were fixed
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
101 """
fa3c4ea99d95 Patch #1486962: Several bugs in the turtle Tk demo module were fixed
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
102
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
103 _ver = "turtle 1.0b1 - for Python 2.6 - 30. 5. 2008, 18:08"
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
104
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
105 #print _ver
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
106
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
107 import Tkinter as TK
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
108 import types
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
109 import math
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
110 import time
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
111 import os
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
112
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
113 from os.path import isfile, split, join
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
114 from copy import deepcopy
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
115
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
116 from math import * ## for compatibility with old turtle module
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
117
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
118 _tg_classes = ['ScrolledCanvas', 'TurtleScreen', 'Screen',
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
119 'RawTurtle', 'Turtle', 'RawPen', 'Pen', 'Shape', 'Vec2D']
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
120 _tg_screen_functions = ['addshape', 'bgcolor', 'bgpic', 'bye',
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
121 'clearscreen', 'colormode', 'delay', 'exitonclick', 'getcanvas',
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
122 'getshapes', 'listen', 'mode', 'onkey', 'onscreenclick', 'ontimer',
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
123 'register_shape', 'resetscreen', 'screensize', 'setup',
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
124 'setworldcoordinates', 'title', 'tracer', 'turtles', 'update',
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
125 'window_height', 'window_width']
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
126 _tg_turtle_functions = ['back', 'backward', 'begin_fill', 'begin_poly', 'bk',
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
127 'circle', 'clear', 'clearstamp', 'clearstamps', 'clone', 'color',
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
128 'degrees', 'distance', 'dot', 'down', 'end_fill', 'end_poly', 'fd',
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
129 'fill', 'fillcolor', 'forward', 'get_poly', 'getpen', 'getscreen',
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
130 'getturtle', 'goto', 'heading', 'hideturtle', 'home', 'ht', 'isdown',
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
131 'isvisible', 'left', 'lt', 'onclick', 'ondrag', 'onrelease', 'pd',
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
132 'pen', 'pencolor', 'pendown', 'pensize', 'penup', 'pos', 'position',
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
133 'pu', 'radians', 'right', 'reset', 'resizemode', 'rt',
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
134 'seth', 'setheading', 'setpos', 'setposition', 'settiltangle',
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
135 'setundobuffer', 'setx', 'sety', 'shape', 'shapesize', 'showturtle',
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
136 'speed', 'st', 'stamp', 'tilt', 'tiltangle', 'towards', 'tracer',
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
137 'turtlesize', 'undo', 'undobufferentries', 'up', 'width',
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
138 'window_height', 'window_width', 'write', 'xcor', 'ycor']
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
139 _tg_utilities = ['write_docstringdict', 'done', 'mainloop']
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
140 _math_functions = ['acos', 'asin', 'atan', 'atan2', 'ceil', 'cos', 'cosh',
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
141 'e', 'exp', 'fabs', 'floor', 'fmod', 'frexp', 'hypot', 'ldexp', 'log',
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
142 'log10', 'modf', 'pi', 'pow', 'sin', 'sinh', 'sqrt', 'tan', 'tanh']
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
143
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
144 __all__ = (_tg_classes + _tg_screen_functions + _tg_turtle_functions +
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
145 _tg_utilities + _math_functions)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
146
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
147 _alias_list = ['addshape', 'backward', 'bk', 'fd', 'ht', 'lt', 'pd', 'pos',
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
148 'pu', 'rt', 'seth', 'setpos', 'setposition', 'st',
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
149 'turtlesize', 'up', 'width']
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
150
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
151 _CFG = {"width" : 0.5, # Screen
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
152 "height" : 0.75,
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
153 "canvwidth" : 400,
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
154 "canvheight": 300,
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
155 "leftright": None,
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
156 "topbottom": None,
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
157 "mode": "standard", # TurtleScreen
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
158 "colormode": 1.0,
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
159 "delay": 10,
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
160 "undobuffersize": 1000, # RawTurtle
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
161 "shape": "classic",
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
162 "pencolor" : "black",
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
163 "fillcolor" : "black",
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
164 "resizemode" : "noresize",
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
165 "visible" : True,
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
166 "language": "english", # docstrings
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
167 "exampleturtle": "turtle",
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
168 "examplescreen": "screen",
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
169 "title": "Python Turtle Graphics",
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
170 "using_IDLE": False
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
171 }
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
172
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
173 ##print "cwd:", os.getcwd()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
174 ##print "__file__:", __file__
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
175 ##
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
176 ##def show(dictionary):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
177 ## print "=========================="
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
178 ## for key in sorted(dictionary.keys()):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
179 ## print key, ":", dictionary[key]
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
180 ## print "=========================="
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
181 ## print
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
182
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
183 def config_dict(filename):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
184 """Convert content of config-file into dictionary."""
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
185 f = open(filename, "r")
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
186 cfglines = f.readlines()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
187 f.close()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
188 cfgdict = {}
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
189 for line in cfglines:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
190 line = line.strip()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
191 if not line or line.startswith("#"):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
192 continue
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
193 try:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
194 key, value = line.split("=")
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
195 except:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
196 print "Bad line in config-file %s:\n%s" % (filename,line)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
197 continue
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
198 key = key.strip()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
199 value = value.strip()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
200 if value in ["True", "False", "None", "''", '""']:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
201 value = eval(value)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
202 else:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
203 try:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
204 if "." in value:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
205 value = float(value)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
206 else:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
207 value = int(value)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
208 except:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
209 pass # value need not be converted
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
210 cfgdict[key] = value
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
211 return cfgdict
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
212
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
213 def readconfig(cfgdict):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
214 """Read config-files, change configuration-dict accordingly.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
215
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
216 If there is a turtle.cfg file in the current working directory,
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
217 read it from there. If this contains an importconfig-value,
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
218 say 'myway', construct filename turtle_mayway.cfg else use
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
219 turtle.cfg and read it from the import-directory, where
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
220 turtle.py is located.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
221 Update configuration dictionary first according to config-file,
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
222 in the import directory, then according to config-file in the
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
223 current working directory.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
224 If no config-file is found, the default configuration is used.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
225 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
226 default_cfg = "turtle.cfg"
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
227 cfgdict1 = {}
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
228 cfgdict2 = {}
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
229 if isfile(default_cfg):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
230 cfgdict1 = config_dict(default_cfg)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
231 #print "1. Loading config-file %s from: %s" % (default_cfg, os.getcwd())
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
232 if "importconfig" in cfgdict1:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
233 default_cfg = "turtle_%s.cfg" % cfgdict1["importconfig"]
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
234 try:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
235 head, tail = split(__file__)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
236 cfg_file2 = join(head, default_cfg)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
237 except:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
238 cfg_file2 = ""
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
239 if isfile(cfg_file2):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
240 #print "2. Loading config-file %s:" % cfg_file2
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
241 cfgdict2 = config_dict(cfg_file2)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
242 ## show(_CFG)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
243 ## show(cfgdict2)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
244 _CFG.update(cfgdict2)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
245 ## show(_CFG)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
246 ## show(cfgdict1)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
247 _CFG.update(cfgdict1)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
248 ## show(_CFG)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
249
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
250 try:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
251 readconfig(_CFG)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
252 except:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
253 print "No configfile read, reason unknown"
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
254
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
255
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
256 class Vec2D(tuple):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
257 """A 2 dimensional vector class, used as a helper class
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
258 for implementing turtle graphics.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
259 May be useful for turtle graphics programs also.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
260 Derived from tuple, so a vector is a tuple!
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
261
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
262 Provides (for a, b vectors, k number):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
263 a+b vector addition
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
264 a-b vector subtraction
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
265 a*b inner product
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
266 k*a and a*k multiplication with scalar
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
267 |a| absolute value of a
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
268 a.rotate(angle) rotation
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
269 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
270 def __new__(cls, x, y):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
271 return tuple.__new__(cls, (x, y))
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
272 def __add__(self, other):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
273 return Vec2D(self[0]+other[0], self[1]+other[1])
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
274 def __mul__(self, other):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
275 if isinstance(other, Vec2D):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
276 return self[0]*other[0]+self[1]*other[1]
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
277 return Vec2D(self[0]*other, self[1]*other)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
278 def __rmul__(self, other):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
279 if isinstance(other, int) or isinstance(other, float):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
280 return Vec2D(self[0]*other, self[1]*other)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
281 def __sub__(self, other):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
282 return Vec2D(self[0]-other[0], self[1]-other[1])
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
283 def __neg__(self):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
284 return Vec2D(-self[0], -self[1])
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
285 def __abs__(self):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
286 return (self[0]**2 + self[1]**2)**0.5
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
287 def rotate(self, angle):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
288 """rotate self counterclockwise by angle
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
289 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
290 perp = Vec2D(-self[1], self[0])
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
291 angle = angle * math.pi / 180.0
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
292 c, s = math.cos(angle), math.sin(angle)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
293 return Vec2D(self[0]*c+perp[0]*s, self[1]*c+perp[1]*s)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
294 def __getnewargs__(self):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
295 return (self[0], self[1])
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
296 def __repr__(self):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
297 return "(%.2f,%.2f)" % self
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
298
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
299
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
300 ##############################################################################
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
301 ### From here up to line : Tkinter - Interface for turtle.py ###
51761
6ce96ce4d9af Merged revisions 69816 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 49758
diff changeset
302 ### May be replaced by an interface to some different graphics toolkit ###
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
303 ##############################################################################
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
304
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
305 ## helper functions for Scrolled Canvas, to forward Canvas-methods
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
306 ## to ScrolledCanvas class
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
307
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
308 def __methodDict(cls, _dict):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
309 """helper function for Scrolled Canvas"""
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
310 baseList = list(cls.__bases__)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
311 baseList.reverse()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
312 for _super in baseList:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
313 __methodDict(_super, _dict)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
314 for key, value in cls.__dict__.items():
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
315 if type(value) == types.FunctionType:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
316 _dict[key] = value
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
317
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
318 def __methods(cls):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
319 """helper function for Scrolled Canvas"""
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
320 _dict = {}
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
321 __methodDict(cls, _dict)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
322 return _dict.keys()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
323
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
324 __stringBody = (
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
325 'def %(method)s(self, *args, **kw): return ' +
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
326 'self.%(attribute)s.%(method)s(*args, **kw)')
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
327
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
328 def __forwardmethods(fromClass, toClass, toPart, exclude = ()):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
329 """Helper functions for Scrolled Canvas, used to forward
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
330 ScrolledCanvas-methods to Tkinter.Canvas class.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
331 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
332 _dict = {}
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
333 __methodDict(toClass, _dict)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
334 for ex in _dict.keys():
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
335 if ex[:1] == '_' or ex[-1:] == '_':
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
336 del _dict[ex]
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
337 for ex in exclude:
56436
684960fb1cbf replace has_key with 'in' operator
Benjamin Peterson <benjamin@python.org>
parents: 56267
diff changeset
338 if ex in _dict:
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
339 del _dict[ex]
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
340 for ex in __methods(fromClass):
56436
684960fb1cbf replace has_key with 'in' operator
Benjamin Peterson <benjamin@python.org>
parents: 56267
diff changeset
341 if ex in _dict:
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
342 del _dict[ex]
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
343
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
344 for method, func in _dict.items():
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
345 d = {'method': method, 'func': func}
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
346 if type(toPart) == types.StringType:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
347 execString = \
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
348 __stringBody % {'method' : method, 'attribute' : toPart}
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
349 exec execString in d
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
350 fromClass.__dict__[method] = d[method]
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
351
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
352
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
353 class ScrolledCanvas(TK.Frame):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
354 """Modeled after the scrolled canvas class from Grayons's Tkinter book.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
355
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
356 Used as the default canvas, which pops up automatically when
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
357 using turtle graphics functions or the Turtle class.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
358 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
359 def __init__(self, master, width=500, height=350,
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
360 canvwidth=600, canvheight=500):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
361 TK.Frame.__init__(self, master, width=width, height=height)
49758
7bb204612fc6 Merged revisions 67279 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 49344
diff changeset
362 self._rootwindow = self.winfo_toplevel()
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
363 self.width, self.height = width, height
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
364 self.canvwidth, self.canvheight = canvwidth, canvheight
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
365 self.bg = "white"
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
366 self._canvas = TK.Canvas(master, width=width, height=height,
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
367 bg=self.bg, relief=TK.SUNKEN, borderwidth=2)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
368 self.hscroll = TK.Scrollbar(master, command=self._canvas.xview,
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
369 orient=TK.HORIZONTAL)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
370 self.vscroll = TK.Scrollbar(master, command=self._canvas.yview)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
371 self._canvas.configure(xscrollcommand=self.hscroll.set,
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
372 yscrollcommand=self.vscroll.set)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
373 self.rowconfigure(0, weight=1, minsize=0)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
374 self.columnconfigure(0, weight=1, minsize=0)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
375 self._canvas.grid(padx=1, in_ = self, pady=1, row=0,
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
376 column=0, rowspan=1, columnspan=1, sticky='news')
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
377 self.vscroll.grid(padx=1, in_ = self, pady=1, row=0,
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
378 column=1, rowspan=1, columnspan=1, sticky='news')
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
379 self.hscroll.grid(padx=1, in_ = self, pady=1, row=1,
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
380 column=0, rowspan=1, columnspan=1, sticky='news')
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
381 self.reset()
49758
7bb204612fc6 Merged revisions 67279 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 49344
diff changeset
382 self._rootwindow.bind('<Configure>', self.onResize)
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
383
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
384 def reset(self, canvwidth=None, canvheight=None, bg = None):
51761
6ce96ce4d9af Merged revisions 69816 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 49758
diff changeset
385 """Adjust canvas and scrollbars according to given canvas size."""
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
386 if canvwidth:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
387 self.canvwidth = canvwidth
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
388 if canvheight:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
389 self.canvheight = canvheight
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
390 if bg:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
391 self.bg = bg
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
392 self._canvas.config(bg=bg,
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
393 scrollregion=(-self.canvwidth//2, -self.canvheight//2,
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
394 self.canvwidth//2, self.canvheight//2))
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
395 self._canvas.xview_moveto(0.5*(self.canvwidth - self.width + 30) /
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
396 self.canvwidth)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
397 self._canvas.yview_moveto(0.5*(self.canvheight- self.height + 30) /
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
398 self.canvheight)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
399 self.adjustScrolls()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
400
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
401
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
402 def adjustScrolls(self):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
403 """ Adjust scrollbars according to window- and canvas-size.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
404 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
405 cwidth = self._canvas.winfo_width()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
406 cheight = self._canvas.winfo_height()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
407 self._canvas.xview_moveto(0.5*(self.canvwidth-cwidth)/self.canvwidth)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
408 self._canvas.yview_moveto(0.5*(self.canvheight-cheight)/self.canvheight)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
409 if cwidth < self.canvwidth or cheight < self.canvheight:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
410 self.hscroll.grid(padx=1, in_ = self, pady=1, row=1,
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
411 column=0, rowspan=1, columnspan=1, sticky='news')
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
412 self.vscroll.grid(padx=1, in_ = self, pady=1, row=0,
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
413 column=1, rowspan=1, columnspan=1, sticky='news')
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
414 else:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
415 self.hscroll.grid_forget()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
416 self.vscroll.grid_forget()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
417
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
418 def onResize(self, event):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
419 """self-explanatory"""
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
420 self.adjustScrolls()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
421
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
422 def bbox(self, *args):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
423 """ 'forward' method, which canvas itself has inherited...
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
424 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
425 return self._canvas.bbox(*args)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
426
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
427 def cget(self, *args, **kwargs):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
428 """ 'forward' method, which canvas itself has inherited...
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
429 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
430 return self._canvas.cget(*args, **kwargs)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
431
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
432 def config(self, *args, **kwargs):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
433 """ 'forward' method, which canvas itself has inherited...
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
434 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
435 self._canvas.config(*args, **kwargs)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
436
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
437 def bind(self, *args, **kwargs):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
438 """ 'forward' method, which canvas itself has inherited...
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
439 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
440 self._canvas.bind(*args, **kwargs)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
441
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
442 def unbind(self, *args, **kwargs):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
443 """ 'forward' method, which canvas itself has inherited...
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
444 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
445 self._canvas.unbind(*args, **kwargs)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
446
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
447 def focus_force(self):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
448 """ 'forward' method, which canvas itself has inherited...
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
449 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
450 self._canvas.focus_force()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
451
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
452 __forwardmethods(ScrolledCanvas, TK.Canvas, '_canvas')
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
453
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
454
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
455 class _Root(TK.Tk):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
456 """Root class for Screen based on Tkinter."""
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
457 def __init__(self):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
458 TK.Tk.__init__(self)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
460 def setupcanvas(self, width, height, cwidth, cheight):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
461 self._canvas = ScrolledCanvas(self, width, height, cwidth, cheight)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
462 self._canvas.pack(expand=1, fill="both")
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
463
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
464 def _getcanvas(self):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
465 return self._canvas
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
466
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
467 def set_geometry(self, width, height, startx, starty):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
468 self.geometry("%dx%d%+d%+d"%(width, height, startx, starty))
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
469
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
470 def ondestroy(self, destroy):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
471 self.wm_protocol("WM_DELETE_WINDOW", destroy)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
472
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
473 def win_width(self):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
474 return self.winfo_screenwidth()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
475
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
476 def win_height(self):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
477 return self.winfo_screenheight()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
478
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
479 Canvas = TK.Canvas
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
480
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
481
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
482 class TurtleScreenBase(object):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
483 """Provide the basic graphics functionality.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
484 Interface between Tkinter and turtle.py.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
485
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
486 To port turtle.py to some different graphics toolkit
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
487 a corresponding TurtleScreenBase class has to be implemented.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
488 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
489
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
490 @staticmethod
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
491 def _blankimage():
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
492 """return a blank image object
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
493 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
494 img = TK.PhotoImage(width=1, height=1)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
495 img.blank()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
496 return img
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
497
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
498 @staticmethod
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
499 def _image(filename):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
500 """return an image object containing the
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
501 imagedata from a gif-file named filename.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
502 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
503 return TK.PhotoImage(file=filename)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
504
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
505 def __init__(self, cv):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
506 self.cv = cv
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
507 if isinstance(cv, ScrolledCanvas):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
508 w = self.cv.canvwidth
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
509 h = self.cv.canvheight
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
510 else: # expected: ordinary TK.Canvas
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
511 w = int(self.cv.cget("width"))
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
512 h = int(self.cv.cget("height"))
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
513 self.cv.config(scrollregion = (-w//2, -h//2, w//2, h//2 ))
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
514 self.canvwidth = w
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
515 self.canvheight = h
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
516 self.xscale = self.yscale = 1.0
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
517
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
518 def _createpoly(self):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
519 """Create an invisible polygon item on canvas self.cv)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
520 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
521 return self.cv.create_polygon((0, 0, 0, 0, 0, 0), fill="", outline="")
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
522
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
523 def _drawpoly(self, polyitem, coordlist, fill=None,
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
524 outline=None, width=None, top=False):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
525 """Configure polygonitem polyitem according to provided
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
526 arguments:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
527 coordlist is sequence of coordinates
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
528 fill is filling color
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
529 outline is outline color
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
530 top is a boolean value, which specifies if polyitem
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
531 will be put on top of the canvas' displaylist so it
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
532 will not be covered by other items.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
533 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
534 cl = []
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
535 for x, y in coordlist:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
536 cl.append(x * self.xscale)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
537 cl.append(-y * self.yscale)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
538 self.cv.coords(polyitem, *cl)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
539 if fill is not None:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
540 self.cv.itemconfigure(polyitem, fill=fill)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
541 if outline is not None:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
542 self.cv.itemconfigure(polyitem, outline=outline)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
543 if width is not None:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
544 self.cv.itemconfigure(polyitem, width=width)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
545 if top:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
546 self.cv.tag_raise(polyitem)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
547
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
548 def _createline(self):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
549 """Create an invisible line item on canvas self.cv)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
550 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
551 return self.cv.create_line(0, 0, 0, 0, fill="", width=2,
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
552 capstyle = TK.ROUND)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
553
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
554 def _drawline(self, lineitem, coordlist=None,
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
555 fill=None, width=None, top=False):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
556 """Configure lineitem according to provided arguments:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
557 coordlist is sequence of coordinates
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
558 fill is drawing color
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
559 width is width of drawn line.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
560 top is a boolean value, which specifies if polyitem
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
561 will be put on top of the canvas' displaylist so it
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
562 will not be covered by other items.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
563 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
564 if coordlist is not None:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
565 cl = []
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
566 for x, y in coordlist:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
567 cl.append(x * self.xscale)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
568 cl.append(-y * self.yscale)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
569 self.cv.coords(lineitem, *cl)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
570 if fill is not None:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
571 self.cv.itemconfigure(lineitem, fill=fill)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
572 if width is not None:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
573 self.cv.itemconfigure(lineitem, width=width)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
574 if top:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
575 self.cv.tag_raise(lineitem)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
576
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
577 def _delete(self, item):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
578 """Delete graphics item from canvas.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
579 If item is"all" delete all graphics items.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
580 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
581 self.cv.delete(item)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
582
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
583 def _update(self):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
584 """Redraw graphics items on canvas
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
585 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
586 self.cv.update()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
587
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
588 def _delay(self, delay):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
589 """Delay subsequent canvas actions for delay ms."""
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
590 self.cv.after(delay)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
591
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
592 def _iscolorstring(self, color):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
593 """Check if the string color is a legal Tkinter color string.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
594 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
595 try:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
596 rgb = self.cv.winfo_rgb(color)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
597 ok = True
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
598 except TK.TclError:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
599 ok = False
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
600 return ok
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
601
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
602 def _bgcolor(self, color=None):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
603 """Set canvas' backgroundcolor if color is not None,
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
604 else return backgroundcolor."""
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
605 if color is not None:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
606 self.cv.config(bg = color)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
607 self._update()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
608 else:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
609 return self.cv.cget("bg")
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
610
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
611 def _write(self, pos, txt, align, font, pencolor):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
612 """Write txt at pos in canvas with specified font
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
613 and color.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
614 Return text item and x-coord of right bottom corner
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
615 of text's bounding box."""
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
616 x, y = pos
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
617 x = x * self.xscale
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
618 y = y * self.yscale
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
619 anchor = {"left":"sw", "center":"s", "right":"se" }
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
620 item = self.cv.create_text(x-1, -y, text = txt, anchor = anchor[align],
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
621 fill = pencolor, font = font)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
622 x0, y0, x1, y1 = self.cv.bbox(item)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
623 self.cv.update()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
624 return item, x1-1
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
625
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
626 ## def _dot(self, pos, size, color):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
627 ## """may be implemented for some other graphics toolkit"""
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
628
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
629 def _onclick(self, item, fun, num=1, add=None):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
630 """Bind fun to mouse-click event on turtle.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
631 fun must be a function with two arguments, the coordinates
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
632 of the clicked point on the canvas.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
633 num, the number of the mouse-button defaults to 1
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
634 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
635 if fun is None:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
636 self.cv.tag_unbind(item, "<Button-%s>" % num)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
637 else:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
638 def eventfun(event):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
639 x, y = (self.cv.canvasx(event.x)/self.xscale,
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
640 -self.cv.canvasy(event.y)/self.yscale)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
641 fun(x, y)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
642 self.cv.tag_bind(item, "<Button-%s>" % num, eventfun, add)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
643
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
644 def _onrelease(self, item, fun, num=1, add=None):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
645 """Bind fun to mouse-button-release event on turtle.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
646 fun must be a function with two arguments, the coordinates
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
647 of the point on the canvas where mouse button is released.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
648 num, the number of the mouse-button defaults to 1
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
649
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
650 If a turtle is clicked, first _onclick-event will be performed,
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
651 then _onscreensclick-event.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
652 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
653 if fun is None:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
654 self.cv.tag_unbind(item, "<Button%s-ButtonRelease>" % num)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
655 else:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
656 def eventfun(event):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
657 x, y = (self.cv.canvasx(event.x)/self.xscale,
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
658 -self.cv.canvasy(event.y)/self.yscale)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
659 fun(x, y)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
660 self.cv.tag_bind(item, "<Button%s-ButtonRelease>" % num,
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
661 eventfun, add)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
662
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
663 def _ondrag(self, item, fun, num=1, add=None):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
664 """Bind fun to mouse-move-event (with pressed mouse button) on turtle.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
665 fun must be a function with two arguments, the coordinates of the
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
666 actual mouse position on the canvas.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
667 num, the number of the mouse-button defaults to 1
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
668
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
669 Every sequence of mouse-move-events on a turtle is preceded by a
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
670 mouse-click event on that turtle.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
671 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
672 if fun is None:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
673 self.cv.tag_unbind(item, "<Button%s-Motion>" % num)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
674 else:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
675 def eventfun(event):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
676 try:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
677 x, y = (self.cv.canvasx(event.x)/self.xscale,
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
678 -self.cv.canvasy(event.y)/self.yscale)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
679 fun(x, y)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
680 except:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
681 pass
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
682 self.cv.tag_bind(item, "<Button%s-Motion>" % num, eventfun, add)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
683
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
684 def _onscreenclick(self, fun, num=1, add=None):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
685 """Bind fun to mouse-click event on canvas.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
686 fun must be a function with two arguments, the coordinates
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
687 of the clicked point on the canvas.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
688 num, the number of the mouse-button defaults to 1
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
689
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
690 If a turtle is clicked, first _onclick-event will be performed,
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
691 then _onscreensclick-event.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
692 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
693 if fun is None:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
694 self.cv.unbind("<Button-%s>" % num)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
695 else:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
696 def eventfun(event):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
697 x, y = (self.cv.canvasx(event.x)/self.xscale,
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
698 -self.cv.canvasy(event.y)/self.yscale)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
699 fun(x, y)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
700 self.cv.bind("<Button-%s>" % num, eventfun, add)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
701
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
702 def _onkey(self, fun, key):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
703 """Bind fun to key-release event of key.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
704 Canvas must have focus. See method listen
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
705 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
706 if fun is None:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
707 self.cv.unbind("<KeyRelease-%s>" % key, None)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
708 else:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
709 def eventfun(event):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
710 fun()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
711 self.cv.bind("<KeyRelease-%s>" % key, eventfun)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
712
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
713 def _listen(self):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
714 """Set focus on canvas (in order to collect key-events)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
715 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
716 self.cv.focus_force()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
717
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
718 def _ontimer(self, fun, t):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
719 """Install a timer, which calls fun after t milliseconds.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
720 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
721 if t == 0:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
722 self.cv.after_idle(fun)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
723 else:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
724 self.cv.after(t, fun)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
725
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
726 def _createimage(self, image):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
727 """Create and return image item on canvas.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
728 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
729 return self.cv.create_image(0, 0, image=image)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
730
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
731 def _drawimage(self, item, (x, y), image):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
732 """Configure image item as to draw image object
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
733 at position (x,y) on canvas)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
734 """
49291
120b4bcf09ff #3950 fix missing scale factors in turtle.py
Benjamin Peterson <benjamin@python.org>
parents: 47459
diff changeset
735 self.cv.coords(item, (x * self.xscale, -y * self.yscale))
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
736 self.cv.itemconfig(item, image=image)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
737
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
738 def _setbgpic(self, item, image):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
739 """Configure image item as to draw image object
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
740 at center of canvas. Set item to the first item
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
741 in the displaylist, so it will be drawn below
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
742 any other item ."""
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
743 self.cv.itemconfig(item, image=image)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
744 self.cv.tag_lower(item)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
745
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
746 def _type(self, item):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
747 """Return 'line' or 'polygon' or 'image' depending on
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
748 type of item.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
749 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
750 return self.cv.type(item)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
751
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
752 def _pointlist(self, item):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
753 """returns list of coordinate-pairs of points of item
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
754 Example (for insiders):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
755 >>> from turtle import *
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
756 >>> getscreen()._pointlist(getturtle().turtle._item)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
757 [(0.0, 9.9999999999999982), (0.0, -9.9999999999999982),
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
758 (9.9999999999999982, 0.0)]
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
759 >>> """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
760 cl = self.cv.coords(item)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
761 pl = [(cl[i], -cl[i+1]) for i in range(0, len(cl), 2)]
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
762 return pl
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
763
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
764 def _setscrollregion(self, srx1, sry1, srx2, sry2):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
765 self.cv.config(scrollregion=(srx1, sry1, srx2, sry2))
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
766
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
767 def _rescale(self, xscalefactor, yscalefactor):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
768 items = self.cv.find_all()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
769 for item in items:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
770 coordinates = self.cv.coords(item)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
771 newcoordlist = []
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
772 while coordinates:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
773 x, y = coordinates[:2]
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
774 newcoordlist.append(x * xscalefactor)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
775 newcoordlist.append(y * yscalefactor)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
776 coordinates = coordinates[2:]
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
777 self.cv.coords(item, *newcoordlist)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
778
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
779 def _resize(self, canvwidth=None, canvheight=None, bg=None):
51761
6ce96ce4d9af Merged revisions 69816 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 49758
diff changeset
780 """Resize the canvas the turtles are drawing on. Does
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
781 not alter the drawing window.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
782 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
783 # needs amendment
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
784 if not isinstance(self.cv, ScrolledCanvas):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
785 return self.canvwidth, self.canvheight
60060
0bc04525af63 Merged revisions 79195,79425,79427,79450 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59834
diff changeset
786 if canvwidth is canvheight is bg is None:
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
787 return self.cv.canvwidth, self.cv.canvheight
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
788 if canvwidth is not None:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
789 self.canvwidth = canvwidth
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
790 if canvheight is not None:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
791 self.canvheight = canvheight
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
792 self.cv.reset(canvwidth, canvheight, bg)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
793
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
794 def _window_size(self):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
795 """ Return the width and height of the turtle window.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
796 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
797 width = self.cv.winfo_width()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
798 if width <= 1: # the window isn't managed by a geometry manager
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
799 width = self.cv['width']
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
800 height = self.cv.winfo_height()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
801 if height <= 1: # the window isn't managed by a geometry manager
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
802 height = self.cv['height']
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
803 return width, height
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
804
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
805
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
806 ##############################################################################
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
807 ### End of Tkinter - interface ###
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
808 ##############################################################################
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
809
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
810
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
811 class Terminator (Exception):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
812 """Will be raised in TurtleScreen.update, if _RUNNING becomes False.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
813
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
814 Thus stops execution of turtle graphics script. Main purpose: use in
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
815 in the Demo-Viewer turtle.Demo.py.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
816 """
14957
db1cce1ffb60 Don't rename Tkinter to Tk; closes bug 115714
Martin v. Löwis <martin@v.loewis.de>
parents: 9585
diff changeset
817 pass
9585
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
818
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
819
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
820 class TurtleGraphicsError(Exception):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
821 """Some TurtleGraphics Error
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
822 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
823
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
824
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
825 class Shape(object):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
826 """Data structure modeling shapes.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
827
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
828 attribute _type is one of "polygon", "image", "compound"
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
829 attribute _data is - depending on _type a poygon-tuple,
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
830 an image or a list constructed using the addcomponent method.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
831 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
832 def __init__(self, type_, data=None):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
833 self._type = type_
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
834 if type_ == "polygon":
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
835 if isinstance(data, list):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
836 data = tuple(data)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
837 elif type_ == "image":
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
838 if isinstance(data, str):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
839 if data.lower().endswith(".gif") and isfile(data):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
840 data = TurtleScreen._image(data)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
841 # else data assumed to be Photoimage
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
842 elif type_ == "compound":
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
843 data = []
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
844 else:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
845 raise TurtleGraphicsError("There is no shape type %s" % type_)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
846 self._data = data
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
847
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
848 def addcomponent(self, poly, fill, outline=None):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
849 """Add component to a shape of type compound.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
850
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
851 Arguments: poly is a polygon, i. e. a tuple of number pairs.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
852 fill is the fillcolor of the component,
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
853 outline is the outline color of the component.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
854
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
855 call (for a Shapeobject namend s):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
856 -- s.addcomponent(((0,0), (10,10), (-10,10)), "red", "blue")
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
857
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
858 Example:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
859 >>> poly = ((0,0),(10,-5),(0,10),(-10,-5))
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
860 >>> s = Shape("compound")
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
861 >>> s.addcomponent(poly, "red", "blue")
73815
6e03ab9950f6 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72984
diff changeset
862 >>> # .. add more components and then use register_shape()
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
863 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
864 if self._type != "compound":
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
865 raise TurtleGraphicsError("Cannot add component to %s Shape"
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
866 % self._type)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
867 if outline is None:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
868 outline = fill
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
869 self._data.append([poly, fill, outline])
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
870
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
871
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
872 class Tbuffer(object):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
873 """Ring buffer used as undobuffer for RawTurtle objects."""
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
874 def __init__(self, bufsize=10):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
875 self.bufsize = bufsize
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
876 self.buffer = [[None]] * bufsize
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
877 self.ptr = -1
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
878 self.cumulate = False
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
879 def reset(self, bufsize=None):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
880 if bufsize is None:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
881 for i in range(self.bufsize):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
882 self.buffer[i] = [None]
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
883 else:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
884 self.bufsize = bufsize
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
885 self.buffer = [[None]] * bufsize
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
886 self.ptr = -1
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
887 def push(self, item):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
888 if self.bufsize > 0:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
889 if not self.cumulate:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
890 self.ptr = (self.ptr + 1) % self.bufsize
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
891 self.buffer[self.ptr] = item
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
892 else:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
893 self.buffer[self.ptr].append(item)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
894 def pop(self):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
895 if self.bufsize > 0:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
896 item = self.buffer[self.ptr]
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
897 if item is None:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
898 return None
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
899 else:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
900 self.buffer[self.ptr] = [None]
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
901 self.ptr = (self.ptr - 1) % self.bufsize
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
902 return (item)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
903 def nr_of_items(self):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
904 return self.bufsize - self.buffer.count([None])
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
905 def __repr__(self):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
906 return str(self.buffer) + " " + str(self.ptr)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
907
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
908
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
909
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
910 class TurtleScreen(TurtleScreenBase):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
911 """Provides screen oriented methods like setbg etc.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
912
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
913 Only relies upon the methods of TurtleScreenBase and NOT
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
914 upon components of the underlying graphics toolkit -
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
915 which is Tkinter in this case.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
916 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
917 # _STANDARD_DELAY = 5
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
918 _RUNNING = True
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
919
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
920 def __init__(self, cv, mode=_CFG["mode"],
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
921 colormode=_CFG["colormode"], delay=_CFG["delay"]):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
922 self._shapes = {
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
923 "arrow" : Shape("polygon", ((-10,0), (10,0), (0,10))),
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
924 "turtle" : Shape("polygon", ((0,16), (-2,14), (-1,10), (-4,7),
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
925 (-7,9), (-9,8), (-6,5), (-7,1), (-5,-3), (-8,-6),
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
926 (-6,-8), (-4,-5), (0,-7), (4,-5), (6,-8), (8,-6),
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
927 (5,-3), (7,1), (6,5), (9,8), (7,9), (4,7), (1,10),
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
928 (2,14))),
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
929 "circle" : Shape("polygon", ((10,0), (9.51,3.09), (8.09,5.88),
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
930 (5.88,8.09), (3.09,9.51), (0,10), (-3.09,9.51),
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
931 (-5.88,8.09), (-8.09,5.88), (-9.51,3.09), (-10,0),
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
932 (-9.51,-3.09), (-8.09,-5.88), (-5.88,-8.09),
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
933 (-3.09,-9.51), (-0.00,-10.00), (3.09,-9.51),
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
934 (5.88,-8.09), (8.09,-5.88), (9.51,-3.09))),
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
935 "square" : Shape("polygon", ((10,-10), (10,10), (-10,10),
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
936 (-10,-10))),
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
937 "triangle" : Shape("polygon", ((10,-5.77), (0,11.55),
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
938 (-10,-5.77))),
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
939 "classic": Shape("polygon", ((0,0),(-5,-9),(0,-7),(5,-9))),
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
940 "blank" : Shape("image", self._blankimage())
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
941 }
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
942
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
943 self._bgpics = {"nopic" : ""}
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
944
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
945 TurtleScreenBase.__init__(self, cv)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
946 self._mode = mode
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
947 self._delayvalue = delay
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
948 self._colormode = _CFG["colormode"]
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
949 self._keys = []
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
950 self.clear()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
951
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
952 def clear(self):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
953 """Delete all drawings and all turtles from the TurtleScreen.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
954
51761
6ce96ce4d9af Merged revisions 69816 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 49758
diff changeset
955 Reset empty TurtleScreen to its initial state: white background,
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
956 no backgroundimage, no eventbindings and tracing on.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
957
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
958 No argument.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
959
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
960 Example (for a TurtleScreen instance named screen):
73815
6e03ab9950f6 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72984
diff changeset
961 >>> screen.clear()
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
962
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
963 Note: this method is not available as function.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
964 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
965 self._delayvalue = _CFG["delay"]
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
966 self._colormode = _CFG["colormode"]
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
967 self._delete("all")
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
968 self._bgpic = self._createimage("")
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
969 self._bgpicname = "nopic"
9585
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
970 self._tracing = 1
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
971 self._updatecounter = 0
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
972 self._turtles = []
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
973 self.bgcolor("white")
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
974 for btn in 1, 2, 3:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
975 self.onclick(None, btn)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
976 for key in self._keys[:]:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
977 self.onkey(None, key)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
978 Turtle._pen = None
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
979
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
980 def mode(self, mode=None):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
981 """Set turtle-mode ('standard', 'logo' or 'world') and perform reset.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
982
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
983 Optional argument:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
984 mode -- on of the strings 'standard', 'logo' or 'world'
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
985
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
986 Mode 'standard' is compatible with turtle.py.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
987 Mode 'logo' is compatible with most Logo-Turtle-Graphics.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
988 Mode 'world' uses userdefined 'worldcoordinates'. *Attention*: in
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
989 this mode angles appear distorted if x/y unit-ratio doesn't equal 1.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
990 If mode is not given, return the current mode.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
991
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
992 Mode Initial turtle heading positive angles
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
993 ------------|-------------------------|-------------------
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
994 'standard' to the right (east) counterclockwise
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
995 'logo' upward (north) clockwise
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
996
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
997 Examples:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
998 >>> mode('logo') # resets turtle heading to north
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
999 >>> mode()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1000 'logo'
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1001 """
60060
0bc04525af63 Merged revisions 79195,79425,79427,79450 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59834
diff changeset
1002 if mode is None:
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1003 return self._mode
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1004 mode = mode.lower()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1005 if mode not in ["standard", "logo", "world"]:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1006 raise TurtleGraphicsError("No turtle-graphics-mode %s" % mode)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1007 self._mode = mode
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1008 if mode in ["standard", "logo"]:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1009 self._setscrollregion(-self.canvwidth//2, -self.canvheight//2,
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1010 self.canvwidth//2, self.canvheight//2)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1011 self.xscale = self.yscale = 1.0
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1012 self.reset()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1013
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1014 def setworldcoordinates(self, llx, lly, urx, ury):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1015 """Set up a user defined coordinate-system.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1016
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1017 Arguments:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1018 llx -- a number, x-coordinate of lower left corner of canvas
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1019 lly -- a number, y-coordinate of lower left corner of canvas
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1020 urx -- a number, x-coordinate of upper right corner of canvas
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1021 ury -- a number, y-coordinate of upper right corner of canvas
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1022
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1023 Set up user coodinat-system and switch to mode 'world' if necessary.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1024 This performs a screen.reset. If mode 'world' is already active,
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1025 all drawings are redrawn according to the new coordinates.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1026
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1027 But ATTENTION: in user-defined coordinatesystems angles may appear
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1028 distorted. (see Screen.mode())
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1029
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1030 Example (for a TurtleScreen instance named screen):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1031 >>> screen.setworldcoordinates(-10,-0.5,50,1.5)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1032 >>> for _ in range(36):
73815
6e03ab9950f6 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72984
diff changeset
1033 ... left(10)
6e03ab9950f6 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72984
diff changeset
1034 ... forward(0.5)
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1035 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1036 if self.mode() != "world":
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1037 self.mode("world")
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1038 xspan = float(urx - llx)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1039 yspan = float(ury - lly)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1040 wx, wy = self._window_size()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1041 self.screensize(wx-20, wy-20)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1042 oldxscale, oldyscale = self.xscale, self.yscale
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1043 self.xscale = self.canvwidth / xspan
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1044 self.yscale = self.canvheight / yspan
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1045 srx1 = llx * self.xscale
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1046 sry1 = -ury * self.yscale
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1047 srx2 = self.canvwidth + srx1
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1048 sry2 = self.canvheight + sry1
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1049 self._setscrollregion(srx1, sry1, srx2, sry2)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1050 self._rescale(self.xscale/oldxscale, self.yscale/oldyscale)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1051 self.update()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1052
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1053 def register_shape(self, name, shape=None):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1054 """Adds a turtle shape to TurtleScreen's shapelist.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1055
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1056 Arguments:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1057 (1) name is the name of a gif-file and shape is None.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1058 Installs the corresponding image shape.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1059 !! Image-shapes DO NOT rotate when turning the turtle,
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1060 !! so they do not display the heading of the turtle!
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1061 (2) name is an arbitrary string and shape is a tuple
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1062 of pairs of coordinates. Installs the corresponding
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1063 polygon shape
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1064 (3) name is an arbitrary string and shape is a
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1065 (compound) Shape object. Installs the corresponding
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1066 compound shape.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1067 To use a shape, you have to issue the command shape(shapename).
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1068
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1069 call: register_shape("turtle.gif")
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1070 --or: register_shape("tri", ((0,0), (10,10), (-10,10)))
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1071
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1072 Example (for a TurtleScreen instance named screen):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1073 >>> screen.register_shape("triangle", ((5,-3),(0,5),(-5,-3)))
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1074
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1075 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1076 if shape is None:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1077 # image
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1078 if name.lower().endswith(".gif"):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1079 shape = Shape("image", self._image(name))
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1080 else:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1081 raise TurtleGraphicsError("Bad arguments for register_shape.\n"
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1082 + "Use help(register_shape)" )
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1083 elif isinstance(shape, tuple):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1084 shape = Shape("polygon", shape)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1085 ## else shape assumed to be Shape-instance
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1086 self._shapes[name] = shape
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1087 # print "shape added:" , self._shapes
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1088
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1089 def _colorstr(self, color):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1090 """Return color string corresponding to args.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1091
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1092 Argument may be a string or a tuple of three
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1093 numbers corresponding to actual colormode,
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1094 i.e. in the range 0<=n<=colormode.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1095
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1096 If the argument doesn't represent a color,
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1097 an error is raised.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1098 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1099 if len(color) == 1:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1100 color = color[0]
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1101 if isinstance(color, str):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1102 if self._iscolorstring(color) or color == "":
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1103 return color
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1104 else:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1105 raise TurtleGraphicsError("bad color string: %s" % str(color))
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1106 try:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1107 r, g, b = color
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1108 except:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1109 raise TurtleGraphicsError("bad color arguments: %s" % str(color))
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1110 if self._colormode == 1.0:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1111 r, g, b = [round(255.0*x) for x in (r, g, b)]
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1112 if not ((0 <= r <= 255) and (0 <= g <= 255) and (0 <= b <= 255)):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1113 raise TurtleGraphicsError("bad color sequence: %s" % str(color))
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1114 return "#%02x%02x%02x" % (r, g, b)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1115
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1116 def _color(self, cstr):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1117 if not cstr.startswith("#"):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1118 return cstr
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1119 if len(cstr) == 7:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1120 cl = [int(cstr[i:i+2], 16) for i in (1, 3, 5)]
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1121 elif len(cstr) == 4:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1122 cl = [16*int(cstr[h], 16) for h in cstr[1:]]
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1123 else:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1124 raise TurtleGraphicsError("bad colorstring: %s" % cstr)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1125 return tuple([c * self._colormode/255 for c in cl])
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1126
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1127 def colormode(self, cmode=None):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1128 """Return the colormode or set it to 1.0 or 255.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1129
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1130 Optional argument:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1131 cmode -- one of the values 1.0 or 255
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1132
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1133 r, g, b values of colortriples have to be in range 0..cmode.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1134
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1135 Example (for a TurtleScreen instance named screen):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1136 >>> screen.colormode()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1137 1.0
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1138 >>> screen.colormode(255)
73815
6e03ab9950f6 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72984
diff changeset
1139 >>> pencolor(240,160,80)
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1140 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1141 if cmode is None:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1142 return self._colormode
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1143 if cmode == 1.0:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1144 self._colormode = float(cmode)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1145 elif cmode == 255:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1146 self._colormode = int(cmode)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1147
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1148 def reset(self):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1149 """Reset all Turtles on the Screen to their initial state.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1150
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1151 No argument.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1152
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1153 Example (for a TurtleScreen instance named screen):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1154 >>> screen.reset()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1155 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1156 for turtle in self._turtles:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1157 turtle._setmode(self._mode)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1158 turtle.reset()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1159
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1160 def turtles(self):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1161 """Return the list of turtles on the screen.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1162
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1163 Example (for a TurtleScreen instance named screen):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1164 >>> screen.turtles()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1165 [<turtle.Turtle object at 0x00E11FB0>]
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1166 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1167 return self._turtles
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1168
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1169 def bgcolor(self, *args):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1170 """Set or return backgroundcolor of the TurtleScreen.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1171
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1172 Arguments (if given): a color string or three numbers
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1173 in the range 0..colormode or a 3-tuple of such numbers.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1174
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1175 Example (for a TurtleScreen instance named screen):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1176 >>> screen.bgcolor("orange")
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1177 >>> screen.bgcolor()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1178 'orange'
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1179 >>> screen.bgcolor(0.5,0,0.5)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1180 >>> screen.bgcolor()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1181 '#800080'
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1182 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1183 if args:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1184 color = self._colorstr(args)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1185 else:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1186 color = None
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1187 color = self._bgcolor(color)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1188 if color is not None:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1189 color = self._color(color)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1190 return color
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1191
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1192 def tracer(self, n=None, delay=None):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1193 """Turns turtle animation on/off and set delay for update drawings.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1194
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1195 Optional arguments:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1196 n -- nonnegative integer
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1197 delay -- nonnegative integer
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1198
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1199 If n is given, only each n-th regular screen update is really performed.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1200 (Can be used to accelerate the drawing of complex graphics.)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1201 Second arguments sets delay value (see RawTurtle.delay())
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1202
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1203 Example (for a TurtleScreen instance named screen):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1204 >>> screen.tracer(8, 25)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1205 >>> dist = 2
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1206 >>> for i in range(200):
73815
6e03ab9950f6 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72984
diff changeset
1207 ... fd(dist)
6e03ab9950f6 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72984
diff changeset
1208 ... rt(90)
6e03ab9950f6 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72984
diff changeset
1209 ... dist += 2
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1210 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1211 if n is None:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1212 return self._tracing
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1213 self._tracing = int(n)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1214 self._updatecounter = 0
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1215 if delay is not None:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1216 self._delayvalue = int(delay)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1217 if self._tracing:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1218 self.update()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1219
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1220 def delay(self, delay=None):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1221 """ Return or set the drawing delay in milliseconds.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1222
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1223 Optional argument:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1224 delay -- positive integer
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1225
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1226 Example (for a TurtleScreen instance named screen):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1227 >>> screen.delay(15)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1228 >>> screen.delay()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1229 15
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1230 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1231 if delay is None:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1232 return self._delayvalue
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1233 self._delayvalue = int(delay)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1234
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1235 def _incrementudc(self):
73815
6e03ab9950f6 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72984
diff changeset
1236 """Increment upadate counter."""
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1237 if not TurtleScreen._RUNNING:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1238 TurtleScreen._RUNNNING = True
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1239 raise Terminator
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1240 if self._tracing > 0:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1241 self._updatecounter += 1
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1242 self._updatecounter %= self._tracing
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1243
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1244 def update(self):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1245 """Perform a TurtleScreen update.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1246 """
56267
56b530029bac Merged revisions 75115 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 55049
diff changeset
1247 tracing = self._tracing
56b530029bac Merged revisions 75115 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 55049
diff changeset
1248 self._tracing = True
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1249 for t in self.turtles():
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1250 t._update_data()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1251 t._drawturtle()
56267
56b530029bac Merged revisions 75115 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 55049
diff changeset
1252 self._tracing = tracing
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1253 self._update()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1254
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1255 def window_width(self):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1256 """ Return the width of the turtle window.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1257
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1258 Example (for a TurtleScreen instance named screen):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1259 >>> screen.window_width()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1260 640
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1261 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1262 return self._window_size()[0]
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1263
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1264 def window_height(self):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1265 """ Return the height of the turtle window.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1266
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1267 Example (for a TurtleScreen instance named screen):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1268 >>> screen.window_height()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1269 480
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1270 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1271 return self._window_size()[1]
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1272
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1273 def getcanvas(self):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1274 """Return the Canvas of this TurtleScreen.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1275
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1276 No argument.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1277
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1278 Example (for a Screen instance named screen):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1279 >>> cv = screen.getcanvas()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1280 >>> cv
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1281 <turtle.ScrolledCanvas instance at 0x010742D8>
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1282 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1283 return self.cv
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1284
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1285 def getshapes(self):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1286 """Return a list of names of all currently available turtle shapes.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1287
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1288 No argument.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1289
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1290 Example (for a TurtleScreen instance named screen):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1291 >>> screen.getshapes()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1292 ['arrow', 'blank', 'circle', ... , 'turtle']
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1293 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1294 return sorted(self._shapes.keys())
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1295
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1296 def onclick(self, fun, btn=1, add=None):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1297 """Bind fun to mouse-click event on canvas.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1298
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1299 Arguments:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1300 fun -- a function with two arguments, the coordinates of the
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1301 clicked point on the canvas.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1302 num -- the number of the mouse-button, defaults to 1
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1303
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1304 Example (for a TurtleScreen instance named screen
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1305 and a Turtle instance named turtle):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1306
73815
6e03ab9950f6 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72984
diff changeset
1307 >>> screen.onclick(goto)
6e03ab9950f6 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72984
diff changeset
1308 >>> # Subsequently clicking into the TurtleScreen will
6e03ab9950f6 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72984
diff changeset
1309 >>> # make the turtle move to the clicked point.
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1310 >>> screen.onclick(None)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1311 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1312 self._onscreenclick(fun, btn, add)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1313
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1314 def onkey(self, fun, key):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1315 """Bind fun to key-release event of key.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1316
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1317 Arguments:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1318 fun -- a function with no arguments
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1319 key -- a string: key (e.g. "a") or key-symbol (e.g. "space")
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1320
51761
6ce96ce4d9af Merged revisions 69816 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 49758
diff changeset
1321 In order to be able to register key-events, TurtleScreen
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1322 must have focus. (See method listen.)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1323
73815
6e03ab9950f6 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72984
diff changeset
1324 Example (for a TurtleScreen instance named screen):
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1325
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1326 >>> def f():
73815
6e03ab9950f6 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72984
diff changeset
1327 ... fd(50)
6e03ab9950f6 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72984
diff changeset
1328 ... lt(60)
6e03ab9950f6 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72984
diff changeset
1329 ...
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1330 >>> screen.onkey(f, "Up")
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1331 >>> screen.listen()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1332
73815
6e03ab9950f6 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72984
diff changeset
1333 Subsequently the turtle can be moved by repeatedly pressing
6e03ab9950f6 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72984
diff changeset
1334 the up-arrow key, consequently drawing a hexagon
6e03ab9950f6 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72984
diff changeset
1335
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1336 """
60060
0bc04525af63 Merged revisions 79195,79425,79427,79450 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59834
diff changeset
1337 if fun is None:
56267
56b530029bac Merged revisions 75115 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 55049
diff changeset
1338 if key in self._keys:
56b530029bac Merged revisions 75115 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 55049
diff changeset
1339 self._keys.remove(key)
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1340 elif key not in self._keys:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1341 self._keys.append(key)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1342 self._onkey(fun, key)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1343
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1344 def listen(self, xdummy=None, ydummy=None):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1345 """Set focus on TurtleScreen (in order to collect key-events)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1346
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1347 No arguments.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1348 Dummy arguments are provided in order
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1349 to be able to pass listen to the onclick method.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1350
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1351 Example (for a TurtleScreen instance named screen):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1352 >>> screen.listen()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1353 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1354 self._listen()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1355
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1356 def ontimer(self, fun, t=0):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1357 """Install a timer, which calls fun after t milliseconds.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1358
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1359 Arguments:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1360 fun -- a function with no arguments.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1361 t -- a number >= 0
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1362
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1363 Example (for a TurtleScreen instance named screen):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1364
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1365 >>> running = True
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1366 >>> def f():
73815
6e03ab9950f6 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72984
diff changeset
1367 ... if running:
6e03ab9950f6 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72984
diff changeset
1368 ... fd(50)
6e03ab9950f6 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72984
diff changeset
1369 ... lt(60)
6e03ab9950f6 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72984
diff changeset
1370 ... screen.ontimer(f, 250)
6e03ab9950f6 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72984
diff changeset
1371 ...
6e03ab9950f6 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72984
diff changeset
1372 >>> f() # makes the turtle marching around
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1373 >>> running = False
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1374 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1375 self._ontimer(fun, t)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1376
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1377 def bgpic(self, picname=None):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1378 """Set background image or return name of current backgroundimage.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1379
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1380 Optional argument:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1381 picname -- a string, name of a gif-file or "nopic".
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1382
68503
ee5350f1d0c2 #11515: fix several typos. Patch by Piotr Kasprzyk.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 68256
diff changeset
1383 If picname is a filename, set the corresponding image as background.
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1384 If picname is "nopic", delete backgroundimage, if present.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1385 If picname is None, return the filename of the current backgroundimage.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1386
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1387 Example (for a TurtleScreen instance named screen):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1388 >>> screen.bgpic()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1389 'nopic'
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1390 >>> screen.bgpic("landscape.gif")
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1391 >>> screen.bgpic()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1392 'landscape.gif'
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1393 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1394 if picname is None:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1395 return self._bgpicname
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1396 if picname not in self._bgpics:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1397 self._bgpics[picname] = self._image(picname)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1398 self._setbgpic(self._bgpic, self._bgpics[picname])
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1399 self._bgpicname = picname
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1400
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1401 def screensize(self, canvwidth=None, canvheight=None, bg=None):
51761
6ce96ce4d9af Merged revisions 69816 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 49758
diff changeset
1402 """Resize the canvas the turtles are drawing on.
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1403
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1404 Optional arguments:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1405 canvwidth -- positive integer, new width of canvas in pixels
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1406 canvheight -- positive integer, new height of canvas in pixels
68588
cd8d3c2d3bce #11565: Fix several typos. Patch by Piotr Kasprzyk.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 68503
diff changeset
1407 bg -- colorstring or color-tuple, new backgroundcolor
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1408 If no arguments are given, return current (canvaswidth, canvasheight)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1409
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1410 Do not alter the drawing window. To observe hidden parts of
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1411 the canvas use the scrollbars. (Can make visible those parts
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1412 of a drawing, which were outside the canvas before!)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1413
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1414 Example (for a Turtle instance named turtle):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1415 >>> turtle.screensize(2000,1500)
73815
6e03ab9950f6 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72984
diff changeset
1416 >>> # e. g. to search for an erroneously escaped turtle ;-)
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1417 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1418 return self._resize(canvwidth, canvheight, bg)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1419
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1420 onscreenclick = onclick
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1421 resetscreen = reset
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1422 clearscreen = clear
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1423 addshape = register_shape
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1424
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1425 class TNavigator(object):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1426 """Navigation part of the RawTurtle.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1427 Implements methods for turtle movement.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1428 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1429 START_ORIENTATION = {
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1430 "standard": Vec2D(1.0, 0.0),
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1431 "world" : Vec2D(1.0, 0.0),
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1432 "logo" : Vec2D(0.0, 1.0) }
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1433 DEFAULT_MODE = "standard"
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1434 DEFAULT_ANGLEOFFSET = 0
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1435 DEFAULT_ANGLEORIENT = 1
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1436
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1437 def __init__(self, mode=DEFAULT_MODE):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1438 self._angleOffset = self.DEFAULT_ANGLEOFFSET
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1439 self._angleOrient = self.DEFAULT_ANGLEORIENT
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1440 self._mode = mode
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1441 self.undobuffer = None
9585
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
1442 self.degrees()
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1443 self._mode = None
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1444 self._setmode(mode)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1445 TNavigator.reset(self)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1446
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1447 def reset(self):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1448 """reset turtle to its initial values
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1449
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1450 Will be overwritten by parent class
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1451 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1452 self._position = Vec2D(0.0, 0.0)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1453 self._orient = TNavigator.START_ORIENTATION[self._mode]
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1454
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1455 def _setmode(self, mode=None):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1456 """Set turtle-mode to 'standard', 'world' or 'logo'.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1457 """
60060
0bc04525af63 Merged revisions 79195,79425,79427,79450 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59834
diff changeset
1458 if mode is None:
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1459 return self._mode
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1460 if mode not in ["standard", "logo", "world"]:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1461 return
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1462 self._mode = mode
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1463 if mode in ["standard", "world"]:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1464 self._angleOffset = 0
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1465 self._angleOrient = 1
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1466 else: # mode == "logo":
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1467 self._angleOffset = self._fullcircle/4.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1468 self._angleOrient = -1
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1469
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1470 def _setDegreesPerAU(self, fullcircle):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1471 """Helper function for degrees() and radians()"""
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1472 self._fullcircle = fullcircle
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1473 self._degreesPerAU = 360/fullcircle
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1474 if self._mode == "standard":
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1475 self._angleOffset = 0
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1476 else:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1477 self._angleOffset = fullcircle/4.
9585
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
1478
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
1479 def degrees(self, fullcircle=360.0):
38204
fa3c4ea99d95 Patch #1486962: Several bugs in the turtle Tk demo module were fixed
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
1480 """ Set angle measurement units to degrees.
fa3c4ea99d95 Patch #1486962: Several bugs in the turtle Tk demo module were fixed
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
1481
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1482 Optional argument:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1483 fullcircle - a number
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1484
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1485 Set angle measurement units, i. e. set number
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1486 of 'degrees' for a full circle. Dafault value is
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1487 360 degrees.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1488
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1489 Example (for a Turtle instance named turtle):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1490 >>> turtle.left(90)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1491 >>> turtle.heading()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1492 90
65954
947b5acc64bd Merged revisions 85930 via svnmerge from
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 60018
diff changeset
1493
947b5acc64bd Merged revisions 85930 via svnmerge from
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 60018
diff changeset
1494 Change angle measurement unit to grad (also known as gon,
947b5acc64bd Merged revisions 85930 via svnmerge from
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 60018
diff changeset
1495 grade, or gradian and equals 1/100-th of the right angle.)
947b5acc64bd Merged revisions 85930 via svnmerge from
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 60018
diff changeset
1496 >>> turtle.degrees(400.0)
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1497 >>> turtle.heading()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1498 100
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1499
38204
fa3c4ea99d95 Patch #1486962: Several bugs in the turtle Tk demo module were fixed
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
1500 """
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1501 self._setDegreesPerAU(fullcircle)
9585
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
1502
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
1503 def radians(self):
38204
fa3c4ea99d95 Patch #1486962: Several bugs in the turtle Tk demo module were fixed
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
1504 """ Set the angle measurement units to radians.
fa3c4ea99d95 Patch #1486962: Several bugs in the turtle Tk demo module were fixed
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
1505
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1506 No arguments.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1507
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1508 Example (for a Turtle instance named turtle):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1509 >>> turtle.heading()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1510 90
38204
fa3c4ea99d95 Patch #1486962: Several bugs in the turtle Tk demo module were fixed
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
1511 >>> turtle.radians()
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1512 >>> turtle.heading()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1513 1.5707963267948966
38204
fa3c4ea99d95 Patch #1486962: Several bugs in the turtle Tk demo module were fixed
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
1514 """
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1515 self._setDegreesPerAU(2*math.pi)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1516
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1517 def _go(self, distance):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1518 """move turtle forward by specified distance"""
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1519 ende = self._position + self._orient * distance
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1520 self._goto(ende)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1521
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1522 def _rotate(self, angle):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1523 """Turn turtle counterclockwise by specified angle if angle > 0."""
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1524 angle *= self._degreesPerAU
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1525 self._orient = self._orient.rotate(angle)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1526
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1527 def _goto(self, end):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1528 """move turtle to position end."""
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1529 self._position = end
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1530
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1531 def forward(self, distance):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1532 """Move the turtle forward by the specified distance.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1533
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1534 Aliases: forward | fd
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1535
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1536 Argument:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1537 distance -- a number (integer or float)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1538
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1539 Move the turtle forward by the specified distance, in the direction
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1540 the turtle is headed.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1541
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1542 Example (for a Turtle instance named turtle):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1543 >>> turtle.position()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1544 (0.00, 0.00)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1545 >>> turtle.forward(25)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1546 >>> turtle.position()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1547 (25.00,0.00)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1548 >>> turtle.forward(-75)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1549 >>> turtle.position()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1550 (-50.00,0.00)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1551 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1552 self._go(distance)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1553
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1554 def back(self, distance):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1555 """Move the turtle backward by distance.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1556
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1557 Aliases: back | backward | bk
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1558
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1559 Argument:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1560 distance -- a number
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1561
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1562 Move the turtle backward by distance ,opposite to the direction the
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1563 turtle is headed. Do not change the turtle's heading.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1564
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1565 Example (for a Turtle instance named turtle):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1566 >>> turtle.position()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1567 (0.00, 0.00)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1568 >>> turtle.backward(30)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1569 >>> turtle.position()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1570 (-30.00, 0.00)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1571 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1572 self._go(-distance)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1573
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1574 def right(self, angle):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1575 """Turn turtle right by angle units.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1576
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1577 Aliases: right | rt
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1578
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1579 Argument:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1580 angle -- a number (integer or float)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1581
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1582 Turn turtle right by angle units. (Units are by default degrees,
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1583 but can be set via the degrees() and radians() functions.)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1584 Angle orientation depends on mode. (See this.)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1585
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1586 Example (for a Turtle instance named turtle):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1587 >>> turtle.heading()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1588 22.0
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1589 >>> turtle.right(45)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1590 >>> turtle.heading()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1591 337.0
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1592 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1593 self._rotate(-angle)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1594
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1595 def left(self, angle):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1596 """Turn turtle left by angle units.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1597
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1598 Aliases: left | lt
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1599
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1600 Argument:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1601 angle -- a number (integer or float)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1602
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1603 Turn turtle left by angle units. (Units are by default degrees,
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1604 but can be set via the degrees() and radians() functions.)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1605 Angle orientation depends on mode. (See this.)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1606
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1607 Example (for a Turtle instance named turtle):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1608 >>> turtle.heading()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1609 22.0
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1610 >>> turtle.left(45)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1611 >>> turtle.heading()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1612 67.0
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1613 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1614 self._rotate(angle)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1615
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1616 def pos(self):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1617 """Return the turtle's current location (x,y), as a Vec2D-vector.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1618
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1619 Aliases: pos | position
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1620
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1621 No arguments.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1622
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1623 Example (for a Turtle instance named turtle):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1624 >>> turtle.pos()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1625 (0.00, 240.00)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1626 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1627 return self._position
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1628
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1629 def xcor(self):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1630 """ Return the turtle's x coordinate.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1631
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1632 No arguments.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1633
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1634 Example (for a Turtle instance named turtle):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1635 >>> reset()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1636 >>> turtle.left(60)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1637 >>> turtle.forward(100)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1638 >>> print turtle.xcor()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1639 50.0
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1640 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1641 return self._position[0]
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1642
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1643 def ycor(self):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1644 """ Return the turtle's y coordinate
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1645 ---
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1646 No arguments.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1647
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1648 Example (for a Turtle instance named turtle):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1649 >>> reset()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1650 >>> turtle.left(60)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1651 >>> turtle.forward(100)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1652 >>> print turtle.ycor()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1653 86.6025403784
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1654 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1655 return self._position[1]
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1656
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1657
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1658 def goto(self, x, y=None):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1659 """Move turtle to an absolute position.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1660
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1661 Aliases: setpos | setposition | goto:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1662
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1663 Arguments:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1664 x -- a number or a pair/vector of numbers
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1665 y -- a number None
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1666
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1667 call: goto(x, y) # two coordinates
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1668 --or: goto((x, y)) # a pair (tuple) of coordinates
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1669 --or: goto(vec) # e.g. as returned by pos()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1670
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1671 Move turtle to an absolute position. If the pen is down,
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1672 a line will be drawn. The turtle's orientation does not change.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1673
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1674 Example (for a Turtle instance named turtle):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1675 >>> tp = turtle.pos()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1676 >>> tp
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1677 (0.00, 0.00)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1678 >>> turtle.setpos(60,30)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1679 >>> turtle.pos()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1680 (60.00,30.00)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1681 >>> turtle.setpos((20,80))
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1682 >>> turtle.pos()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1683 (20.00,80.00)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1684 >>> turtle.setpos(tp)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1685 >>> turtle.pos()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1686 (0.00,0.00)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1687 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1688 if y is None:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1689 self._goto(Vec2D(*x))
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1690 else:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1691 self._goto(Vec2D(x, y))
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1692
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1693 def home(self):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1694 """Move turtle to the origin - coordinates (0,0).
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1695
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1696 No arguments.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1697
51761
6ce96ce4d9af Merged revisions 69816 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 49758
diff changeset
1698 Move turtle to the origin - coordinates (0,0) and set its
6ce96ce4d9af Merged revisions 69816 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 49758
diff changeset
1699 heading to its start-orientation (which depends on mode).
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1700
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1701 Example (for a Turtle instance named turtle):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1702 >>> turtle.home()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1703 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1704 self.goto(0, 0)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1705 self.setheading(0)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1706
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1707 def setx(self, x):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1708 """Set the turtle's first coordinate to x
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1709
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1710 Argument:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1711 x -- a number (integer or float)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1712
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1713 Set the turtle's first coordinate to x, leave second coordinate
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1714 unchanged.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1715
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1716 Example (for a Turtle instance named turtle):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1717 >>> turtle.position()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1718 (0.00, 240.00)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1719 >>> turtle.setx(10)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1720 >>> turtle.position()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1721 (10.00, 240.00)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1722 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1723 self._goto(Vec2D(x, self._position[1]))
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1724
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1725 def sety(self, y):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1726 """Set the turtle's second coordinate to y
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1727
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1728 Argument:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1729 y -- a number (integer or float)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1730
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1731 Set the turtle's first coordinate to x, second coordinate remains
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1732 unchanged.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1733
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1734 Example (for a Turtle instance named turtle):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1735 >>> turtle.position()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1736 (0.00, 40.00)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1737 >>> turtle.sety(-10)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1738 >>> turtle.position()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1739 (0.00, -10.00)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1740 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1741 self._goto(Vec2D(self._position[0], y))
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1742
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1743 def distance(self, x, y=None):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1744 """Return the distance from the turtle to (x,y) in turtle step units.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1745
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1746 Arguments:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1747 x -- a number or a pair/vector of numbers or a turtle instance
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1748 y -- a number None None
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1749
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1750 call: distance(x, y) # two coordinates
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1751 --or: distance((x, y)) # a pair (tuple) of coordinates
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1752 --or: distance(vec) # e.g. as returned by pos()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1753 --or: distance(mypen) # where mypen is another turtle
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1754
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1755 Example (for a Turtle instance named turtle):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1756 >>> turtle.pos()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1757 (0.00, 0.00)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1758 >>> turtle.distance(30,40)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1759 50.0
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1760 >>> pen = Turtle()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1761 >>> pen.forward(77)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1762 >>> turtle.distance(pen)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1763 77.0
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1764 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1765 if y is not None:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1766 pos = Vec2D(x, y)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1767 if isinstance(x, Vec2D):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1768 pos = x
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1769 elif isinstance(x, tuple):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1770 pos = Vec2D(*x)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1771 elif isinstance(x, TNavigator):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1772 pos = x._position
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1773 return abs(pos - self._position)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1774
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1775 def towards(self, x, y=None):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1776 """Return the angle of the line from the turtle's position to (x, y).
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1777
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1778 Arguments:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1779 x -- a number or a pair/vector of numbers or a turtle instance
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1780 y -- a number None None
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1781
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1782 call: distance(x, y) # two coordinates
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1783 --or: distance((x, y)) # a pair (tuple) of coordinates
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1784 --or: distance(vec) # e.g. as returned by pos()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1785 --or: distance(mypen) # where mypen is another turtle
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1786
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1787 Return the angle, between the line from turtle-position to position
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1788 specified by x, y and the turtle's start orientation. (Depends on
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1789 modes - "standard" or "logo")
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1790
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1791 Example (for a Turtle instance named turtle):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1792 >>> turtle.pos()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1793 (10.00, 10.00)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1794 >>> turtle.towards(0,0)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1795 225.0
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1796 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1797 if y is not None:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1798 pos = Vec2D(x, y)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1799 if isinstance(x, Vec2D):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1800 pos = x
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1801 elif isinstance(x, tuple):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1802 pos = Vec2D(*x)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1803 elif isinstance(x, TNavigator):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1804 pos = x._position
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1805 x, y = pos - self._position
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1806 result = round(math.atan2(y, x)*180.0/math.pi, 10) % 360.0
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1807 result /= self._degreesPerAU
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1808 return (self._angleOffset + self._angleOrient*result) % self._fullcircle
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1809
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1810 def heading(self):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1811 """ Return the turtle's current heading.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1812
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1813 No arguments.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1814
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1815 Example (for a Turtle instance named turtle):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1816 >>> turtle.left(67)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1817 >>> turtle.heading()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1818 67.0
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1819 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1820 x, y = self._orient
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1821 result = round(math.atan2(y, x)*180.0/math.pi, 10) % 360.0
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1822 result /= self._degreesPerAU
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1823 return (self._angleOffset + self._angleOrient*result) % self._fullcircle
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1824
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1825 def setheading(self, to_angle):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1826 """Set the orientation of the turtle to to_angle.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1827
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1828 Aliases: setheading | seth
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1829
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1830 Argument:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1831 to_angle -- a number (integer or float)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1832
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1833 Set the orientation of the turtle to to_angle.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1834 Here are some common directions in degrees:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1835
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1836 standard - mode: logo-mode:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1837 -------------------|--------------------
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1838 0 - east 0 - north
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1839 90 - north 90 - east
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1840 180 - west 180 - south
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1841 270 - south 270 - west
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1842
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1843 Example (for a Turtle instance named turtle):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1844 >>> turtle.setheading(90)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1845 >>> turtle.heading()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1846 90
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1847 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1848 angle = (to_angle - self.heading())*self._angleOrient
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1849 full = self._fullcircle
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1850 angle = (angle+full/2.)%full - full/2.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1851 self._rotate(angle)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1852
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1853 def circle(self, radius, extent = None, steps = None):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1854 """ Draw a circle with given radius.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1855
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1856 Arguments:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1857 radius -- a number
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1858 extent (optional) -- a number
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1859 steps (optional) -- an integer
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1860
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1861 Draw a circle with given radius. The center is radius units left
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1862 of the turtle; extent - an angle - determines which part of the
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1863 circle is drawn. If extent is not given, draw the entire circle.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1864 If extent is not a full circle, one endpoint of the arc is the
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1865 current pen position. Draw the arc in counterclockwise direction
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1866 if radius is positive, otherwise in clockwise direction. Finally
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1867 the direction of the turtle is changed by the amount of extent.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1868
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1869 As the circle is approximated by an inscribed regular polygon,
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1870 steps determines the number of steps to use. If not given,
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1871 it will be calculated automatically. Maybe used to draw regular
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1872 polygons.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1873
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1874 call: circle(radius) # full circle
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1875 --or: circle(radius, extent) # arc
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1876 --or: circle(radius, extent, steps)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1877 --or: circle(radius, steps=6) # 6-sided polygon
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1878
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1879 Example (for a Turtle instance named turtle):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1880 >>> turtle.circle(50)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1881 >>> turtle.circle(120, 180) # semicircle
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1882 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1883 if self.undobuffer:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1884 self.undobuffer.push(["seq"])
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1885 self.undobuffer.cumulate = True
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1886 speed = self.speed()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1887 if extent is None:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1888 extent = self._fullcircle
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1889 if steps is None:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1890 frac = abs(extent)/self._fullcircle
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1891 steps = 1+int(min(11+abs(radius)/6.0, 59.0)*frac)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1892 w = 1.0 * extent / steps
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1893 w2 = 0.5 * w
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1894 l = 2.0 * radius * math.sin(w2*math.pi/180.0*self._degreesPerAU)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1895 if radius < 0:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1896 l, w, w2 = -l, -w, -w2
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1897 tr = self.tracer()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1898 dl = self._delay()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1899 if speed == 0:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1900 self.tracer(0, 0)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1901 else:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1902 self.speed(0)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1903 self._rotate(w2)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1904 for i in range(steps):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1905 self.speed(speed)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1906 self._go(l)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1907 self.speed(0)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1908 self._rotate(w)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1909 self._rotate(-w2)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1910 if speed == 0:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1911 self.tracer(tr, dl)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1912 self.speed(speed)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1913 if self.undobuffer:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1914 self.undobuffer.cumulate = False
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1915
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1916 ## three dummy methods to be implemented by child class:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1917
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1918 def speed(self, s=0):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1919 """dummy method - to be overwritten by child class"""
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1920 def tracer(self, a=None, b=None):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1921 """dummy method - to be overwritten by child class"""
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1922 def _delay(self, n=None):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1923 """dummy method - to be overwritten by child class"""
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1924
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1925 fd = forward
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1926 bk = back
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1927 backward = back
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1928 rt = right
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1929 lt = left
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1930 position = pos
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1931 setpos = goto
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1932 setposition = goto
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1933 seth = setheading
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1934
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1935
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1936 class TPen(object):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1937 """Drawing part of the RawTurtle.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1938 Implements drawing properties.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1939 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1940 def __init__(self, resizemode=_CFG["resizemode"]):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1941 self._resizemode = resizemode # or "user" or "noresize"
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1942 self.undobuffer = None
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1943 TPen._reset(self)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1944
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1945 def _reset(self, pencolor=_CFG["pencolor"],
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1946 fillcolor=_CFG["fillcolor"]):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1947 self._pensize = 1
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1948 self._shown = True
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1949 self._pencolor = pencolor
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1950 self._fillcolor = fillcolor
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1951 self._drawing = True
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1952 self._speed = 3
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1953 self._stretchfactor = (1, 1)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1954 self._tilt = 0
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1955 self._outlinewidth = 1
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1956 ### self.screen = None # to override by child class
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1957
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1958 def resizemode(self, rmode=None):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1959 """Set resizemode to one of the values: "auto", "user", "noresize".
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1960
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1961 (Optional) Argument:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1962 rmode -- one of the strings "auto", "user", "noresize"
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1963
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1964 Different resizemodes have the following effects:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1965 - "auto" adapts the appearance of the turtle
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1966 corresponding to the value of pensize.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1967 - "user" adapts the appearance of the turtle according to the
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1968 values of stretchfactor and outlinewidth (outline),
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1969 which are set by shapesize()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1970 - "noresize" no adaption of the turtle's appearance takes place.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1971 If no argument is given, return current resizemode.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1972 resizemode("user") is called by a call of shapesize with arguments.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1973
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1974
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1975 Examples (for a Turtle instance named turtle):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1976 >>> turtle.resizemode("noresize")
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1977 >>> turtle.resizemode()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1978 'noresize'
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1979 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1980 if rmode is None:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1981 return self._resizemode
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1982 rmode = rmode.lower()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1983 if rmode in ["auto", "user", "noresize"]:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1984 self.pen(resizemode=rmode)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1985
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1986 def pensize(self, width=None):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1987 """Set or return the line thickness.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1988
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1989 Aliases: pensize | width
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1990
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1991 Argument:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1992 width -- positive number
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1993
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1994 Set the line thickness to width or return it. If resizemode is set
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1995 to "auto" and turtleshape is a polygon, that polygon is drawn with
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1996 the same line thickness. If no argument is given, current pensize
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1997 is returned.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1998
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1999 Example (for a Turtle instance named turtle):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2000 >>> turtle.pensize()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2001 1
73815
6e03ab9950f6 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72984
diff changeset
2002 >>> turtle.pensize(10) # from here on lines of width 10 are drawn
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2003 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2004 if width is None:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2005 return self._pensize
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2006 self.pen(pensize=width)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2007
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2008
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2009 def penup(self):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2010 """Pull the pen up -- no drawing when moving.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2011
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2012 Aliases: penup | pu | up
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2013
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2014 No argument
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2015
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2016 Example (for a Turtle instance named turtle):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2017 >>> turtle.penup()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2018 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2019 if not self._drawing:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2020 return
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2021 self.pen(pendown=False)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2022
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2023 def pendown(self):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2024 """Pull the pen down -- drawing when moving.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2025
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2026 Aliases: pendown | pd | down
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2027
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2028 No argument.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2029
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2030 Example (for a Turtle instance named turtle):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2031 >>> turtle.pendown()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2032 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2033 if self._drawing:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2034 return
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2035 self.pen(pendown=True)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2036
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2037 def isdown(self):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2038 """Return True if pen is down, False if it's up.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2039
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2040 No argument.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2041
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2042 Example (for a Turtle instance named turtle):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2043 >>> turtle.penup()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2044 >>> turtle.isdown()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2045 False
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2046 >>> turtle.pendown()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2047 >>> turtle.isdown()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2048 True
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2049 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2050 return self._drawing
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2051
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2052 def speed(self, speed=None):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2053 """ Return or set the turtle's speed.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2054
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2055 Optional argument:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2056 speed -- an integer in the range 0..10 or a speedstring (see below)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2057
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2058 Set the turtle's speed to an integer value in the range 0 .. 10.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2059 If no argument is given: return current speed.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2060
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2061 If input is a number greater than 10 or smaller than 0.5,
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2062 speed is set to 0.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2063 Speedstrings are mapped to speedvalues in the following way:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2064 'fastest' : 0
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2065 'fast' : 10
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2066 'normal' : 6
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2067 'slow' : 3
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2068 'slowest' : 1
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2069 speeds from 1 to 10 enforce increasingly faster animation of
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2070 line drawing and turtle turning.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2071
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2072 Attention:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2073 speed = 0 : *no* animation takes place. forward/back makes turtle jump
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2074 and likewise left/right make the turtle turn instantly.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2075
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2076 Example (for a Turtle instance named turtle):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2077 >>> turtle.speed(3)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2078 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2079 speeds = {'fastest':0, 'fast':10, 'normal':6, 'slow':3, 'slowest':1 }
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2080 if speed is None:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2081 return self._speed
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2082 if speed in speeds:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2083 speed = speeds[speed]
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2084 elif 0.5 < speed < 10.5:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2085 speed = int(round(speed))
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2086 else:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2087 speed = 0
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2088 self.pen(speed=speed)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2089
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2090 def color(self, *args):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2091 """Return or set the pencolor and fillcolor.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2092
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2093 Arguments:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2094 Several input formats are allowed.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2095 They use 0, 1, 2, or 3 arguments as follows:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2096
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2097 color()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2098 Return the current pencolor and the current fillcolor
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2099 as a pair of color specification strings as are returned
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2100 by pencolor and fillcolor.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2101 color(colorstring), color((r,g,b)), color(r,g,b)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2102 inputs as in pencolor, set both, fillcolor and pencolor,
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2103 to the given value.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2104 color(colorstring1, colorstring2),
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2105 color((r1,g1,b1), (r2,g2,b2))
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2106 equivalent to pencolor(colorstring1) and fillcolor(colorstring2)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2107 and analogously, if the other input format is used.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2108
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2109 If turtleshape is a polygon, outline and interior of that polygon
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2110 is drawn with the newly set colors.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2111 For mor info see: pencolor, fillcolor
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2112
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2113 Example (for a Turtle instance named turtle):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2114 >>> turtle.color('red', 'green')
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2115 >>> turtle.color()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2116 ('red', 'green')
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2117 >>> colormode(255)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2118 >>> color((40, 80, 120), (160, 200, 240))
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2119 >>> color()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2120 ('#285078', '#a0c8f0')
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2121 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2122 if args:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2123 l = len(args)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2124 if l == 1:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2125 pcolor = fcolor = args[0]
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2126 elif l == 2:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2127 pcolor, fcolor = args
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2128 elif l == 3:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2129 pcolor = fcolor = args
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2130 pcolor = self._colorstr(pcolor)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2131 fcolor = self._colorstr(fcolor)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2132 self.pen(pencolor=pcolor, fillcolor=fcolor)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2133 else:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2134 return self._color(self._pencolor), self._color(self._fillcolor)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2135
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2136 def pencolor(self, *args):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2137 """ Return or set the pencolor.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2138
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2139 Arguments:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2140 Four input formats are allowed:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2141 - pencolor()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2142 Return the current pencolor as color specification string,
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2143 possibly in hex-number format (see example).
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2144 May be used as input to another color/pencolor/fillcolor call.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2145 - pencolor(colorstring)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2146 s is a Tk color specification string, such as "red" or "yellow"
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2147 - pencolor((r, g, b))
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2148 *a tuple* of r, g, and b, which represent, an RGB color,
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2149 and each of r, g, and b are in the range 0..colormode,
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2150 where colormode is either 1.0 or 255
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2151 - pencolor(r, g, b)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2152 r, g, and b represent an RGB color, and each of r, g, and b
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2153 are in the range 0..colormode
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2154
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2155 If turtleshape is a polygon, the outline of that polygon is drawn
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2156 with the newly set pencolor.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2157
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2158 Example (for a Turtle instance named turtle):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2159 >>> turtle.pencolor('brown')
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2160 >>> tup = (0.2, 0.8, 0.55)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2161 >>> turtle.pencolor(tup)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2162 >>> turtle.pencolor()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2163 '#33cc8c'
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2164 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2165 if args:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2166 color = self._colorstr(args)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2167 if color == self._pencolor:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2168 return
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2169 self.pen(pencolor=color)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2170 else:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2171 return self._color(self._pencolor)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2172
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2173 def fillcolor(self, *args):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2174 """ Return or set the fillcolor.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2175
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2176 Arguments:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2177 Four input formats are allowed:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2178 - fillcolor()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2179 Return the current fillcolor as color specification string,
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2180 possibly in hex-number format (see example).
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2181 May be used as input to another color/pencolor/fillcolor call.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2182 - fillcolor(colorstring)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2183 s is a Tk color specification string, such as "red" or "yellow"
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2184 - fillcolor((r, g, b))
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2185 *a tuple* of r, g, and b, which represent, an RGB color,
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2186 and each of r, g, and b are in the range 0..colormode,
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2187 where colormode is either 1.0 or 255
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2188 - fillcolor(r, g, b)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2189 r, g, and b represent an RGB color, and each of r, g, and b
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2190 are in the range 0..colormode
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2191
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2192 If turtleshape is a polygon, the interior of that polygon is drawn
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2193 with the newly set fillcolor.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2194
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2195 Example (for a Turtle instance named turtle):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2196 >>> turtle.fillcolor('violet')
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2197 >>> col = turtle.pencolor()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2198 >>> turtle.fillcolor(col)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2199 >>> turtle.fillcolor(0, .5, 0)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2200 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2201 if args:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2202 color = self._colorstr(args)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2203 if color == self._fillcolor:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2204 return
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2205 self.pen(fillcolor=color)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2206 else:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2207 return self._color(self._fillcolor)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2208
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2209 def showturtle(self):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2210 """Makes the turtle visible.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2211
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2212 Aliases: showturtle | st
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2213
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2214 No argument.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2215
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2216 Example (for a Turtle instance named turtle):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2217 >>> turtle.hideturtle()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2218 >>> turtle.showturtle()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2219 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2220 self.pen(shown=True)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2221
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2222 def hideturtle(self):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2223 """Makes the turtle invisible.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2224
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2225 Aliases: hideturtle | ht
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2226
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2227 No argument.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2228
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2229 It's a good idea to do this while you're in the
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2230 middle of a complicated drawing, because hiding
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2231 the turtle speeds up the drawing observably.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2232
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2233 Example (for a Turtle instance named turtle):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2234 >>> turtle.hideturtle()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2235 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2236 self.pen(shown=False)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2237
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2238 def isvisible(self):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2239 """Return True if the Turtle is shown, False if it's hidden.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2240
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2241 No argument.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2242
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2243 Example (for a Turtle instance named turtle):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2244 >>> turtle.hideturtle()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2245 >>> print turtle.isvisible():
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2246 False
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2247 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2248 return self._shown
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2249
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2250 def pen(self, pen=None, **pendict):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2251 """Return or set the pen's attributes.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2252
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2253 Arguments:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2254 pen -- a dictionary with some or all of the below listed keys.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2255 **pendict -- one or more keyword-arguments with the below
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2256 listed keys as keywords.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2257
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2258 Return or set the pen's attributes in a 'pen-dictionary'
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2259 with the following key/value pairs:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2260 "shown" : True/False
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2261 "pendown" : True/False
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2262 "pencolor" : color-string or color-tuple
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2263 "fillcolor" : color-string or color-tuple
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2264 "pensize" : positive number
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2265 "speed" : number in range 0..10
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2266 "resizemode" : "auto" or "user" or "noresize"
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2267 "stretchfactor": (positive number, positive number)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2268 "outline" : positive number
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2269 "tilt" : number
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2270
51761
6ce96ce4d9af Merged revisions 69816 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 49758
diff changeset
2271 This dictionary can be used as argument for a subsequent
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2272 pen()-call to restore the former pen-state. Moreover one
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2273 or more of these attributes can be provided as keyword-arguments.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2274 This can be used to set several pen attributes in one statement.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2275
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2276
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2277 Examples (for a Turtle instance named turtle):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2278 >>> turtle.pen(fillcolor="black", pencolor="red", pensize=10)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2279 >>> turtle.pen()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2280 {'pensize': 10, 'shown': True, 'resizemode': 'auto', 'outline': 1,
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2281 'pencolor': 'red', 'pendown': True, 'fillcolor': 'black',
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2282 'stretchfactor': (1,1), 'speed': 3}
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2283 >>> penstate=turtle.pen()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2284 >>> turtle.color("yellow","")
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2285 >>> turtle.penup()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2286 >>> turtle.pen()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2287 {'pensize': 10, 'shown': True, 'resizemode': 'auto', 'outline': 1,
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2288 'pencolor': 'yellow', 'pendown': False, 'fillcolor': '',
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2289 'stretchfactor': (1,1), 'speed': 3}
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2290 >>> p.pen(penstate, fillcolor="green")
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2291 >>> p.pen()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2292 {'pensize': 10, 'shown': True, 'resizemode': 'auto', 'outline': 1,
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2293 'pencolor': 'red', 'pendown': True, 'fillcolor': 'green',
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2294 'stretchfactor': (1,1), 'speed': 3}
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2295 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2296 _pd = {"shown" : self._shown,
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2297 "pendown" : self._drawing,
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2298 "pencolor" : self._pencolor,
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2299 "fillcolor" : self._fillcolor,
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2300 "pensize" : self._pensize,
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2301 "speed" : self._speed,
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2302 "resizemode" : self._resizemode,
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2303 "stretchfactor" : self._stretchfactor,
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2304 "outline" : self._outlinewidth,
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2305 "tilt" : self._tilt
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2306 }
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2307
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2308 if not (pen or pendict):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2309 return _pd
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2310
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2311 if isinstance(pen, dict):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2312 p = pen
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2313 else:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2314 p = {}
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2315 p.update(pendict)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2316
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2317 _p_buf = {}
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2318 for key in p:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2319 _p_buf[key] = _pd[key]
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2320
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2321 if self.undobuffer:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2322 self.undobuffer.push(("pen", _p_buf))
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2323
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2324 newLine = False
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2325 if "pendown" in p:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2326 if self._drawing != p["pendown"]:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2327 newLine = True
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2328 if "pencolor" in p:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2329 if isinstance(p["pencolor"], tuple):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2330 p["pencolor"] = self._colorstr((p["pencolor"],))
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2331 if self._pencolor != p["pencolor"]:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2332 newLine = True
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2333 if "pensize" in p:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2334 if self._pensize != p["pensize"]:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2335 newLine = True
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2336 if newLine:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2337 self._newLine()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2338 if "pendown" in p:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2339 self._drawing = p["pendown"]
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2340 if "pencolor" in p:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2341 self._pencolor = p["pencolor"]
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2342 if "pensize" in p:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2343 self._pensize = p["pensize"]
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2344 if "fillcolor" in p:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2345 if isinstance(p["fillcolor"], tuple):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2346 p["fillcolor"] = self._colorstr((p["fillcolor"],))
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2347 self._fillcolor = p["fillcolor"]
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2348 if "speed" in p:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2349 self._speed = p["speed"]
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2350 if "resizemode" in p:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2351 self._resizemode = p["resizemode"]
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2352 if "stretchfactor" in p:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2353 sf = p["stretchfactor"]
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2354 if isinstance(sf, (int, float)):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2355 sf = (sf, sf)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2356 self._stretchfactor = sf
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2357 if "outline" in p:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2358 self._outlinewidth = p["outline"]
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2359 if "shown" in p:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2360 self._shown = p["shown"]
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2361 if "tilt" in p:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2362 self._tilt = p["tilt"]
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2363 self._update()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2364
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2365 ## three dummy methods to be implemented by child class:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2366
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2367 def _newLine(self, usePos = True):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2368 """dummy method - to be overwritten by child class"""
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2369 def _update(self, count=True, forced=False):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2370 """dummy method - to be overwritten by child class"""
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2371 def _color(self, args):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2372 """dummy method - to be overwritten by child class"""
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2373 def _colorstr(self, args):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2374 """dummy method - to be overwritten by child class"""
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2375
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2376 width = pensize
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2377 up = penup
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2378 pu = penup
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2379 pd = pendown
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2380 down = pendown
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2381 st = showturtle
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2382 ht = hideturtle
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2383
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2384
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2385 class _TurtleImage(object):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2386 """Helper class: Datatype to store Turtle attributes
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2387 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2388
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2389 def __init__(self, screen, shapeIndex):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2390 self.screen = screen
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2391 self._type = None
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2392 self._setshape(shapeIndex)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2393
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2394 def _setshape(self, shapeIndex):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2395 screen = self.screen # RawTurtle.screens[self.screenIndex]
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2396 self.shapeIndex = shapeIndex
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2397 if self._type == "polygon" == screen._shapes[shapeIndex]._type:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2398 return
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2399 if self._type == "image" == screen._shapes[shapeIndex]._type:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2400 return
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2401 if self._type in ["image", "polygon"]:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2402 screen._delete(self._item)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2403 elif self._type == "compound":
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2404 for item in self._item:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2405 screen._delete(item)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2406 self._type = screen._shapes[shapeIndex]._type
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2407 if self._type == "polygon":
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2408 self._item = screen._createpoly()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2409 elif self._type == "image":
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2410 self._item = screen._createimage(screen._shapes["blank"]._data)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2411 elif self._type == "compound":
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2412 self._item = [screen._createpoly() for item in
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2413 screen._shapes[shapeIndex]._data]
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2414
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2415
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2416 class RawTurtle(TPen, TNavigator):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2417 """Animation part of the RawTurtle.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2418 Puts RawTurtle upon a TurtleScreen and provides tools for
51761
6ce96ce4d9af Merged revisions 69816 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 49758
diff changeset
2419 its animation.
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2420 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2421 screens = []
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2422
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2423 def __init__(self, canvas=None,
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2424 shape=_CFG["shape"],
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2425 undobuffersize=_CFG["undobuffersize"],
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2426 visible=_CFG["visible"]):
49344
d88f1f2f0213 Issue #3965: Allow repeated calls to turtle.Screen, by making it a
Martin v. Löwis <martin@v.loewis.de>
parents: 49291
diff changeset
2427 if isinstance(canvas, _Screen):
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2428 self.screen = canvas
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2429 elif isinstance(canvas, TurtleScreen):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2430 if canvas not in RawTurtle.screens:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2431 RawTurtle.screens.append(canvas)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2432 self.screen = canvas
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2433 elif isinstance(canvas, (ScrolledCanvas, Canvas)):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2434 for screen in RawTurtle.screens:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2435 if screen.cv == canvas:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2436 self.screen = screen
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2437 break
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2438 else:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2439 self.screen = TurtleScreen(canvas)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2440 RawTurtle.screens.append(self.screen)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2441 else:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2442 raise TurtleGraphicsError("bad cavas argument %s" % canvas)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2443
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2444 screen = self.screen
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2445 TNavigator.__init__(self, screen.mode())
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2446 TPen.__init__(self)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2447 screen._turtles.append(self)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2448 self.drawingLineItem = screen._createline()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2449 self.turtle = _TurtleImage(screen, shape)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2450 self._poly = None
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2451 self._creatingPoly = False
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2452 self._fillitem = self._fillpath = None
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2453 self._shown = visible
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2454 self._hidden_from_screen = False
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2455 self.currentLineItem = screen._createline()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2456 self.currentLine = [self._position]
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2457 self.items = [self.currentLineItem]
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2458 self.stampItems = []
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2459 self._undobuffersize = undobuffersize
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2460 self.undobuffer = Tbuffer(undobuffersize)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2461 self._update()
9585
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
2462
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
2463 def reset(self):
51761
6ce96ce4d9af Merged revisions 69816 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 49758
diff changeset
2464 """Delete the turtle's drawings and restore its default values.
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2465
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2466 No argument.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2467 ,
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2468 Delete the turtle's drawings from the screen, re-center the turtle
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2469 and set variables to the default values.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2470
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2471 Example (for a Turtle instance named turtle):
38204
fa3c4ea99d95 Patch #1486962: Several bugs in the turtle Tk demo module were fixed
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
2472 >>> turtle.position()
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2473 (0.00,-22.00)
38204
fa3c4ea99d95 Patch #1486962: Several bugs in the turtle Tk demo module were fixed
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
2474 >>> turtle.heading()
fa3c4ea99d95 Patch #1486962: Several bugs in the turtle Tk demo module were fixed
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
2475 100.0
fa3c4ea99d95 Patch #1486962: Several bugs in the turtle Tk demo module were fixed
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
2476 >>> turtle.reset()
fa3c4ea99d95 Patch #1486962: Several bugs in the turtle Tk demo module were fixed
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
2477 >>> turtle.position()
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2478 (0.00,0.00)
38204
fa3c4ea99d95 Patch #1486962: Several bugs in the turtle Tk demo module were fixed
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
2479 >>> turtle.heading()
fa3c4ea99d95 Patch #1486962: Several bugs in the turtle Tk demo module were fixed
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
2480 0.0
fa3c4ea99d95 Patch #1486962: Several bugs in the turtle Tk demo module were fixed
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
2481 """
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2482 TNavigator.reset(self)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2483 TPen._reset(self)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2484 self._clear()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2485 self._drawturtle()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2486 self._update()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2487
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2488 def setundobuffer(self, size):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2489 """Set or disable undobuffer.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2490
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2491 Argument:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2492 size -- an integer or None
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2493
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2494 If size is an integer an empty undobuffer of given size is installed.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2495 Size gives the maximum number of turtle-actions that can be undone
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2496 by the undo() function.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2497 If size is None, no undobuffer is present.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2498
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2499 Example (for a Turtle instance named turtle):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2500 >>> turtle.setundobuffer(42)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2501 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2502 if size is None:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2503 self.undobuffer = None
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2504 else:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2505 self.undobuffer = Tbuffer(size)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2506
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2507 def undobufferentries(self):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2508 """Return count of entries in the undobuffer.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2509
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2510 No argument.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2511
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2512 Example (for a Turtle instance named turtle):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2513 >>> while undobufferentries():
73815
6e03ab9950f6 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72984
diff changeset
2514 ... undo()
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2515 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2516 if self.undobuffer is None:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2517 return 0
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2518 return self.undobuffer.nr_of_items()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2519
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2520 def _clear(self):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2521 """Delete all of pen's drawings"""
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2522 self._fillitem = self._fillpath = None
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2523 for item in self.items:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2524 self.screen._delete(item)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2525 self.currentLineItem = self.screen._createline()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2526 self.currentLine = []
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2527 if self._drawing:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2528 self.currentLine.append(self._position)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2529 self.items = [self.currentLineItem]
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2530 self.clearstamps()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2531 self.setundobuffer(self._undobuffersize)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2532
9585
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
2533
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
2534 def clear(self):
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2535 """Delete the turtle's drawings from the screen. Do not move turtle.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2536
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2537 No arguments.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2538
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2539 Delete the turtle's drawings from the screen. Do not move turtle.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2540 State and position of the turtle as well as drawings of other
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2541 turtles are not affected.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2542
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2543 Examples (for a Turtle instance named turtle):
38204
fa3c4ea99d95 Patch #1486962: Several bugs in the turtle Tk demo module were fixed
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
2544 >>> turtle.clear()
fa3c4ea99d95 Patch #1486962: Several bugs in the turtle Tk demo module were fixed
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
2545 """
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2546 self._clear()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2547 self._update()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2548
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2549 def _update_data(self):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2550 self.screen._incrementudc()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2551 if self.screen._updatecounter != 0:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2552 return
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2553 if len(self.currentLine)>1:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2554 self.screen._drawline(self.currentLineItem, self.currentLine,
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2555 self._pencolor, self._pensize)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2556
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2557 def _update(self):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2558 """Perform a Turtle-data update.
38204
fa3c4ea99d95 Patch #1486962: Several bugs in the turtle Tk demo module were fixed
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
2559 """
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2560 screen = self.screen
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2561 if screen._tracing == 0:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2562 return
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2563 elif screen._tracing == 1:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2564 self._update_data()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2565 self._drawturtle()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2566 screen._update() # TurtleScreenBase
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2567 screen._delay(screen._delayvalue) # TurtleScreenBase
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2568 else:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2569 self._update_data()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2570 if screen._updatecounter == 0:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2571 for t in screen.turtles():
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2572 t._drawturtle()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2573 screen._update()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2574
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2575 def tracer(self, flag=None, delay=None):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2576 """Turns turtle animation on/off and set delay for update drawings.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2577
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2578 Optional arguments:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2579 n -- nonnegative integer
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2580 delay -- nonnegative integer
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2581
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2582 If n is given, only each n-th regular screen update is really performed.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2583 (Can be used to accelerate the drawing of complex graphics.)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2584 Second arguments sets delay value (see RawTurtle.delay())
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2585
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2586 Example (for a Turtle instance named turtle):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2587 >>> turtle.tracer(8, 25)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2588 >>> dist = 2
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2589 >>> for i in range(200):
73815
6e03ab9950f6 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72984
diff changeset
2590 ... turtle.fd(dist)
6e03ab9950f6 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72984
diff changeset
2591 ... turtle.rt(90)
6e03ab9950f6 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72984
diff changeset
2592 ... dist += 2
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2593 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2594 return self.screen.tracer(flag, delay)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2595
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2596 def _color(self, args):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2597 return self.screen._color(args)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2598
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2599 def _colorstr(self, args):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2600 return self.screen._colorstr(args)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2601
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2602 def _cc(self, args):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2603 """Convert colortriples to hexstrings.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2604 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2605 if isinstance(args, str):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2606 return args
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2607 try:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2608 r, g, b = args
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2609 except:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2610 raise TurtleGraphicsError("bad color arguments: %s" % str(args))
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2611 if self.screen._colormode == 1.0:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2612 r, g, b = [round(255.0*x) for x in (r, g, b)]
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2613 if not ((0 <= r <= 255) and (0 <= g <= 255) and (0 <= b <= 255)):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2614 raise TurtleGraphicsError("bad color sequence: %s" % str(args))
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2615 return "#%02x%02x%02x" % (r, g, b)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2616
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2617 def clone(self):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2618 """Create and return a clone of the turtle.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2619
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2620 No argument.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2621
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2622 Create and return a clone of the turtle with same position, heading
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2623 and turtle properties.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2624
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2625 Example (for a Turtle instance named mick):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2626 mick = Turtle()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2627 joe = mick.clone()
38204
fa3c4ea99d95 Patch #1486962: Several bugs in the turtle Tk demo module were fixed
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
2628 """
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2629 screen = self.screen
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2630 self._newLine(self._drawing)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2631
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2632 turtle = self.turtle
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2633 self.screen = None
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2634 self.turtle = None # too make self deepcopy-able
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2635
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2636 q = deepcopy(self)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2637
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2638 self.screen = screen
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2639 self.turtle = turtle
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2640
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2641 q.screen = screen
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2642 q.turtle = _TurtleImage(screen, self.turtle.shapeIndex)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2643
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2644 screen._turtles.append(q)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2645 ttype = screen._shapes[self.turtle.shapeIndex]._type
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2646 if ttype == "polygon":
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2647 q.turtle._item = screen._createpoly()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2648 elif ttype == "image":
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2649 q.turtle._item = screen._createimage(screen._shapes["blank"]._data)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2650 elif ttype == "compound":
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2651 q.turtle._item = [screen._createpoly() for item in
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2652 screen._shapes[self.turtle.shapeIndex]._data]
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2653 q.currentLineItem = screen._createline()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2654 q._update()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2655 return q
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2656
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2657 def shape(self, name=None):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2658 """Set turtle shape to shape with given name / return current shapename.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2659
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2660 Optional argument:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2661 name -- a string, which is a valid shapename
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2662
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2663 Set turtle shape to shape with given name or, if name is not given,
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2664 return name of current shape.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2665 Shape with name must exist in the TurtleScreen's shape dictionary.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2666 Initially there are the following polygon shapes:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2667 'arrow', 'turtle', 'circle', 'square', 'triangle', 'classic'.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2668 To learn about how to deal with shapes see Screen-method register_shape.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2669
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2670 Example (for a Turtle instance named turtle):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2671 >>> turtle.shape()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2672 'arrow'
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2673 >>> turtle.shape("turtle")
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2674 >>> turtle.shape()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2675 'turtle'
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2676 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2677 if name is None:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2678 return self.turtle.shapeIndex
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2679 if not name in self.screen.getshapes():
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2680 raise TurtleGraphicsError("There is no shape named %s" % name)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2681 self.turtle._setshape(name)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2682 self._update()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2683
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2684 def shapesize(self, stretch_wid=None, stretch_len=None, outline=None):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2685 """Set/return turtle's stretchfactors/outline. Set resizemode to "user".
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2686
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2687 Optinonal arguments:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2688 stretch_wid : positive number
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2689 stretch_len : positive number
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2690 outline : positive number
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2691
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2692 Return or set the pen's attributes x/y-stretchfactors and/or outline.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2693 Set resizemode to "user".
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2694 If and only if resizemode is set to "user", the turtle will be displayed
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2695 stretched according to its stretchfactors:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2696 stretch_wid is stretchfactor perpendicular to orientation
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2697 stretch_len is stretchfactor in direction of turtles orientation.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2698 outline determines the width of the shapes's outline.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2699
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2700 Examples (for a Turtle instance named turtle):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2701 >>> turtle.resizemode("user")
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2702 >>> turtle.shapesize(5, 5, 12)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2703 >>> turtle.shapesize(outline=8)
38204
fa3c4ea99d95 Patch #1486962: Several bugs in the turtle Tk demo module were fixed
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
2704 """
60060
0bc04525af63 Merged revisions 79195,79425,79427,79450 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59834
diff changeset
2705 if stretch_wid is stretch_len is outline is None:
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2706 stretch_wid, stretch_len = self._stretchfactor
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2707 return stretch_wid, stretch_len, self._outlinewidth
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2708 if stretch_wid is not None:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2709 if stretch_len is None:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2710 stretchfactor = stretch_wid, stretch_wid
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2711 else:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2712 stretchfactor = stretch_wid, stretch_len
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2713 elif stretch_len is not None:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2714 stretchfactor = self._stretchfactor[0], stretch_len
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2715 else:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2716 stretchfactor = self._stretchfactor
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2717 if outline is None:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2718 outline = self._outlinewidth
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2719 self.pen(resizemode="user",
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2720 stretchfactor=stretchfactor, outline=outline)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2721
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2722 def settiltangle(self, angle):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2723 """Rotate the turtleshape to point in the specified direction
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2724
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2725 Optional argument:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2726 angle -- number
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2727
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2728 Rotate the turtleshape to point in the direction specified by angle,
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2729 regardless of its current tilt-angle. DO NOT change the turtle's
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2730 heading (direction of movement).
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2731
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2732
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2733 Examples (for a Turtle instance named turtle):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2734 >>> turtle.shape("circle")
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2735 >>> turtle.shapesize(5,2)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2736 >>> turtle.settiltangle(45)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2737 >>> stamp()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2738 >>> turtle.fd(50)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2739 >>> turtle.settiltangle(-45)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2740 >>> stamp()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2741 >>> turtle.fd(50)
38204
fa3c4ea99d95 Patch #1486962: Several bugs in the turtle Tk demo module were fixed
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
2742 """
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2743 tilt = -angle * self._degreesPerAU * self._angleOrient
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2744 tilt = (tilt * math.pi / 180.0) % (2*math.pi)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2745 self.pen(resizemode="user", tilt=tilt)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2746
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2747 def tiltangle(self):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2748 """Return the current tilt-angle.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2749
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2750 No argument.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2751
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2752 Return the current tilt-angle, i. e. the angle between the
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2753 orientation of the turtleshape and the heading of the turtle
51761
6ce96ce4d9af Merged revisions 69816 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 49758
diff changeset
2754 (its direction of movement).
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2755
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2756 Examples (for a Turtle instance named turtle):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2757 >>> turtle.shape("circle")
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2758 >>> turtle.shapesize(5,2)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2759 >>> turtle.tilt(45)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2760 >>> turtle.tiltangle()
38204
fa3c4ea99d95 Patch #1486962: Several bugs in the turtle Tk demo module were fixed
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
2761 """
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2762 tilt = -self._tilt * (180.0/math.pi) * self._angleOrient
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2763 return (tilt / self._degreesPerAU) % self._fullcircle
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2764
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2765 def tilt(self, angle):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2766 """Rotate the turtleshape by angle.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2767
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2768 Argument:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2769 angle - a number
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2770
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2771 Rotate the turtleshape by angle from its current tilt-angle,
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2772 but do NOT change the turtle's heading (direction of movement).
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2773
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2774 Examples (for a Turtle instance named turtle):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2775 >>> turtle.shape("circle")
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2776 >>> turtle.shapesize(5,2)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2777 >>> turtle.tilt(30)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2778 >>> turtle.fd(50)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2779 >>> turtle.tilt(30)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2780 >>> turtle.fd(50)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2781 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2782 self.settiltangle(angle + self.tiltangle())
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2783
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2784 def _polytrafo(self, poly):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2785 """Computes transformed polygon shapes from a shape
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2786 according to current position and heading.
38204
fa3c4ea99d95 Patch #1486962: Several bugs in the turtle Tk demo module were fixed
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
2787 """
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2788 screen = self.screen
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2789 p0, p1 = self._position
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2790 e0, e1 = self._orient
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2791 e = Vec2D(e0, e1 * screen.yscale / screen.xscale)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2792 e0, e1 = (1.0 / abs(e)) * e
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2793 return [(p0+(e1*x+e0*y)/screen.xscale, p1+(-e0*x+e1*y)/screen.yscale)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2794 for (x, y) in poly]
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2795
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2796 def _drawturtle(self):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2797 """Manages the correct rendering of the turtle with respect to
55049
74c0a78114ae Merged revisions 69846 via svnmerge from
Jesus Cea <jcea@jcea.es>
parents: 51761
diff changeset
2798 its shape, resizemode, stretch and tilt etc."""
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2799 screen = self.screen
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2800 shape = screen._shapes[self.turtle.shapeIndex]
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2801 ttype = shape._type
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2802 titem = self.turtle._item
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2803 if self._shown and screen._updatecounter == 0 and screen._tracing > 0:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2804 self._hidden_from_screen = False
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2805 tshape = shape._data
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2806 if ttype == "polygon":
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2807 if self._resizemode == "noresize":
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2808 w = 1
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2809 shape = tshape
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2810 else:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2811 if self._resizemode == "auto":
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2812 lx = ly = max(1, self._pensize/5.0)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2813 w = self._pensize
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2814 tiltangle = 0
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2815 elif self._resizemode == "user":
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2816 lx, ly = self._stretchfactor
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2817 w = self._outlinewidth
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2818 tiltangle = self._tilt
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2819 shape = [(lx*x, ly*y) for (x, y) in tshape]
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2820 t0, t1 = math.sin(tiltangle), math.cos(tiltangle)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2821 shape = [(t1*x+t0*y, -t0*x+t1*y) for (x, y) in shape]
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2822 shape = self._polytrafo(shape)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2823 fc, oc = self._fillcolor, self._pencolor
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2824 screen._drawpoly(titem, shape, fill=fc, outline=oc,
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2825 width=w, top=True)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2826 elif ttype == "image":
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2827 screen._drawimage(titem, self._position, tshape)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2828 elif ttype == "compound":
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2829 lx, ly = self._stretchfactor
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2830 w = self._outlinewidth
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2831 for item, (poly, fc, oc) in zip(titem, tshape):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2832 poly = [(lx*x, ly*y) for (x, y) in poly]
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2833 poly = self._polytrafo(poly)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2834 screen._drawpoly(item, poly, fill=self._cc(fc),
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2835 outline=self._cc(oc), width=w, top=True)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2836 else:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2837 if self._hidden_from_screen:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2838 return
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2839 if ttype == "polygon":
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2840 screen._drawpoly(titem, ((0, 0), (0, 0), (0, 0)), "", "")
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2841 elif ttype == "image":
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2842 screen._drawimage(titem, self._position,
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2843 screen._shapes["blank"]._data)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2844 elif ttype == "compound":
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2845 for item in titem:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2846 screen._drawpoly(item, ((0, 0), (0, 0), (0, 0)), "", "")
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2847 self._hidden_from_screen = True
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2848
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2849 ############################## stamp stuff ###############################
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2850
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2851 def stamp(self):
51761
6ce96ce4d9af Merged revisions 69816 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 49758
diff changeset
2852 """Stamp a copy of the turtleshape onto the canvas and return its id.
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2853
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2854 No argument.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2855
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2856 Stamp a copy of the turtle shape onto the canvas at the current
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2857 turtle position. Return a stamp_id for that stamp, which can be
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2858 used to delete it by calling clearstamp(stamp_id).
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2859
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2860 Example (for a Turtle instance named turtle):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2861 >>> turtle.color("blue")
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2862 >>> turtle.stamp()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2863 13
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2864 >>> turtle.fd(50)
38204
fa3c4ea99d95 Patch #1486962: Several bugs in the turtle Tk demo module were fixed
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
2865 """
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2866 screen = self.screen
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2867 shape = screen._shapes[self.turtle.shapeIndex]
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2868 ttype = shape._type
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2869 tshape = shape._data
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2870 if ttype == "polygon":
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2871 stitem = screen._createpoly()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2872 if self._resizemode == "noresize":
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2873 w = 1
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2874 shape = tshape
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2875 else:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2876 if self._resizemode == "auto":
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2877 lx = ly = max(1, self._pensize/5.0)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2878 w = self._pensize
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2879 tiltangle = 0
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2880 elif self._resizemode == "user":
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2881 lx, ly = self._stretchfactor
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2882 w = self._outlinewidth
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2883 tiltangle = self._tilt
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2884 shape = [(lx*x, ly*y) for (x, y) in tshape]
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2885 t0, t1 = math.sin(tiltangle), math.cos(tiltangle)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2886 shape = [(t1*x+t0*y, -t0*x+t1*y) for (x, y) in shape]
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2887 shape = self._polytrafo(shape)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2888 fc, oc = self._fillcolor, self._pencolor
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2889 screen._drawpoly(stitem, shape, fill=fc, outline=oc,
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2890 width=w, top=True)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2891 elif ttype == "image":
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2892 stitem = screen._createimage("")
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2893 screen._drawimage(stitem, self._position, tshape)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2894 elif ttype == "compound":
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2895 stitem = []
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2896 for element in tshape:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2897 item = screen._createpoly()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2898 stitem.append(item)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2899 stitem = tuple(stitem)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2900 lx, ly = self._stretchfactor
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2901 w = self._outlinewidth
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2902 for item, (poly, fc, oc) in zip(stitem, tshape):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2903 poly = [(lx*x, ly*y) for (x, y) in poly]
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2904 poly = self._polytrafo(poly)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2905 screen._drawpoly(item, poly, fill=self._cc(fc),
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2906 outline=self._cc(oc), width=w, top=True)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2907 self.stampItems.append(stitem)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2908 self.undobuffer.push(("stamp", stitem))
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2909 return stitem
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2910
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2911 def _clearstamp(self, stampid):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2912 """does the work for clearstamp() and clearstamps()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2913 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2914 if stampid in self.stampItems:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2915 if isinstance(stampid, tuple):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2916 for subitem in stampid:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2917 self.screen._delete(subitem)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2918 else:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2919 self.screen._delete(stampid)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2920 self.stampItems.remove(stampid)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2921 # Delete stampitem from undobuffer if necessary
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2922 # if clearstamp is called directly.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2923 item = ("stamp", stampid)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2924 buf = self.undobuffer
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2925 if item not in buf.buffer:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2926 return
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2927 index = buf.buffer.index(item)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2928 buf.buffer.remove(item)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2929 if index <= buf.ptr:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2930 buf.ptr = (buf.ptr - 1) % buf.bufsize
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2931 buf.buffer.insert((buf.ptr+1)%buf.bufsize, [None])
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2932
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2933 def clearstamp(self, stampid):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2934 """Delete stamp with given stampid
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2935
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2936 Argument:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2937 stampid - an integer, must be return value of previous stamp() call.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2938
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2939 Example (for a Turtle instance named turtle):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2940 >>> turtle.color("blue")
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2941 >>> astamp = turtle.stamp()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2942 >>> turtle.fd(50)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2943 >>> turtle.clearstamp(astamp)
38204
fa3c4ea99d95 Patch #1486962: Several bugs in the turtle Tk demo module were fixed
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
2944 """
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2945 self._clearstamp(stampid)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2946 self._update()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2947
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2948 def clearstamps(self, n=None):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2949 """Delete all or first/last n of turtle's stamps.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2950
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2951 Optional argument:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2952 n -- an integer
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2953
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2954 If n is None, delete all of pen's stamps,
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2955 else if n > 0 delete first n stamps
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2956 else if n < 0 delete last n stamps.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2957
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2958 Example (for a Turtle instance named turtle):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2959 >>> for i in range(8):
73815
6e03ab9950f6 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72984
diff changeset
2960 ... turtle.stamp(); turtle.fd(30)
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2961 ...
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2962 >>> turtle.clearstamps(2)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2963 >>> turtle.clearstamps(-2)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2964 >>> turtle.clearstamps()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2965 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2966 if n is None:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2967 toDelete = self.stampItems[:]
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2968 elif n >= 0:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2969 toDelete = self.stampItems[:n]
9585
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
2970 else:
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2971 toDelete = self.stampItems[n:]
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2972 for item in toDelete:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2973 self._clearstamp(item)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2974 self._update()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2975
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2976 def _goto(self, end):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2977 """Move the pen to the point end, thereby drawing a line
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2978 if pen is down. All other methodes for turtle movement depend
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2979 on this one.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2980 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2981 ## Version mit undo-stuff
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2982 go_modes = ( self._drawing,
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2983 self._pencolor,
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2984 self._pensize,
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2985 isinstance(self._fillpath, list))
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2986 screen = self.screen
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2987 undo_entry = ("go", self._position, end, go_modes,
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2988 (self.currentLineItem,
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2989 self.currentLine[:],
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2990 screen._pointlist(self.currentLineItem),
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2991 self.items[:])
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2992 )
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2993 if self.undobuffer:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2994 self.undobuffer.push(undo_entry)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2995 start = self._position
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2996 if self._speed and screen._tracing == 1:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2997 diff = (end-start)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2998 diffsq = (diff[0]*screen.xscale)**2 + (diff[1]*screen.yscale)**2
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2999 nhops = 1+int((diffsq**0.5)/(3*(1.1**self._speed)*self._speed))
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3000 delta = diff * (1.0/nhops)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3001 for n in range(1, nhops):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3002 if n == 1:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3003 top = True
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3004 else:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3005 top = False
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3006 self._position = start + delta * n
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3007 if self._drawing:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3008 screen._drawline(self.drawingLineItem,
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3009 (start, self._position),
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3010 self._pencolor, self._pensize, top)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3011 self._update()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3012 if self._drawing:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3013 screen._drawline(self.drawingLineItem, ((0, 0), (0, 0)),
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3014 fill="", width=self._pensize)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3015 # Turtle now at end,
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3016 if self._drawing: # now update currentLine
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3017 self.currentLine.append(end)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3018 if isinstance(self._fillpath, list):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3019 self._fillpath.append(end)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3020 ###### vererbung!!!!!!!!!!!!!!!!!!!!!!
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3021 self._position = end
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3022 if self._creatingPoly:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3023 self._poly.append(end)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3024 if len(self.currentLine) > 42: # 42! answer to the ultimate question
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3025 # of life, the universe and everything
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3026 self._newLine()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3027 self._update() #count=True)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3028
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3029 def _undogoto(self, entry):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3030 """Reverse a _goto. Used for undo()
38204
fa3c4ea99d95 Patch #1486962: Several bugs in the turtle Tk demo module were fixed
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
3031 """
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3032 old, new, go_modes, coodata = entry
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3033 drawing, pc, ps, filling = go_modes
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3034 cLI, cL, pl, items = coodata
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3035 screen = self.screen
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3036 if abs(self._position - new) > 0.5:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3037 print "undogoto: HALLO-DA-STIMMT-WAS-NICHT!"
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3038 # restore former situation
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3039 self.currentLineItem = cLI
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3040 self.currentLine = cL
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3041
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3042 if pl == [(0, 0), (0, 0)]:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3043 usepc = ""
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3044 else:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3045 usepc = pc
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3046 screen._drawline(cLI, pl, fill=usepc, width=ps)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3047
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3048 todelete = [i for i in self.items if (i not in items) and
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3049 (screen._type(i) == "line")]
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3050 for i in todelete:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3051 screen._delete(i)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3052 self.items.remove(i)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3053
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3054 start = old
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3055 if self._speed and screen._tracing == 1:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3056 diff = old - new
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3057 diffsq = (diff[0]*screen.xscale)**2 + (diff[1]*screen.yscale)**2
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3058 nhops = 1+int((diffsq**0.5)/(3*(1.1**self._speed)*self._speed))
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3059 delta = diff * (1.0/nhops)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3060 for n in range(1, nhops):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3061 if n == 1:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3062 top = True
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3063 else:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3064 top = False
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3065 self._position = new + delta * n
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3066 if drawing:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3067 screen._drawline(self.drawingLineItem,
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3068 (start, self._position),
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3069 pc, ps, top)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3070 self._update()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3071 if drawing:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3072 screen._drawline(self.drawingLineItem, ((0, 0), (0, 0)),
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3073 fill="", width=ps)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3074 # Turtle now at position old,
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3075 self._position = old
68588
cd8d3c2d3bce #11565: Fix several typos. Patch by Piotr Kasprzyk.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 68503
diff changeset
3076 ## if undo is done during creating a polygon, the last vertex
cd8d3c2d3bce #11565: Fix several typos. Patch by Piotr Kasprzyk.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 68503
diff changeset
3077 ## will be deleted. if the polygon is entirely deleted,
cd8d3c2d3bce #11565: Fix several typos. Patch by Piotr Kasprzyk.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 68503
diff changeset
3078 ## creatingPoly will be set to False.
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3079 ## Polygons created before the last one will not be affected by undo()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3080 if self._creatingPoly:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3081 if len(self._poly) > 0:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3082 self._poly.pop()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3083 if self._poly == []:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3084 self._creatingPoly = False
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3085 self._poly = None
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3086 if filling:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3087 if self._fillpath == []:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3088 self._fillpath = None
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3089 print "Unwahrscheinlich in _undogoto!"
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3090 elif self._fillpath is not None:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3091 self._fillpath.pop()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3092 self._update() #count=True)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3093
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3094 def _rotate(self, angle):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3095 """Turns pen clockwise by angle.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3096 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3097 if self.undobuffer:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3098 self.undobuffer.push(("rot", angle, self._degreesPerAU))
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3099 angle *= self._degreesPerAU
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3100 neworient = self._orient.rotate(angle)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3101 tracing = self.screen._tracing
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3102 if tracing == 1 and self._speed > 0:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3103 anglevel = 3.0 * self._speed
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3104 steps = 1 + int(abs(angle)/anglevel)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3105 delta = 1.0*angle/steps
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3106 for _ in range(steps):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3107 self._orient = self._orient.rotate(delta)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3108 self._update()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3109 self._orient = neworient
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3110 self._update()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3111
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3112 def _newLine(self, usePos=True):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3113 """Closes current line item and starts a new one.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3114 Remark: if current line became too long, animation
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3115 performance (via _drawline) slowed down considerably.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3116 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3117 if len(self.currentLine) > 1:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3118 self.screen._drawline(self.currentLineItem, self.currentLine,
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3119 self._pencolor, self._pensize)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3120 self.currentLineItem = self.screen._createline()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3121 self.items.append(self.currentLineItem)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3122 else:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3123 self.screen._drawline(self.currentLineItem, top=True)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3124 self.currentLine = []
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3125 if usePos:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3126 self.currentLine = [self._position]
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3127
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3128 def fill(self, flag=None):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3129 """Call fill(True) before drawing a shape to fill, fill(False) when done.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3130
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3131 Optional argument:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3132 flag -- True/False (or 1/0 respectively)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3133
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3134 Call fill(True) before drawing the shape you want to fill,
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3135 and fill(False) when done.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3136 When used without argument: return fillstate (True if filling,
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3137 False else)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3138
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3139 Example (for a Turtle instance named turtle):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3140 >>> turtle.fill(True)
38204
fa3c4ea99d95 Patch #1486962: Several bugs in the turtle Tk demo module were fixed
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
3141 >>> turtle.forward(100)
fa3c4ea99d95 Patch #1486962: Several bugs in the turtle Tk demo module were fixed
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
3142 >>> turtle.left(90)
fa3c4ea99d95 Patch #1486962: Several bugs in the turtle Tk demo module were fixed
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
3143 >>> turtle.forward(100)
fa3c4ea99d95 Patch #1486962: Several bugs in the turtle Tk demo module were fixed
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
3144 >>> turtle.left(90)
fa3c4ea99d95 Patch #1486962: Several bugs in the turtle Tk demo module were fixed
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
3145 >>> turtle.forward(100)
fa3c4ea99d95 Patch #1486962: Several bugs in the turtle Tk demo module were fixed
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
3146 >>> turtle.left(90)
fa3c4ea99d95 Patch #1486962: Several bugs in the turtle Tk demo module were fixed
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
3147 >>> turtle.forward(100)
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3148 >>> turtle.fill(False)
38204
fa3c4ea99d95 Patch #1486962: Several bugs in the turtle Tk demo module were fixed
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
3149 """
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3150 filling = isinstance(self._fillpath, list)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3151 if flag is None:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3152 return filling
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3153 screen = self.screen
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3154 entry1 = entry2 = ()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3155 if filling:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3156 if len(self._fillpath) > 2:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3157 self.screen._drawpoly(self._fillitem, self._fillpath,
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3158 fill=self._fillcolor)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3159 entry1 = ("dofill", self._fillitem)
9585
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
3160 if flag:
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3161 self._fillitem = self.screen._createpoly()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3162 self.items.append(self._fillitem)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3163 self._fillpath = [self._position]
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3164 entry2 = ("beginfill", self._fillitem) # , self._fillpath)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3165 self._newLine()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3166 else:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3167 self._fillitem = self._fillpath = None
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3168 if self.undobuffer:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3169 if entry1 == ():
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3170 if entry2 != ():
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3171 self.undobuffer.push(entry2)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3172 else:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3173 if entry2 == ():
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3174 self.undobuffer.push(entry1)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3175 else:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3176 self.undobuffer.push(["seq", entry1, entry2])
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3177 self._update()
9585
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
3178
38204
fa3c4ea99d95 Patch #1486962: Several bugs in the turtle Tk demo module were fixed
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
3179 def begin_fill(self):
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3180 """Called just before drawing a shape to be filled.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3181
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3182 No argument.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3183
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3184 Example (for a Turtle instance named turtle):
38204
fa3c4ea99d95 Patch #1486962: Several bugs in the turtle Tk demo module were fixed
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
3185 >>> turtle.begin_fill()
fa3c4ea99d95 Patch #1486962: Several bugs in the turtle Tk demo module were fixed
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
3186 >>> turtle.forward(100)
fa3c4ea99d95 Patch #1486962: Several bugs in the turtle Tk demo module were fixed
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
3187 >>> turtle.left(90)
fa3c4ea99d95 Patch #1486962: Several bugs in the turtle Tk demo module were fixed
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
3188 >>> turtle.forward(100)
fa3c4ea99d95 Patch #1486962: Several bugs in the turtle Tk demo module were fixed
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
3189 >>> turtle.left(90)
fa3c4ea99d95 Patch #1486962: Several bugs in the turtle Tk demo module were fixed
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
3190 >>> turtle.forward(100)
fa3c4ea99d95 Patch #1486962: Several bugs in the turtle Tk demo module were fixed
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
3191 >>> turtle.left(90)
fa3c4ea99d95 Patch #1486962: Several bugs in the turtle Tk demo module were fixed
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
3192 >>> turtle.forward(100)
fa3c4ea99d95 Patch #1486962: Several bugs in the turtle Tk demo module were fixed
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
3193 >>> turtle.end_fill()
fa3c4ea99d95 Patch #1486962: Several bugs in the turtle Tk demo module were fixed
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
3194 """
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3195 self.fill(True)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3196
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3197 def end_fill(self):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3198 """Fill the shape drawn after the call begin_fill().
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3199
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3200 No argument.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3201
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3202 Example (for a Turtle instance named turtle):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3203 >>> turtle.begin_fill()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3204 >>> turtle.forward(100)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3205 >>> turtle.left(90)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3206 >>> turtle.forward(100)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3207 >>> turtle.left(90)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3208 >>> turtle.forward(100)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3209 >>> turtle.left(90)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3210 >>> turtle.forward(100)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3211 >>> turtle.end_fill()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3212 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3213 self.fill(False)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3214
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3215 def dot(self, size=None, *color):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3216 """Draw a dot with diameter size, using color.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3217
68503
ee5350f1d0c2 #11515: fix several typos. Patch by Piotr Kasprzyk.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 68256
diff changeset
3218 Optional arguments:
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3219 size -- an integer >= 1 (if given)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3220 color -- a colorstring or a numeric color tuple
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3221
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3222 Draw a circular dot with diameter size, using color.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3223 If size is not given, the maximum of pensize+4 and 2*pensize is used.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3224
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3225 Example (for a Turtle instance named turtle):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3226 >>> turtle.dot()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3227 >>> turtle.fd(50); turtle.dot(20, "blue"); turtle.fd(50)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3228 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3229 #print "dot-1:", size, color
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3230 if not color:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3231 if isinstance(size, (str, tuple)):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3232 color = self._colorstr(size)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3233 size = self._pensize + max(self._pensize, 4)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3234 else:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3235 color = self._pencolor
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3236 if not size:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3237 size = self._pensize + max(self._pensize, 4)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3238 else:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3239 if size is None:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3240 size = self._pensize + max(self._pensize, 4)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3241 color = self._colorstr(color)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3242 #print "dot-2:", size, color
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3243 if hasattr(self.screen, "_dot"):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3244 item = self.screen._dot(self._position, size, color)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3245 #print "dot:", size, color, "item:", item
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3246 self.items.append(item)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3247 if self.undobuffer:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3248 self.undobuffer.push(("dot", item))
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3249 else:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3250 pen = self.pen()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3251 if self.undobuffer:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3252 self.undobuffer.push(["seq"])
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3253 self.undobuffer.cumulate = True
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3254 try:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3255 if self.resizemode() == 'auto':
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3256 self.ht()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3257 self.pendown()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3258 self.pensize(size)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3259 self.pencolor(color)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3260 self.forward(0)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3261 finally:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3262 self.pen(pen)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3263 if self.undobuffer:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3264 self.undobuffer.cumulate = False
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3265
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3266 def _write(self, txt, align, font):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3267 """Performs the writing for write()
38204
fa3c4ea99d95 Patch #1486962: Several bugs in the turtle Tk demo module were fixed
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
3268 """
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3269 item, end = self.screen._write(self._position, txt, align, font,
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3270 self._pencolor)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3271 self.items.append(item)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3272 if self.undobuffer:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3273 self.undobuffer.push(("wri", item))
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3274 return end
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3275
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3276 def write(self, arg, move=False, align="left", font=("Arial", 8, "normal")):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3277 """Write text at the current turtle position.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3278
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3279 Arguments:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3280 arg -- info, which is to be written to the TurtleScreen
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3281 move (optional) -- True/False
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3282 align (optional) -- one of the strings "left", "center" or right"
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3283 font (optional) -- a triple (fontname, fontsize, fonttype)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3284
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3285 Write text - the string representation of arg - at the current
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3286 turtle position according to align ("left", "center" or right")
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3287 and with the given font.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3288 If move is True, the pen is moved to the bottom-right corner
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3289 of the text. By default, move is False.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3290
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3291 Example (for a Turtle instance named turtle):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3292 >>> turtle.write('Home = ', True, align="center")
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3293 >>> turtle.write((0,0), True)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3294 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3295 if self.undobuffer:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3296 self.undobuffer.push(["seq"])
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3297 self.undobuffer.cumulate = True
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3298 end = self._write(str(arg), align.lower(), font)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3299 if move:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3300 x, y = self.pos()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3301 self.setpos(end, y)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3302 if self.undobuffer:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3303 self.undobuffer.cumulate = False
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3304
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3305 def begin_poly(self):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3306 """Start recording the vertices of a polygon.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3307
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3308 No argument.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3309
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3310 Start recording the vertices of a polygon. Current turtle position
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3311 is first point of polygon.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3312
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3313 Example (for a Turtle instance named turtle):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3314 >>> turtle.begin_poly()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3315 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3316 self._poly = [self._position]
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3317 self._creatingPoly = True
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3318
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3319 def end_poly(self):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3320 """Stop recording the vertices of a polygon.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3321
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3322 No argument.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3323
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3324 Stop recording the vertices of a polygon. Current turtle position is
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3325 last point of polygon. This will be connected with the first point.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3326
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3327 Example (for a Turtle instance named turtle):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3328 >>> turtle.end_poly()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3329 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3330 self._creatingPoly = False
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3331
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3332 def get_poly(self):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3333 """Return the lastly recorded polygon.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3334
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3335 No argument.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3336
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3337 Example (for a Turtle instance named turtle):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3338 >>> p = turtle.get_poly()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3339 >>> turtle.register_shape("myFavouriteShape", p)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3340 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3341 ## check if there is any poly? -- 1st solution:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3342 if self._poly is not None:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3343 return tuple(self._poly)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3344
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3345 def getscreen(self):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3346 """Return the TurtleScreen object, the turtle is drawing on.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3347
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3348 No argument.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3349
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3350 Return the TurtleScreen object, the turtle is drawing on.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3351 So TurtleScreen-methods can be called for that object.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3352
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3353 Example (for a Turtle instance named turtle):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3354 >>> ts = turtle.getscreen()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3355 >>> ts
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3356 <turtle.TurtleScreen object at 0x0106B770>
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3357 >>> ts.bgcolor("pink")
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3358 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3359 return self.screen
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3360
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3361 def getturtle(self):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3362 """Return the Turtleobject itself.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3363
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3364 No argument.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3365
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3366 Only reasonable use: as a function to return the 'anonymous turtle':
38204
fa3c4ea99d95 Patch #1486962: Several bugs in the turtle Tk demo module were fixed
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
3367
fa3c4ea99d95 Patch #1486962: Several bugs in the turtle Tk demo module were fixed
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
3368 Example:
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3369 >>> pet = getturtle()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3370 >>> pet.fd(50)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3371 >>> pet
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3372 <turtle.Turtle object at 0x0187D810>
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3373 >>> turtles()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3374 [<turtle.Turtle object at 0x0187D810>]
38204
fa3c4ea99d95 Patch #1486962: Several bugs in the turtle Tk demo module were fixed
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
3375 """
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3376 return self
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3377
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3378 getpen = getturtle
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3379
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3380
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3381 ################################################################
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3382 ### screen oriented methods recurring to methods of TurtleScreen
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3383 ################################################################
25360
5255b45fddc2 Add the bulk of SF patch 595111 by Attila Babo.
Guido van Rossum <guido@python.org>
parents: 25340
diff changeset
3384
5255b45fddc2 Add the bulk of SF patch 595111 by Attila Babo.
Guido van Rossum <guido@python.org>
parents: 25340
diff changeset
3385 def window_width(self):
38204
fa3c4ea99d95 Patch #1486962: Several bugs in the turtle Tk demo module were fixed
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
3386 """ Returns the width of the turtle window.
fa3c4ea99d95 Patch #1486962: Several bugs in the turtle Tk demo module were fixed
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
3387
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3388 No argument.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3389
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3390 Example (for a TurtleScreen instance named screen):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3391 >>> screen.window_width()
38204
fa3c4ea99d95 Patch #1486962: Several bugs in the turtle Tk demo module were fixed
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
3392 640
fa3c4ea99d95 Patch #1486962: Several bugs in the turtle Tk demo module were fixed
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
3393 """
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3394 return self.screen._window_size()[0]
25360
5255b45fddc2 Add the bulk of SF patch 595111 by Attila Babo.
Guido van Rossum <guido@python.org>
parents: 25340
diff changeset
3395
5255b45fddc2 Add the bulk of SF patch 595111 by Attila Babo.
Guido van Rossum <guido@python.org>
parents: 25340
diff changeset
3396 def window_height(self):
38204
fa3c4ea99d95 Patch #1486962: Several bugs in the turtle Tk demo module were fixed
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
3397 """ Return the height of the turtle window.
fa3c4ea99d95 Patch #1486962: Several bugs in the turtle Tk demo module were fixed
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
3398
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3399 No argument.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3400
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3401 Example (for a TurtleScreen instance named screen):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3402 >>> screen.window_height()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3403 480
38204
fa3c4ea99d95 Patch #1486962: Several bugs in the turtle Tk demo module were fixed
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
3404 """
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3405 return self.screen._window_size()[1]
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3406
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3407 def _delay(self, delay=None):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3408 """Set delay value which determines speed of turtle animation.
38204
fa3c4ea99d95 Patch #1486962: Several bugs in the turtle Tk demo module were fixed
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
3409 """
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3410 return self.screen.delay(delay)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3411
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3412 ##### event binding methods #####
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3413
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3414 def onclick(self, fun, btn=1, add=None):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3415 """Bind fun to mouse-click event on this turtle on canvas.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3416
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3417 Arguments:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3418 fun -- a function with two arguments, to which will be assigned
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3419 the coordinates of the clicked point on the canvas.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3420 num -- number of the mouse-button defaults to 1 (left mouse button).
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3421 add -- True or False. If True, new binding will be added, otherwise
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3422 it will replace a former binding.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3423
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3424 Example for the anonymous turtle, i. e. the procedural way:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3425
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3426 >>> def turn(x, y):
73815
6e03ab9950f6 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72984
diff changeset
3427 ... left(360)
6e03ab9950f6 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72984
diff changeset
3428 ...
6e03ab9950f6 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72984
diff changeset
3429 >>> onclick(turn) # Now clicking into the turtle will turn it.
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3430 >>> onclick(None) # event-binding will be removed
38204
fa3c4ea99d95 Patch #1486962: Several bugs in the turtle Tk demo module were fixed
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
3431 """
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3432 self.screen._onclick(self.turtle._item, fun, btn, add)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3433 self._update()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3434
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3435 def onrelease(self, fun, btn=1, add=None):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3436 """Bind fun to mouse-button-release event on this turtle on canvas.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3437
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3438 Arguments:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3439 fun -- a function with two arguments, to which will be assigned
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3440 the coordinates of the clicked point on the canvas.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3441 num -- number of the mouse-button defaults to 1 (left mouse button).
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3442
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3443 Example (for a MyTurtle instance named joe):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3444 >>> class MyTurtle(Turtle):
73815
6e03ab9950f6 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72984
diff changeset
3445 ... def glow(self,x,y):
6e03ab9950f6 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72984
diff changeset
3446 ... self.fillcolor("red")
6e03ab9950f6 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72984
diff changeset
3447 ... def unglow(self,x,y):
6e03ab9950f6 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72984
diff changeset
3448 ... self.fillcolor("")
6e03ab9950f6 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72984
diff changeset
3449 ...
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3450 >>> joe = MyTurtle()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3451 >>> joe.onclick(joe.glow)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3452 >>> joe.onrelease(joe.unglow)
73815
6e03ab9950f6 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72984
diff changeset
3453
6e03ab9950f6 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72984
diff changeset
3454 Clicking on joe turns fillcolor red, unclicking turns it to
6e03ab9950f6 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72984
diff changeset
3455 transparent.
38204
fa3c4ea99d95 Patch #1486962: Several bugs in the turtle Tk demo module were fixed
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
3456 """
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3457 self.screen._onrelease(self.turtle._item, fun, btn, add)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3458 self._update()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3460 def ondrag(self, fun, btn=1, add=None):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3461 """Bind fun to mouse-move event on this turtle on canvas.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3462
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3463 Arguments:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3464 fun -- a function with two arguments, to which will be assigned
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3465 the coordinates of the clicked point on the canvas.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3466 num -- number of the mouse-button defaults to 1 (left mouse button).
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3467
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3468 Every sequence of mouse-move-events on a turtle is preceded by a
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3469 mouse-click event on that turtle.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3470
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3471 Example (for a Turtle instance named turtle):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3472 >>> turtle.ondrag(turtle.goto)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3473
73815
6e03ab9950f6 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72984
diff changeset
3474 Subsequently clicking and dragging a Turtle will move it
6e03ab9950f6 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72984
diff changeset
3475 across the screen thereby producing handdrawings (if pen is
6e03ab9950f6 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72984
diff changeset
3476 down).
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3477 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3478 self.screen._ondrag(self.turtle._item, fun, btn, add)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3479
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3480
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3481 def _undo(self, action, data):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3482 """Does the main part of the work for undo()
38204
fa3c4ea99d95 Patch #1486962: Several bugs in the turtle Tk demo module were fixed
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
3483 """
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3484 if self.undobuffer is None:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3485 return
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3486 if action == "rot":
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3487 angle, degPAU = data
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3488 self._rotate(-angle*degPAU/self._degreesPerAU)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3489 dummy = self.undobuffer.pop()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3490 elif action == "stamp":
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3491 stitem = data[0]
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3492 self.clearstamp(stitem)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3493 elif action == "go":
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3494 self._undogoto(data)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3495 elif action in ["wri", "dot"]:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3496 item = data[0]
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3497 self.screen._delete(item)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3498 self.items.remove(item)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3499 elif action == "dofill":
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3500 item = data[0]
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3501 self.screen._drawpoly(item, ((0, 0),(0, 0),(0, 0)),
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3502 fill="", outline="")
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3503 elif action == "beginfill":
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3504 item = data[0]
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3505 self._fillitem = self._fillpath = None
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3506 self.screen._delete(item)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3507 self.items.remove(item)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3508 elif action == "pen":
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3509 TPen.pen(self, data[0])
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3510 self.undobuffer.pop()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3511
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3512 def undo(self):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3513 """undo (repeatedly) the last turtle action.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3514
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3515 No argument.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3516
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3517 undo (repeatedly) the last turtle action.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3518 Number of available undo actions is determined by the size of
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3519 the undobuffer.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3520
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3521 Example (for a Turtle instance named turtle):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3522 >>> for i in range(4):
73815
6e03ab9950f6 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72984
diff changeset
3523 ... turtle.fd(50); turtle.lt(80)
6e03ab9950f6 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72984
diff changeset
3524 ...
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3525 >>> for i in range(8):
73815
6e03ab9950f6 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72984
diff changeset
3526 ... turtle.undo()
6e03ab9950f6 Issue #13439: Fix many errors in turtle docstrings.
Petri Lehtinen <petri@digip.org>
parents: 72984
diff changeset
3527 ...
38204
fa3c4ea99d95 Patch #1486962: Several bugs in the turtle Tk demo module were fixed
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
3528 """
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3529 if self.undobuffer is None:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3530 return
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3531 item = self.undobuffer.pop()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3532 action = item[0]
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3533 data = item[1:]
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3534 if action == "seq":
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3535 while data:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3536 item = data.pop()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3537 self._undo(item[0], item[1:])
9585
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
3538 else:
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3539 self._undo(action, data)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3540
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3541 turtlesize = shapesize
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3542
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3543 RawPen = RawTurtle
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3544
49344
d88f1f2f0213 Issue #3965: Allow repeated calls to turtle.Screen, by making it a
Martin v. Löwis <martin@v.loewis.de>
parents: 49291
diff changeset
3545 ### Screen - Singleton ########################
d88f1f2f0213 Issue #3965: Allow repeated calls to turtle.Screen, by making it a
Martin v. Löwis <martin@v.loewis.de>
parents: 49291
diff changeset
3546
d88f1f2f0213 Issue #3965: Allow repeated calls to turtle.Screen, by making it a
Martin v. Löwis <martin@v.loewis.de>
parents: 49291
diff changeset
3547 def Screen():
d88f1f2f0213 Issue #3965: Allow repeated calls to turtle.Screen, by making it a
Martin v. Löwis <martin@v.loewis.de>
parents: 49291
diff changeset
3548 """Return the singleton screen object.
d88f1f2f0213 Issue #3965: Allow repeated calls to turtle.Screen, by making it a
Martin v. Löwis <martin@v.loewis.de>
parents: 49291
diff changeset
3549 If none exists at the moment, create a new one and return it,
d88f1f2f0213 Issue #3965: Allow repeated calls to turtle.Screen, by making it a
Martin v. Löwis <martin@v.loewis.de>
parents: 49291
diff changeset
3550 else return the existing one."""
d88f1f2f0213 Issue #3965: Allow repeated calls to turtle.Screen, by making it a
Martin v. Löwis <martin@v.loewis.de>
parents: 49291
diff changeset
3551 if Turtle._screen is None:
d88f1f2f0213 Issue #3965: Allow repeated calls to turtle.Screen, by making it a
Martin v. Löwis <martin@v.loewis.de>
parents: 49291
diff changeset
3552 Turtle._screen = _Screen()
d88f1f2f0213 Issue #3965: Allow repeated calls to turtle.Screen, by making it a
Martin v. Löwis <martin@v.loewis.de>
parents: 49291
diff changeset
3553 return Turtle._screen
d88f1f2f0213 Issue #3965: Allow repeated calls to turtle.Screen, by making it a
Martin v. Löwis <martin@v.loewis.de>
parents: 49291
diff changeset
3554
d88f1f2f0213 Issue #3965: Allow repeated calls to turtle.Screen, by making it a
Martin v. Löwis <martin@v.loewis.de>
parents: 49291
diff changeset
3555 class _Screen(TurtleScreen):
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3556
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3557 _root = None
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3558 _canvas = None
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3559 _title = _CFG["title"]
9585
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
3560
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
3561 def __init__(self):
49344
d88f1f2f0213 Issue #3965: Allow repeated calls to turtle.Screen, by making it a
Martin v. Löwis <martin@v.loewis.de>
parents: 49291
diff changeset
3562 # XXX there is no need for this code to be conditional,
d88f1f2f0213 Issue #3965: Allow repeated calls to turtle.Screen, by making it a
Martin v. Löwis <martin@v.loewis.de>
parents: 49291
diff changeset
3563 # as there will be only a single _Screen instance, anyway
d88f1f2f0213 Issue #3965: Allow repeated calls to turtle.Screen, by making it a
Martin v. Löwis <martin@v.loewis.de>
parents: 49291
diff changeset
3564 # XXX actually, the turtle demo is injecting root window,
d88f1f2f0213 Issue #3965: Allow repeated calls to turtle.Screen, by making it a
Martin v. Löwis <martin@v.loewis.de>
parents: 49291
diff changeset
3565 # so perhaps the conditional creation of a root should be
d88f1f2f0213 Issue #3965: Allow repeated calls to turtle.Screen, by making it a
Martin v. Löwis <martin@v.loewis.de>
parents: 49291
diff changeset
3566 # preserved (perhaps by passing it as an optional parameter)
d88f1f2f0213 Issue #3965: Allow repeated calls to turtle.Screen, by making it a
Martin v. Löwis <martin@v.loewis.de>
parents: 49291
diff changeset
3567 if _Screen._root is None:
d88f1f2f0213 Issue #3965: Allow repeated calls to turtle.Screen, by making it a
Martin v. Löwis <martin@v.loewis.de>
parents: 49291
diff changeset
3568 _Screen._root = self._root = _Root()
d88f1f2f0213 Issue #3965: Allow repeated calls to turtle.Screen, by making it a
Martin v. Löwis <martin@v.loewis.de>
parents: 49291
diff changeset
3569 self._root.title(_Screen._title)
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3570 self._root.ondestroy(self._destroy)
49344
d88f1f2f0213 Issue #3965: Allow repeated calls to turtle.Screen, by making it a
Martin v. Löwis <martin@v.loewis.de>
parents: 49291
diff changeset
3571 if _Screen._canvas is None:
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3572 width = _CFG["width"]
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3573 height = _CFG["height"]
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3574 canvwidth = _CFG["canvwidth"]
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3575 canvheight = _CFG["canvheight"]
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3576 leftright = _CFG["leftright"]
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3577 topbottom = _CFG["topbottom"]
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3578 self._root.setupcanvas(width, height, canvwidth, canvheight)
49344
d88f1f2f0213 Issue #3965: Allow repeated calls to turtle.Screen, by making it a
Martin v. Löwis <martin@v.loewis.de>
parents: 49291
diff changeset
3579 _Screen._canvas = self._root._getcanvas()
56267
56b530029bac Merged revisions 75115 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 55049
diff changeset
3580 TurtleScreen.__init__(self, _Screen._canvas)
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3581 self.setup(width, height, leftright, topbottom)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3582
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3583 def setup(self, width=_CFG["width"], height=_CFG["height"],
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3584 startx=_CFG["leftright"], starty=_CFG["topbottom"]):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3585 """ Set the size and position of the main window.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3586
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3587 Arguments:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3588 width: as integer a size in pixels, as float a fraction of the screen.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3589 Default is 50% of screen.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3590 height: as integer the height in pixels, as float a fraction of the
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3591 screen. Default is 75% of screen.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3592 startx: if positive, starting position in pixels from the left
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3593 edge of the screen, if negative from the right edge
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3594 Default, startx=None is to center window horizontally.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3595 starty: if positive, starting position in pixels from the top
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3596 edge of the screen, if negative from the bottom edge
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3597 Default, starty=None is to center window vertically.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3598
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3599 Examples (for a Screen instance named screen):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3600 >>> screen.setup (width=200, height=200, startx=0, starty=0)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3601
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3602 sets window to 200x200 pixels, in upper left of screen
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3603
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3604 >>> screen.setup(width=.75, height=0.5, startx=None, starty=None)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3605
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3606 sets window to 75% of screen by 50% of screen and centers
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3607 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3608 if not hasattr(self._root, "set_geometry"):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3609 return
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3610 sw = self._root.win_width()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3611 sh = self._root.win_height()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3612 if isinstance(width, float) and 0 <= width <= 1:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3613 width = sw*width
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3614 if startx is None:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3615 startx = (sw - width) / 2
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3616 if isinstance(height, float) and 0 <= height <= 1:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3617 height = sh*height
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3618 if starty is None:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3619 starty = (sh - height) / 2
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3620 self._root.set_geometry(width, height, startx, starty)
56267
56b530029bac Merged revisions 75115 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 55049
diff changeset
3621 self.update()
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3622
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3623 def title(self, titlestring):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3624 """Set title of turtle-window
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3625
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3626 Argument:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3627 titlestring -- a string, to appear in the titlebar of the
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3628 turtle graphics window.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3629
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3630 This is a method of Screen-class. Not available for TurtleScreen-
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3631 objects.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3632
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3633 Example (for a Screen instance named screen):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3634 >>> screen.title("Welcome to the turtle-zoo!")
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3635 """
49344
d88f1f2f0213 Issue #3965: Allow repeated calls to turtle.Screen, by making it a
Martin v. Löwis <martin@v.loewis.de>
parents: 49291
diff changeset
3636 if _Screen._root is not None:
d88f1f2f0213 Issue #3965: Allow repeated calls to turtle.Screen, by making it a
Martin v. Löwis <martin@v.loewis.de>
parents: 49291
diff changeset
3637 _Screen._root.title(titlestring)
d88f1f2f0213 Issue #3965: Allow repeated calls to turtle.Screen, by making it a
Martin v. Löwis <martin@v.loewis.de>
parents: 49291
diff changeset
3638 _Screen._title = titlestring
9585
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
3639
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
3640 def _destroy(self):
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3641 root = self._root
49344
d88f1f2f0213 Issue #3965: Allow repeated calls to turtle.Screen, by making it a
Martin v. Löwis <martin@v.loewis.de>
parents: 49291
diff changeset
3642 if root is _Screen._root:
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3643 Turtle._pen = None
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3644 Turtle._screen = None
49344
d88f1f2f0213 Issue #3965: Allow repeated calls to turtle.Screen, by making it a
Martin v. Löwis <martin@v.loewis.de>
parents: 49291
diff changeset
3645 _Screen._root = None
d88f1f2f0213 Issue #3965: Allow repeated calls to turtle.Screen, by making it a
Martin v. Löwis <martin@v.loewis.de>
parents: 49291
diff changeset
3646 _Screen._canvas = None
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3647 TurtleScreen._RUNNING = True
9585
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
3648 root.destroy()
15308
847ae40074d4 Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents: 14957
diff changeset
3649
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3650 def bye(self):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3651 """Shut the turtlegraphics window.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3652
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3653 Example (for a TurtleScreen instance named screen):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3654 >>> screen.bye()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3655 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3656 self._destroy()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3657
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3658 def exitonclick(self):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3659 """Go into mainloop until the mouse is clicked.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3660
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3661 No arguments.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3662
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3663 Bind bye() method to mouseclick on TurtleScreen.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3664 If "using_IDLE" - value in configuration dictionary is False
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3665 (default value), enter mainloop.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3666 If IDLE with -n switch (no subprocess) is used, this value should be
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3667 set to True in turtle.cfg. In this case IDLE's mainloop
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3668 is active also for the client script.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3669
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3670 This is a method of the Screen-class and not available for
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3671 TurtleScreen instances.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3672
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3673 Example (for a Screen instance named screen):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3674 >>> screen.exitonclick()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3675
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3676 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3677 def exitGracefully(x, y):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3678 """Screen.bye() with two dummy-parameters"""
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3679 self.bye()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3680 self.onclick(exitGracefully)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3681 if _CFG["using_IDLE"]:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3682 return
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3683 try:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3684 mainloop()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3685 except AttributeError:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3686 exit(0)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3687
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3688
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3689 class Turtle(RawTurtle):
68588
cd8d3c2d3bce #11565: Fix several typos. Patch by Piotr Kasprzyk.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 68503
diff changeset
3690 """RawTurtle auto-creating (scrolled) canvas.
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3691
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3692 When a Turtle object is created or a function derived from some
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3693 Turtle method is called a TurtleScreen object is automatically created.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3694 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3695 _pen = None
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3696 _screen = None
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3697
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3698 def __init__(self,
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3699 shape=_CFG["shape"],
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3700 undobuffersize=_CFG["undobuffersize"],
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3701 visible=_CFG["visible"]):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3702 if Turtle._screen is None:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3703 Turtle._screen = Screen()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3704 RawTurtle.__init__(self, Turtle._screen,
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3705 shape=shape,
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3706 undobuffersize=undobuffersize,
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3707 visible=visible)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3708
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3709 Pen = Turtle
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3710
9585
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
3711 def _getpen():
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3712 """Create the 'anonymous' turtle if not already present."""
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3713 if Turtle._pen is None:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3714 Turtle._pen = Turtle()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3715 return Turtle._pen
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3716
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3717 def _getscreen():
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3718 """Create a TurtleScreen if not already present."""
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3719 if Turtle._screen is None:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3720 Turtle._screen = Screen()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3721 return Turtle._screen
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3722
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3723 def write_docstringdict(filename="turtle_docstringdict"):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3724 """Create and write docstring-dictionary to file.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3725
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3726 Optional argument:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3727 filename -- a string, used as filename
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3728 default value is turtle_docstringdict
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3729
68588
cd8d3c2d3bce #11565: Fix several typos. Patch by Piotr Kasprzyk.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 68503
diff changeset
3730 Has to be called explicitly, (not used by the turtle-graphics classes)
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3731 The docstring dictionary will be written to the Python script <filname>.py
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3732 It is intended to serve as a template for translation of the docstrings
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3733 into different languages.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3734 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3735 docsdict = {}
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3736
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3737 for methodname in _tg_screen_functions:
49344
d88f1f2f0213 Issue #3965: Allow repeated calls to turtle.Screen, by making it a
Martin v. Löwis <martin@v.loewis.de>
parents: 49291
diff changeset
3738 key = "_Screen."+methodname
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3739 docsdict[key] = eval(key).__doc__
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3740 for methodname in _tg_turtle_functions:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3741 key = "Turtle."+methodname
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3742 docsdict[key] = eval(key).__doc__
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3743
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3744 f = open("%s.py" % filename,"w")
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3745 keys = sorted([x for x in docsdict.keys()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3746 if x.split('.')[1] not in _alias_list])
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3747 f.write('docsdict = {\n\n')
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3748 for key in keys[:-1]:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3749 f.write('%s :\n' % repr(key))
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3750 f.write(' """%s\n""",\n\n' % docsdict[key])
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3751 key = keys[-1]
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3752 f.write('%s :\n' % repr(key))
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3753 f.write(' """%s\n"""\n\n' % docsdict[key])
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3754 f.write("}\n")
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3755 f.close()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3756
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3757 def read_docstrings(lang):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3758 """Read in docstrings from lang-specific docstring dictionary.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3759
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3760 Transfer docstrings, translated to lang, from a dictionary-file
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3761 to the methods of classes Screen and Turtle and - in revised form -
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3762 to the corresponding functions.
38204
fa3c4ea99d95 Patch #1486962: Several bugs in the turtle Tk demo module were fixed
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
3763 """
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3764 modname = "turtle_docstringdict_%(language)s" % {'language':lang.lower()}
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3765 module = __import__(modname)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3766 docsdict = module.docsdict
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3767 for key in docsdict:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3768 #print key
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3769 try:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3770 eval(key).im_func.__doc__ = docsdict[key]
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3771 except:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3772 print "Bad docstring-entry: %s" % key
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3773
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3774 _LANGUAGE = _CFG["language"]
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3775
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3776 try:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3777 if _LANGUAGE != "english":
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3778 read_docstrings(_LANGUAGE)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3779 except ImportError:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3780 print "Cannot find docsdict for", _LANGUAGE
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3781 except:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3782 print ("Unknown Error when trying to import %s-docstring-dictionary" %
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3783 _LANGUAGE)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3784
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3785
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3786 def getmethparlist(ob):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3787 "Get strings describing the arguments for the given object"
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3788 argText1 = argText2 = ""
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3789 # bit of a hack for methods - turn it into a function
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3790 # but we drop the "self" param.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3791 if type(ob)==types.MethodType:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3792 fob = ob.im_func
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3793 argOffset = 1
38204
fa3c4ea99d95 Patch #1486962: Several bugs in the turtle Tk demo module were fixed
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
3794 else:
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3795 fob = ob
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3796 argOffset = 0
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3797 # Try and build one for Python defined functions
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3798 if type(fob) in [types.FunctionType, types.LambdaType]:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3799 try:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3800 counter = fob.func_code.co_argcount
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3801 items2 = list(fob.func_code.co_varnames[argOffset:counter])
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3802 realArgs = fob.func_code.co_varnames[argOffset:counter]
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3803 defaults = fob.func_defaults or []
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3804 defaults = list(map(lambda name: "=%s" % repr(name), defaults))
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3805 defaults = [""] * (len(realArgs)-len(defaults)) + defaults
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3806 items1 = map(lambda arg, dflt: arg+dflt, realArgs, defaults)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3807 if fob.func_code.co_flags & 0x4:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3808 items1.append("*"+fob.func_code.co_varnames[counter])
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3809 items2.append("*"+fob.func_code.co_varnames[counter])
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3810 counter += 1
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3811 if fob.func_code.co_flags & 0x8:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3812 items1.append("**"+fob.func_code.co_varnames[counter])
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3813 items2.append("**"+fob.func_code.co_varnames[counter])
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3814 argText1 = ", ".join(items1)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3815 argText1 = "(%s)" % argText1
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3816 argText2 = ", ".join(items2)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3817 argText2 = "(%s)" % argText2
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3818 except:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3819 pass
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3820 return argText1, argText2
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3821
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3822 def _turtle_docrevise(docstr):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3823 """To reduce docstrings from RawTurtle class for functions
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3824 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3825 import re
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3826 if docstr is None:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3827 return None
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3828 turtlename = _CFG["exampleturtle"]
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3829 newdocstr = docstr.replace("%s." % turtlename,"")
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3830 parexp = re.compile(r' \(.+ %s\):' % turtlename)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3831 newdocstr = parexp.sub(":", newdocstr)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3832 return newdocstr
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3833
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3834 def _screen_docrevise(docstr):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3835 """To reduce docstrings from TurtleScreen class for functions
38204
fa3c4ea99d95 Patch #1486962: Several bugs in the turtle Tk demo module were fixed
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
3836 """
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3837 import re
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3838 if docstr is None:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3839 return None
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3840 screenname = _CFG["examplescreen"]
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3841 newdocstr = docstr.replace("%s." % screenname,"")
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3842 parexp = re.compile(r' \(.+ %s\):' % screenname)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3843 newdocstr = parexp.sub(":", newdocstr)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3844 return newdocstr
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3845
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3846 ## The following mechanism makes all methods of RawTurtle and Turtle available
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3847 ## as functions. So we can enhance, change, add, delete methods to these
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3848 ## classes and do not need to change anything here.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3849
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3850
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3851 for methodname in _tg_screen_functions:
49344
d88f1f2f0213 Issue #3965: Allow repeated calls to turtle.Screen, by making it a
Martin v. Löwis <martin@v.loewis.de>
parents: 49291
diff changeset
3852 pl1, pl2 = getmethparlist(eval('_Screen.' + methodname))
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3853 if pl1 == "":
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3854 print ">>>>>>", pl1, pl2
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3855 continue
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3856 defstr = ("def %(key)s%(pl1)s: return _getscreen().%(key)s%(pl2)s" %
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3857 {'key':methodname, 'pl1':pl1, 'pl2':pl2})
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3858 exec defstr
49344
d88f1f2f0213 Issue #3965: Allow repeated calls to turtle.Screen, by making it a
Martin v. Löwis <martin@v.loewis.de>
parents: 49291
diff changeset
3859 eval(methodname).__doc__ = _screen_docrevise(eval('_Screen.'+methodname).__doc__)
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3860
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3861 for methodname in _tg_turtle_functions:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3862 pl1, pl2 = getmethparlist(eval('Turtle.' + methodname))
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3863 if pl1 == "":
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3864 print ">>>>>>", pl1, pl2
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3865 continue
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3866 defstr = ("def %(key)s%(pl1)s: return _getpen().%(key)s%(pl2)s" %
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3867 {'key':methodname, 'pl1':pl1, 'pl2':pl2})
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3868 exec defstr
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3869 eval(methodname).__doc__ = _turtle_docrevise(eval('Turtle.'+methodname).__doc__)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3870
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3871
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3872 done = mainloop = TK.mainloop
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3873 del pl1, pl2, defstr
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3874
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3875 if __name__ == "__main__":
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3876 def switchpen():
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3877 if isdown():
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3878 pu()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3879 else:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3880 pd()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3881
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3882 def demo1():
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3883 """Demo of old turtle.py - module"""
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3884 reset()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3885 tracer(True)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3886 up()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3887 backward(100)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3888 down()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3889 # draw 3 squares; the last filled
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3890 width(3)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3891 for i in range(3):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3892 if i == 2:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3893 fill(1)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3894 for _ in range(4):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3895 forward(20)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3896 left(90)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3897 if i == 2:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3898 color("maroon")
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3899 fill(0)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3900 up()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3901 forward(30)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3902 down()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3903 width(1)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3904 color("black")
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3905 # move out of the way
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3906 tracer(False)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3907 up()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3908 right(90)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3909 forward(100)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3910 right(90)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3911 forward(100)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3912 right(180)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3913 down()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3914 # some text
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3915 write("startstart", 1)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3916 write("start", 1)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3917 color("red")
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3918 # staircase
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3919 for i in range(5):
9585
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
3920 forward(20)
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
3921 left(90)
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3922 forward(20)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3923 right(90)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3924 # filled staircase
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3925 tracer(True)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3926 fill(1)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3927 for i in range(5):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3928 forward(20)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3929 left(90)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3930 forward(20)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3931 right(90)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3932 fill(0)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3933 # more text
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3934
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3935 def demo2():
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3936 """Demo of some new features."""
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3937 speed(1)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3938 st()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3939 pensize(3)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3940 setheading(towards(0, 0))
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3941 radius = distance(0, 0)/2.0
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3942 rt(90)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3943 for _ in range(18):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3944 switchpen()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3945 circle(radius, 10)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3946 write("wait a moment...")
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3947 while undobufferentries():
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3948 undo()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3949 reset()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3950 lt(90)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3951 colormode(255)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3952 laenge = 10
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3953 pencolor("green")
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3954 pensize(3)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3955 lt(180)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3956 for i in range(-2, 16):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3957 if i > 0:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3958 begin_fill()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3959 fillcolor(255-15*i, 0, 15*i)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3960 for _ in range(3):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3961 fd(laenge)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3962 lt(120)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3963 laenge += 10
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3964 lt(15)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3965 speed((speed()+1)%12)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3966 end_fill()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3967
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3968 lt(120)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3969 pu()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3970 fd(70)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3971 rt(30)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3972 pd()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3973 color("red","yellow")
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3974 speed(0)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3975 fill(1)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3976 for _ in range(4):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3977 circle(50, 90)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3978 rt(90)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3979 fd(30)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3980 rt(90)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3981 fill(0)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3982 lt(90)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3983 pu()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3984 fd(30)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3985 pd()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3986 shape("turtle")
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3987
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3988 tri = getturtle()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3989 tri.resizemode("auto")
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3990 turtle = Turtle()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3991 turtle.resizemode("auto")
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3992 turtle.shape("turtle")
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3993 turtle.reset()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3994 turtle.left(90)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3995 turtle.speed(0)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3996 turtle.up()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3997 turtle.goto(280, 40)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3998 turtle.lt(30)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3999 turtle.down()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
4000 turtle.speed(6)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
4001 turtle.color("blue","orange")
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
4002 turtle.pensize(2)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
4003 tri.speed(6)
38204
fa3c4ea99d95 Patch #1486962: Several bugs in the turtle Tk demo module were fixed
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
4004 setheading(towards(turtle))
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
4005 count = 1
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
4006 while tri.distance(turtle) > 4:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
4007 turtle.fd(3.5)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
4008 turtle.lt(0.6)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
4009 tri.setheading(tri.towards(turtle))
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
4010 tri.fd(4)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
4011 if count % 20 == 0:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
4012 turtle.stamp()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
4013 tri.stamp()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
4014 switchpen()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
4015 count += 1
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
4016 tri.write("CAUGHT! ", font=("Arial", 16, "bold"), align="right")
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
4017 tri.pencolor("black")
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
4018 tri.pencolor("red")
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
4019
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
4020 def baba(xdummy, ydummy):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
4021 clearscreen()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
4022 bye()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
4023
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
4024 time.sleep(2)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
4025
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
4026 while undobufferentries():
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
4027 tri.undo()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
4028 turtle.undo()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
4029 tri.fd(50)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
4030 tri.write(" Click me!", font = ("Courier", 12, "bold") )
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
4031 tri.onclick(baba, 1)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
4032
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
4033 demo1()
38204
fa3c4ea99d95 Patch #1486962: Several bugs in the turtle Tk demo module were fixed
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
4034 demo2()
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
4035 exitonclick()