annotate Lib/lib-tk/turtle.py @ 58925:eecf1bef052d 2.6

Merged revisions 78207 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r78207 | ezio.melotti | 2010-02-17 01:26:09 +0200 (Wed, 17 Feb 2010) | 1 line #7930: fix stripid ........
author Ezio Melotti <ezio.melotti@gmail.com>
date Tue, 16 Feb 2010 23:29:44 +0000
parents 56b530029bac
children 9eabba6ded09 684960fb1cbf
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 #
56267
56b530029bac Merged revisions 75115 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 55049
diff changeset
5 # Copyright (C) 2006 - 2009 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
fa3c4ea99d95 Patch #1486962: Several bugs in the turtle Tk demo module were fixed
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
30 Imagine a robotic turtle starting at (0, 0) in the x-y plane. Give it
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
fa3c4ea99d95 Patch #1486962: Several bugs in the turtle Tk demo module were fixed
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
33 command turtle.left(25), and it rotates in-place 25 degrees clockwise.
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
51761
6ce96ce4d9af Merged revisions 69816 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 49758
diff changeset
99 extensions in 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:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
338 if _dict.has_key(ex):
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):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
341 if _dict.has_key(ex):
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
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
786 if canvwidth is None and canvheight is None and bg is None:
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")
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
862 ### .. add more components and then use register_shape()
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):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
961 screen.clear()
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 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1002 if mode == None:
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):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1033 left(10)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1034 forward(0.5)
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)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1139 >>> turtle.pencolor(240,160,80)
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):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1207 fd(dist)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1208 rt(90)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1209 dist += 2
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):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1236 "Increment upadate counter."""
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
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1307 >>> screen.onclick(turtle.goto)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1308
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1309 ### Subsequently clicking into the TurtleScreen will
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1310 ### make the turtle move to the clicked point.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1311 >>> screen.onclick(None)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1312
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1313 ### event-binding will be removed
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1314 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1315 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
1316
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1317 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
1318 """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
1319
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1320 Arguments:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1321 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
1322 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
1323
51761
6ce96ce4d9af Merged revisions 69816 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 49758
diff changeset
1324 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
1325 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
1326
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1327 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
1328 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
1329
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1330 >>> def f():
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1331 fd(50)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1332 lt(60)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1333
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1334
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1335 >>> screen.onkey(f, "Up")
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1336 >>> screen.listen()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1337
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1338 ### Subsequently the turtle can be moved by
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1339 ### repeatedly pressing the up-arrow key,
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1340 ### consequently drawing a hexagon
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1341 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1342 if fun == None:
56267
56b530029bac Merged revisions 75115 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 55049
diff changeset
1343 if key in self._keys:
56b530029bac Merged revisions 75115 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 55049
diff changeset
1344 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
1345 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
1346 self._keys.append(key)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1347 self._onkey(fun, key)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1348
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1349 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
1350 """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
1351
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1352 No arguments.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1353 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
1354 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
1355
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1356 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
1357 >>> screen.listen()
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 self._listen()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1360
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1361 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
1362 """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
1363
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1364 Arguments:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1365 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
1366 t -- a number >= 0
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1367
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1368 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
1369
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1370 >>> running = True
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1371 >>> def f():
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1372 if running:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1373 fd(50)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1374 lt(60)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1375 screen.ontimer(f, 250)
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 >>> f() ### makes the turtle marching around
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1378 >>> running = False
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 self._ontimer(fun, t)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1381
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1382 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
1383 """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
1384
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1385 Optional argument:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1386 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
1387
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1388 If picname is a filename, set the corresponing image as background.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1389 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
1390 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
1391
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1392 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
1393 >>> screen.bgpic()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1394 'nopic'
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1395 >>> screen.bgpic("landscape.gif")
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1396 >>> screen.bgpic()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1397 'landscape.gif'
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1398 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1399 if picname is None:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1400 return self._bgpicname
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1401 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
1402 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
1403 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
1404 self._bgpicname = picname
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1405
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1406 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
1407 """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
1408
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1409 Optional arguments:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1410 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
1411 canvheight -- positive integer, new height of canvas in pixels
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1412 bg -- colorstring or color-tupel, new backgroundcolor
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1413 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
1414
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1415 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
1416 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
1417 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
1418
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1419 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
1420 >>> turtle.screensize(2000,1500)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1421 ### e. g. to search for an erroneously escaped turtle ;-)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1422 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1423 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
1424
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1425 onscreenclick = onclick
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1426 resetscreen = reset
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1427 clearscreen = clear
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1428 addshape = register_shape
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1429
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1430 class TNavigator(object):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1431 """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
1432 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
1433 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1434 START_ORIENTATION = {
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1435 "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
1436 "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
1437 "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
1438 DEFAULT_MODE = "standard"
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1439 DEFAULT_ANGLEOFFSET = 0
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1440 DEFAULT_ANGLEORIENT = 1
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1441
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1442 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
1443 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
1444 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
1445 self._mode = mode
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1446 self.undobuffer = None
9585
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
1447 self.degrees()
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1448 self._mode = None
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1449 self._setmode(mode)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1450 TNavigator.reset(self)
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 def reset(self):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1453 """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
1454
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1455 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
1456 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1457 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
1458 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
1459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1460 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
1461 """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
1462 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1463 if mode == None:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1464 return self._mode
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1465 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
1466 return
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1467 self._mode = mode
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1468 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
1469 self._angleOffset = 0
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1470 self._angleOrient = 1
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1471 else: # mode == "logo":
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1472 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
1473 self._angleOrient = -1
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1474
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1475 def _setDegreesPerAU(self, fullcircle):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1476 """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
1477 self._fullcircle = fullcircle
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1478 self._degreesPerAU = 360/fullcircle
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1479 if self._mode == "standard":
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1480 self._angleOffset = 0
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1481 else:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1482 self._angleOffset = fullcircle/4.
9585
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
1483
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
1484 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
1485 """ 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
1486
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1487 Optional argument:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1488 fullcircle - a number
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1489
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1490 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
1491 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
1492 360 degrees.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1493
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1494 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
1495 >>> turtle.left(90)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1496 >>> turtle.heading()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1497 90
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1498 >>> turtle.degrees(400.0) # angle measurement in gon
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1499 >>> turtle.heading()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1500 100
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1501
38204
fa3c4ea99d95 Patch #1486962: Several bugs in the turtle Tk demo module were fixed
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
1502 """
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1503 self._setDegreesPerAU(fullcircle)
9585
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
1504
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
1505 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
1506 """ 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
1507
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1508 No arguments.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1509
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1510 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
1511 >>> turtle.heading()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1512 90
38204
fa3c4ea99d95 Patch #1486962: Several bugs in the turtle Tk demo module were fixed
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
1513 >>> turtle.radians()
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1514 >>> turtle.heading()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1515 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
1516 """
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1517 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
1518
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1519 def _go(self, distance):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1520 """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
1521 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
1522 self._goto(ende)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1523
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1524 def _rotate(self, angle):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1525 """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
1526 angle *= self._degreesPerAU
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1527 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
1528
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1529 def _goto(self, end):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1530 """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
1531 self._position = end
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1532
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1533 def forward(self, distance):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1534 """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
1535
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1536 Aliases: forward | fd
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1537
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1538 Argument:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1539 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
1540
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1541 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
1542 the turtle is headed.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1543
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1544 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
1545 >>> turtle.position()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1546 (0.00, 0.00)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1547 >>> turtle.forward(25)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1548 >>> turtle.position()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1549 (25.00,0.00)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1550 >>> turtle.forward(-75)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1551 >>> turtle.position()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1552 (-50.00,0.00)
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 self._go(distance)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1555
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1556 def back(self, distance):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1557 """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
1558
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1559 Aliases: back | backward | bk
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1560
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1561 Argument:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1562 distance -- a number
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1563
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1564 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
1565 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
1566
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1567 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
1568 >>> turtle.position()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1569 (0.00, 0.00)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1570 >>> turtle.backward(30)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1571 >>> turtle.position()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1572 (-30.00, 0.00)
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 self._go(-distance)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1575
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1576 def right(self, angle):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1577 """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
1578
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1579 Aliases: right | rt
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1580
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1581 Argument:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1582 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
1583
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1584 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
1585 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
1586 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
1587
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1588 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
1589 >>> turtle.heading()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1590 22.0
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1591 >>> turtle.right(45)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1592 >>> turtle.heading()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1593 337.0
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 self._rotate(-angle)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1596
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1597 def left(self, angle):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1598 """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
1599
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1600 Aliases: left | lt
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1601
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1602 Argument:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1603 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
1604
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1605 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
1606 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
1607 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
1608
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1609 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
1610 >>> turtle.heading()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1611 22.0
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1612 >>> turtle.left(45)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1613 >>> turtle.heading()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1614 67.0
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 self._rotate(angle)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1617
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1618 def pos(self):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1619 """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
1620
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1621 Aliases: pos | position
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 No arguments.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1624
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1625 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
1626 >>> turtle.pos()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1627 (0.00, 240.00)
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 return self._position
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1630
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1631 def xcor(self):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1632 """ 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
1633
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1634 No arguments.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1635
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1636 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
1637 >>> reset()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1638 >>> turtle.left(60)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1639 >>> turtle.forward(100)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1640 >>> print turtle.xcor()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1641 50.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 return self._position[0]
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1644
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1645 def ycor(self):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1646 """ 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
1647 ---
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1648 No arguments.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1649
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1650 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
1651 >>> reset()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1652 >>> turtle.left(60)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1653 >>> turtle.forward(100)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1654 >>> print turtle.ycor()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1655 86.6025403784
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 return self._position[1]
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1658
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1659
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1660 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
1661 """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
1662
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1663 Aliases: setpos | setposition | goto:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1664
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1665 Arguments:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1666 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
1667 y -- a number None
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1668
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1669 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
1670 --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
1671 --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
1672
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1673 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
1674 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
1675
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1676 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
1677 >>> tp = turtle.pos()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1678 >>> tp
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1679 (0.00, 0.00)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1680 >>> turtle.setpos(60,30)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1681 >>> turtle.pos()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1682 (60.00,30.00)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1683 >>> turtle.setpos((20,80))
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1684 >>> turtle.pos()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1685 (20.00,80.00)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1686 >>> turtle.setpos(tp)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1687 >>> turtle.pos()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1688 (0.00,0.00)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1689 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1690 if y is None:
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))
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1692 else:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1693 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
1694
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1695 def home(self):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1696 """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
1697
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1698 No arguments.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1699
51761
6ce96ce4d9af Merged revisions 69816 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 49758
diff changeset
1700 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
1701 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
1702
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1703 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
1704 >>> turtle.home()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1705 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1706 self.goto(0, 0)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1707 self.setheading(0)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1708
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1709 def setx(self, x):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1710 """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
1711
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1712 Argument:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1713 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
1714
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1715 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
1716 unchanged.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1717
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1718 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
1719 >>> turtle.position()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1720 (0.00, 240.00)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1721 >>> turtle.setx(10)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1722 >>> turtle.position()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1723 (10.00, 240.00)
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 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
1726
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1727 def sety(self, y):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1728 """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
1729
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1730 Argument:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1731 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
1732
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1733 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
1734 unchanged.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1735
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1736 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
1737 >>> turtle.position()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1738 (0.00, 40.00)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1739 >>> turtle.sety(-10)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1740 >>> turtle.position()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1741 (0.00, -10.00)
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 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
1744
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1745 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
1746 """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
1747
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1748 Arguments:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1749 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
1750 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
1751
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1752 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
1753 --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
1754 --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
1755 --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
1756
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1757 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
1758 >>> turtle.pos()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1759 (0.00, 0.00)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1760 >>> turtle.distance(30,40)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1761 50.0
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1762 >>> pen = Turtle()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1763 >>> pen.forward(77)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1764 >>> turtle.distance(pen)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1765 77.0
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1766 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1767 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
1768 pos = Vec2D(x, y)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1769 if isinstance(x, Vec2D):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1770 pos = 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, tuple):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1772 pos = Vec2D(*x)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1773 elif isinstance(x, TNavigator):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1774 pos = x._position
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1775 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
1776
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1777 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
1778 """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
1779
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1780 Arguments:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1781 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
1782 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
1783
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1784 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
1785 --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
1786 --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
1787 --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
1788
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1789 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
1790 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
1791 modes - "standard" or "logo")
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1792
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1793 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
1794 >>> turtle.pos()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1795 (10.00, 10.00)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1796 >>> turtle.towards(0,0)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1797 225.0
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1798 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1799 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
1800 pos = Vec2D(x, y)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1801 if isinstance(x, Vec2D):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1802 pos = 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, tuple):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1804 pos = Vec2D(*x)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1805 elif isinstance(x, TNavigator):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1806 pos = x._position
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1807 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
1808 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
1809 result /= self._degreesPerAU
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1810 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
1811
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1812 def heading(self):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1813 """ 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
1814
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1815 No arguments.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1816
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1817 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
1818 >>> turtle.left(67)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1819 >>> turtle.heading()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1820 67.0
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1821 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1822 x, y = self._orient
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1823 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
1824 result /= self._degreesPerAU
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1825 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
1826
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1827 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
1828 """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
1829
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1830 Aliases: setheading | seth
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1831
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1832 Argument:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1833 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
1834
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1835 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
1836 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
1837
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1838 standard - mode: logo-mode:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1839 -------------------|--------------------
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1840 0 - east 0 - north
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1841 90 - north 90 - east
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1842 180 - west 180 - south
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1843 270 - south 270 - west
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1844
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1845 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
1846 >>> turtle.setheading(90)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1847 >>> turtle.heading()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1848 90
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1849 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1850 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
1851 full = self._fullcircle
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1852 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
1853 self._rotate(angle)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1854
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1855 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
1856 """ 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
1857
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1858 Arguments:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1859 radius -- a number
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1860 extent (optional) -- a number
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1861 steps (optional) -- an integer
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1862
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1863 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
1864 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
1865 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
1866 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
1867 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
1868 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
1869 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
1870
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1871 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
1872 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
1873 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
1874 polygons.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1875
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1876 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
1877 --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
1878 --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
1879 --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
1880
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1881 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
1882 >>> turtle.circle(50)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1883 >>> 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
1884 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1885 if self.undobuffer:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1886 self.undobuffer.push(["seq"])
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1887 self.undobuffer.cumulate = True
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1888 speed = self.speed()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1889 if extent is None:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1890 extent = self._fullcircle
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1891 if steps is None:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1892 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
1893 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
1894 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
1895 w2 = 0.5 * w
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1896 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
1897 if radius < 0:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1898 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
1899 tr = self.tracer()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1900 dl = self._delay()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1901 if speed == 0:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1902 self.tracer(0, 0)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1903 else:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1904 self.speed(0)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1905 self._rotate(w2)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1906 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
1907 self.speed(speed)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1908 self._go(l)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1909 self.speed(0)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1910 self._rotate(w)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1911 self._rotate(-w2)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1912 if speed == 0:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1913 self.tracer(tr, dl)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1914 self.speed(speed)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1915 if self.undobuffer:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1916 self.undobuffer.cumulate = False
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 ## 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
1919
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1920 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
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 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
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 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
1925 """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
1926
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1927 fd = forward
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1928 bk = back
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1929 backward = back
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1930 rt = right
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1931 lt = left
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1932 position = pos
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1933 setpos = goto
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1934 setposition = goto
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1935 seth = setheading
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1936
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1937
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1938 class TPen(object):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1939 """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
1940 Implements drawing properties.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1941 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1942 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
1943 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
1944 self.undobuffer = None
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1945 TPen._reset(self)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1946
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1947 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
1948 fillcolor=_CFG["fillcolor"]):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1949 self._pensize = 1
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1950 self._shown = True
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1951 self._pencolor = pencolor
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1952 self._fillcolor = fillcolor
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1953 self._drawing = True
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1954 self._speed = 3
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1955 self._stretchfactor = (1, 1)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1956 self._tilt = 0
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1957 self._outlinewidth = 1
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1958 ### 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
1959
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1960 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
1961 """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
1962
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1963 (Optional) Argument:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1964 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
1965
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1966 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
1967 - "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
1968 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
1969 - "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
1970 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
1971 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
1972 - "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
1973 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
1974 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
1975
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1976
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1977 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
1978 >>> turtle.resizemode("noresize")
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1979 >>> turtle.resizemode()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1980 'noresize'
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1981 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1982 if rmode is None:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1983 return self._resizemode
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1984 rmode = rmode.lower()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1985 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
1986 self.pen(resizemode=rmode)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1987
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1988 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
1989 """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
1990
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1991 Aliases: pensize | width
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1992
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1993 Argument:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1994 width -- positive number
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1995
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
1996 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
1997 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
1998 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
1999 is returned.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2000
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2001 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
2002 >>> turtle.pensize()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2003 1
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2004 turtle.pensize(10) # from here on lines of width 10 are drawn
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2005 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2006 if width is None:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2007 return self._pensize
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2008 self.pen(pensize=width)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2009
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2010
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2011 def penup(self):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2012 """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
2013
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2014 Aliases: penup | pu | up
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 No argument
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2017
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2018 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
2019 >>> turtle.penup()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2020 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2021 if not self._drawing:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2022 return
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2023 self.pen(pendown=False)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2024
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2025 def pendown(self):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2026 """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
2027
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2028 Aliases: pendown | pd | down
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 No argument.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2031
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2032 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
2033 >>> turtle.pendown()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2034 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2035 if self._drawing:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2036 return
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2037 self.pen(pendown=True)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2038
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2039 def isdown(self):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2040 """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
2041
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2042 No argument.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2043
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2044 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
2045 >>> turtle.penup()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2046 >>> turtle.isdown()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2047 False
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2048 >>> turtle.pendown()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2049 >>> turtle.isdown()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2050 True
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 return self._drawing
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2053
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2054 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
2055 """ 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
2056
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2057 Optional argument:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2058 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
2059
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2060 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
2061 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
2062
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2063 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
2064 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
2065 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
2066 'fastest' : 0
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2067 'fast' : 10
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2068 'normal' : 6
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2069 'slow' : 3
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2070 'slowest' : 1
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2071 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
2072 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
2073
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2074 Attention:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2075 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
2076 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
2077
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2078 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
2079 >>> turtle.speed(3)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2080 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2081 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
2082 if speed is None:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2083 return self._speed
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2084 if speed in speeds:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2085 speed = speeds[speed]
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2086 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
2087 speed = int(round(speed))
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2088 else:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2089 speed = 0
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2090 self.pen(speed=speed)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2091
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2092 def color(self, *args):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2093 """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
2094
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2095 Arguments:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2096 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
2097 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
2098
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2099 color()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2100 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
2101 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
2102 by pencolor and fillcolor.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2103 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
2104 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
2105 to the given value.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2106 color(colorstring1, colorstring2),
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2107 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
2108 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
2109 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
2110
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2111 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
2112 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
2113 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
2114
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2115 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
2116 >>> turtle.color('red', 'green')
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2117 >>> turtle.color()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2118 ('red', 'green')
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2119 >>> colormode(255)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2120 >>> 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
2121 >>> color()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2122 ('#285078', '#a0c8f0')
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2123 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2124 if args:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2125 l = len(args)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2126 if l == 1:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2127 pcolor = fcolor = args[0]
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2128 elif l == 2:
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 elif l == 3:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2131 pcolor = fcolor = args
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2132 pcolor = self._colorstr(pcolor)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2133 fcolor = self._colorstr(fcolor)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2134 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
2135 else:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2136 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
2137
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2138 def pencolor(self, *args):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2139 """ 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
2140
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2141 Arguments:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2142 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
2143 - pencolor()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2144 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
2145 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
2146 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
2147 - pencolor(colorstring)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2148 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
2149 - pencolor((r, g, b))
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2150 *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
2151 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
2152 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
2153 - pencolor(r, g, b)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2154 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
2155 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
2156
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2157 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
2158 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
2159
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2160 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
2161 >>> turtle.pencolor('brown')
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2162 >>> 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
2163 >>> turtle.pencolor(tup)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2164 >>> turtle.pencolor()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2165 '#33cc8c'
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2166 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2167 if args:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2168 color = self._colorstr(args)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2169 if color == self._pencolor:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2170 return
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2171 self.pen(pencolor=color)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2172 else:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2173 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
2174
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2175 def fillcolor(self, *args):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2176 """ 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
2177
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2178 Arguments:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2179 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
2180 - fillcolor()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2181 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
2182 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
2183 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
2184 - fillcolor(colorstring)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2185 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
2186 - fillcolor((r, g, b))
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2187 *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
2188 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
2189 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
2190 - fillcolor(r, g, b)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2191 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
2192 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
2193
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2194 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
2195 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
2196
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2197 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
2198 >>> turtle.fillcolor('violet')
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2199 >>> col = turtle.pencolor()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2200 >>> turtle.fillcolor(col)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2201 >>> 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
2202 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2203 if args:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2204 color = self._colorstr(args)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2205 if color == self._fillcolor:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2206 return
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2207 self.pen(fillcolor=color)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2208 else:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2209 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
2210
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2211 def showturtle(self):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2212 """Makes the turtle visible.
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 Aliases: showturtle | st
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 No argument.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2217
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2218 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
2219 >>> turtle.hideturtle()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2220 >>> turtle.showturtle()
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 self.pen(shown=True)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2223
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2224 def hideturtle(self):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2225 """Makes the turtle invisible.
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 Aliases: hideturtle | ht
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 No argument.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2230
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2231 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
2232 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
2233 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
2234
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2235 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
2236 >>> turtle.hideturtle()
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 self.pen(shown=False)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2239
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2240 def isvisible(self):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2241 """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
2242
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2243 No argument.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2244
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2245 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
2246 >>> turtle.hideturtle()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2247 >>> print turtle.isvisible():
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2248 False
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 return self._shown
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2251
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2252 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
2253 """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
2254
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2255 Arguments:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2256 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
2257 **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
2258 listed keys as keywords.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2259
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2260 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
2261 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
2262 "shown" : True/False
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2263 "pendown" : True/False
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2264 "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
2265 "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
2266 "pensize" : positive number
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2267 "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
2268 "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
2269 "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
2270 "outline" : positive number
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2271 "tilt" : number
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2272
51761
6ce96ce4d9af Merged revisions 69816 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 49758
diff changeset
2273 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
2274 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
2275 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
2276 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
2277
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2278
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2279 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
2280 >>> 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
2281 >>> turtle.pen()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2282 {'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
2283 '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
2284 '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
2285 >>> penstate=turtle.pen()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2286 >>> turtle.color("yellow","")
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2287 >>> turtle.penup()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2288 >>> turtle.pen()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2289 {'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
2290 '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
2291 '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
2292 >>> 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
2293 >>> p.pen()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2294 {'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
2295 '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
2296 '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
2297 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2298 _pd = {"shown" : self._shown,
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2299 "pendown" : self._drawing,
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2300 "pencolor" : self._pencolor,
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2301 "fillcolor" : self._fillcolor,
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2302 "pensize" : self._pensize,
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2303 "speed" : self._speed,
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2304 "resizemode" : self._resizemode,
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2305 "stretchfactor" : self._stretchfactor,
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2306 "outline" : self._outlinewidth,
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2307 "tilt" : self._tilt
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2308 }
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2309
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2310 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
2311 return _pd
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2312
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2313 if isinstance(pen, dict):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2314 p = pen
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2315 else:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2316 p = {}
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2317 p.update(pendict)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2318
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2319 _p_buf = {}
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2320 for key in p:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2321 _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
2322
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2323 if self.undobuffer:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2324 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
2325
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2326 newLine = False
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2327 if "pendown" in p:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2328 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
2329 newLine = True
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2330 if "pencolor" in p:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2331 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
2332 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
2333 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
2334 newLine = True
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2335 if "pensize" in p:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2336 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
2337 newLine = True
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2338 if newLine:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2339 self._newLine()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2340 if "pendown" in p:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2341 self._drawing = p["pendown"]
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2342 if "pencolor" in p:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2343 self._pencolor = p["pencolor"]
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2344 if "pensize" in p:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2345 self._pensize = p["pensize"]
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2346 if "fillcolor" in p:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2347 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
2348 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
2349 self._fillcolor = p["fillcolor"]
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2350 if "speed" in p:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2351 self._speed = p["speed"]
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2352 if "resizemode" in p:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2353 self._resizemode = p["resizemode"]
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2354 if "stretchfactor" in p:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2355 sf = p["stretchfactor"]
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2356 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
2357 sf = (sf, sf)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2358 self._stretchfactor = sf
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2359 if "outline" in p:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2360 self._outlinewidth = p["outline"]
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2361 if "shown" in p:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2362 self._shown = p["shown"]
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2363 if "tilt" in p:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2364 self._tilt = p["tilt"]
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2365 self._update()
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 ## 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
2368
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2369 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
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 _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
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 _color(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 def _colorstr(self, args):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2376 """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
2377
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2378 width = pensize
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2379 up = penup
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2380 pu = penup
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2381 pd = pendown
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2382 down = pendown
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2383 st = showturtle
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2384 ht = hideturtle
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2385
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2386
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2387 class _TurtleImage(object):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2388 """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
2389 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2390
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2391 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
2392 self.screen = screen
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2393 self._type = None
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2394 self._setshape(shapeIndex)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2395
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2396 def _setshape(self, shapeIndex):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2397 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
2398 self.shapeIndex = shapeIndex
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2399 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
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 == "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
2402 return
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2403 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
2404 screen._delete(self._item)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2405 elif self._type == "compound":
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2406 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
2407 screen._delete(item)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2408 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
2409 if self._type == "polygon":
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2410 self._item = screen._createpoly()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2411 elif self._type == "image":
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2412 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
2413 elif self._type == "compound":
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2414 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
2415 screen._shapes[shapeIndex]._data]
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2416
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2417
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2418 class RawTurtle(TPen, TNavigator):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2419 """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
2420 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
2421 its animation.
47459
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 screens = []
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2424
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2425 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
2426 shape=_CFG["shape"],
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2427 undobuffersize=_CFG["undobuffersize"],
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2428 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
2429 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
2430 self.screen = canvas
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2431 elif isinstance(canvas, TurtleScreen):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2432 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
2433 RawTurtle.screens.append(canvas)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2434 self.screen = canvas
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2435 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
2436 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
2437 if screen.cv == canvas:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2438 self.screen = screen
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2439 break
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2440 else:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2441 self.screen = TurtleScreen(canvas)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2442 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
2443 else:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2444 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
2445
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2446 screen = self.screen
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2447 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
2448 TPen.__init__(self)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2449 screen._turtles.append(self)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2450 self.drawingLineItem = screen._createline()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2451 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
2452 self._poly = None
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2453 self._creatingPoly = False
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2454 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
2455 self._shown = visible
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2456 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
2457 self.currentLineItem = screen._createline()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2458 self.currentLine = [self._position]
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2459 self.items = [self.currentLineItem]
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2460 self.stampItems = []
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2461 self._undobuffersize = undobuffersize
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2462 self.undobuffer = Tbuffer(undobuffersize)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2463 self._update()
9585
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
2464
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
2465 def reset(self):
51761
6ce96ce4d9af Merged revisions 69816 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 49758
diff changeset
2466 """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
2467
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2468 No argument.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2469 ,
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2470 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
2471 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
2472
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2473 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
2474 >>> turtle.position()
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2475 (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
2476 >>> turtle.heading()
fa3c4ea99d95 Patch #1486962: Several bugs in the turtle Tk demo module were fixed
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
2477 100.0
fa3c4ea99d95 Patch #1486962: Several bugs in the turtle Tk demo module were fixed
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
2478 >>> turtle.reset()
fa3c4ea99d95 Patch #1486962: Several bugs in the turtle Tk demo module were fixed
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
2479 >>> turtle.position()
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2480 (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
2481 >>> turtle.heading()
fa3c4ea99d95 Patch #1486962: Several bugs in the turtle Tk demo module were fixed
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
2482 0.0
fa3c4ea99d95 Patch #1486962: Several bugs in the turtle Tk demo module were fixed
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
2483 """
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2484 TNavigator.reset(self)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2485 TPen._reset(self)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2486 self._clear()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2487 self._drawturtle()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2488 self._update()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2489
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2490 def setundobuffer(self, size):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2491 """Set or disable undobuffer.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2492
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2493 Argument:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2494 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
2495
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2496 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
2497 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
2498 by the undo() function.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2499 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
2500
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2501 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
2502 >>> turtle.setundobuffer(42)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2503 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2504 if size is None:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2505 self.undobuffer = None
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2506 else:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2507 self.undobuffer = Tbuffer(size)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2508
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2509 def undobufferentries(self):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2510 """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
2511
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2512 No argument.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2513
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2514 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
2515 >>> while undobufferentries():
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2516 undo()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2517 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2518 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
2519 return 0
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2520 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
2521
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2522 def _clear(self):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2523 """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
2524 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
2525 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
2526 self.screen._delete(item)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2527 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
2528 self.currentLine = []
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2529 if self._drawing:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2530 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
2531 self.items = [self.currentLineItem]
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2532 self.clearstamps()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2533 self.setundobuffer(self._undobuffersize)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2534
9585
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
2535
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
2536 def clear(self):
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2537 """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
2538
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2539 No arguments.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2540
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2541 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
2542 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
2543 turtles are not affected.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2544
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2545 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
2546 >>> turtle.clear()
fa3c4ea99d95 Patch #1486962: Several bugs in the turtle Tk demo module were fixed
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
2547 """
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2548 self._clear()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2549 self._update()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2550
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2551 def _update_data(self):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2552 self.screen._incrementudc()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2553 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
2554 return
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2555 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
2556 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
2557 self._pencolor, self._pensize)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2558
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2559 def _update(self):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2560 """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
2561 """
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2562 screen = self.screen
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2563 if screen._tracing == 0:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2564 return
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2565 elif screen._tracing == 1:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2566 self._update_data()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2567 self._drawturtle()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2568 screen._update() # TurtleScreenBase
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2569 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
2570 else:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2571 self._update_data()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2572 if screen._updatecounter == 0:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2573 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
2574 t._drawturtle()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2575 screen._update()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2576
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2577 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
2578 """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
2579
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2580 Optional arguments:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2581 n -- nonnegative integer
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2582 delay -- nonnegative integer
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2583
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2584 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
2585 (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
2586 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
2587
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2588 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
2589 >>> turtle.tracer(8, 25)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2590 >>> dist = 2
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2591 >>> for i in range(200):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2592 turtle.fd(dist)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2593 turtle.rt(90)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2594 dist += 2
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 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
2597
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2598 def _color(self, args):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2599 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
2600
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2601 def _colorstr(self, args):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2602 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
2603
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2604 def _cc(self, args):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2605 """Convert colortriples to hexstrings.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2606 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2607 if isinstance(args, str):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2608 return args
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2609 try:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2610 r, g, b = args
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2611 except:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2612 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
2613 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
2614 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
2615 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
2616 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
2617 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
2618
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2619 def clone(self):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2620 """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
2621
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2622 No argument.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2623
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2624 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
2625 and turtle properties.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2626
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2627 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
2628 mick = Turtle()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2629 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
2630 """
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2631 screen = self.screen
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2632 self._newLine(self._drawing)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2633
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2634 turtle = self.turtle
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2635 self.screen = None
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2636 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
2637
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2638 q = deepcopy(self)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2639
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2640 self.screen = screen
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2641 self.turtle = turtle
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2642
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2643 q.screen = screen
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2644 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
2645
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2646 screen._turtles.append(q)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2647 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
2648 if ttype == "polygon":
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._createpoly()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2650 elif ttype == "image":
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._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
2652 elif ttype == "compound":
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2653 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
2654 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
2655 q.currentLineItem = screen._createline()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2656 q._update()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2657 return q
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2658
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2659 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
2660 """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
2661
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2662 Optional argument:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2663 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
2664
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2665 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
2666 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
2667 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
2668 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
2669 '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
2670 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
2671
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2672 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
2673 >>> turtle.shape()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2674 'arrow'
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2675 >>> turtle.shape("turtle")
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2676 >>> turtle.shape()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2677 'turtle'
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2678 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2679 if name is None:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2680 return self.turtle.shapeIndex
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2681 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
2682 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
2683 self.turtle._setshape(name)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2684 self._update()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2685
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2686 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
2687 """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
2688
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2689 Optinonal arguments:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2690 stretch_wid : positive number
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2691 stretch_len : positive number
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2692 outline : positive number
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2693
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2694 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
2695 Set resizemode to "user".
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2696 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
2697 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
2698 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
2699 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
2700 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
2701
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2702 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
2703 >>> turtle.resizemode("user")
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2704 >>> 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
2705 >>> 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
2706 """
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2707 if stretch_wid is None and stretch_len is None and outline == None:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2708 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
2709 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
2710 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
2711 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
2712 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
2713 else:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2714 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
2715 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
2716 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
2717 else:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2718 stretchfactor = self._stretchfactor
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2719 if outline is None:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2720 outline = self._outlinewidth
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2721 self.pen(resizemode="user",
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2722 stretchfactor=stretchfactor, outline=outline)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2723
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2724 def settiltangle(self, angle):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2725 """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
2726
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2727 Optional argument:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2728 angle -- number
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2729
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2730 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
2731 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
2732 heading (direction of movement).
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2733
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2734
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2735 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
2736 >>> turtle.shape("circle")
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2737 >>> turtle.shapesize(5,2)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2738 >>> turtle.settiltangle(45)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2739 >>> stamp()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2740 >>> turtle.fd(50)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2741 >>> turtle.settiltangle(-45)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2742 >>> stamp()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2743 >>> 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
2744 """
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2745 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
2746 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
2747 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
2748
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2749 def tiltangle(self):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2750 """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
2751
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2752 No argument.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2753
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2754 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
2755 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
2756 (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
2757
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2758 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
2759 >>> turtle.shape("circle")
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2760 >>> turtle.shapesize(5,2)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2761 >>> turtle.tilt(45)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2762 >>> turtle.tiltangle()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2763 >>>
38204
fa3c4ea99d95 Patch #1486962: Several bugs in the turtle Tk demo module were fixed
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
2764 """
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2765 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
2766 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
2767
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2768 def tilt(self, angle):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2769 """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
2770
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2771 Argument:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2772 angle - a number
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 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
2775 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
2776
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2777 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
2778 >>> turtle.shape("circle")
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2779 >>> turtle.shapesize(5,2)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2780 >>> turtle.tilt(30)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2781 >>> turtle.fd(50)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2782 >>> turtle.tilt(30)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2783 >>> turtle.fd(50)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2784 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2785 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
2786
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2787 def _polytrafo(self, poly):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2788 """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
2789 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
2790 """
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2791 screen = self.screen
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2792 p0, p1 = self._position
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2793 e0, e1 = self._orient
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2794 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
2795 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
2796 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
2797 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
2798
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2799 def _drawturtle(self):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2800 """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
2801 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
2802 screen = self.screen
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2803 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
2804 ttype = shape._type
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2805 titem = self.turtle._item
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2806 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
2807 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
2808 tshape = shape._data
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2809 if ttype == "polygon":
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2810 if self._resizemode == "noresize":
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2811 w = 1
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2812 shape = tshape
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2813 else:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2814 if self._resizemode == "auto":
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2815 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
2816 w = self._pensize
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2817 tiltangle = 0
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2818 elif self._resizemode == "user":
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2819 lx, ly = self._stretchfactor
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2820 w = self._outlinewidth
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2821 tiltangle = self._tilt
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2822 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
2823 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
2824 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
2825 shape = self._polytrafo(shape)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2826 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
2827 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
2828 width=w, top=True)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2829 elif ttype == "image":
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2830 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
2831 elif ttype == "compound":
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2832 lx, ly = self._stretchfactor
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2833 w = self._outlinewidth
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2834 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
2835 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
2836 poly = self._polytrafo(poly)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2837 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
2838 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
2839 else:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2840 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
2841 return
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2842 if ttype == "polygon":
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2843 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
2844 elif ttype == "image":
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2845 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
2846 screen._shapes["blank"]._data)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2847 elif ttype == "compound":
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2848 for item in titem:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2849 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
2850 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
2851
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2852 ############################## stamp stuff ###############################
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 def stamp(self):
51761
6ce96ce4d9af Merged revisions 69816 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 49758
diff changeset
2855 """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
2856
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2857 No argument.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2858
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2859 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
2860 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
2861 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
2862
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2863 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
2864 >>> turtle.color("blue")
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2865 >>> turtle.stamp()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2866 13
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2867 >>> 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
2868 """
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2869 screen = self.screen
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2870 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
2871 ttype = shape._type
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2872 tshape = shape._data
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2873 if ttype == "polygon":
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2874 stitem = screen._createpoly()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2875 if self._resizemode == "noresize":
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2876 w = 1
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2877 shape = tshape
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2878 else:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2879 if self._resizemode == "auto":
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2880 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
2881 w = self._pensize
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2882 tiltangle = 0
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2883 elif self._resizemode == "user":
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2884 lx, ly = self._stretchfactor
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2885 w = self._outlinewidth
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2886 tiltangle = self._tilt
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2887 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
2888 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
2889 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
2890 shape = self._polytrafo(shape)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2891 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
2892 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
2893 width=w, top=True)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2894 elif ttype == "image":
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2895 stitem = screen._createimage("")
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2896 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
2897 elif ttype == "compound":
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2898 stitem = []
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2899 for element in tshape:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2900 item = screen._createpoly()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2901 stitem.append(item)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2902 stitem = tuple(stitem)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2903 lx, ly = self._stretchfactor
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2904 w = self._outlinewidth
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2905 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
2906 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
2907 poly = self._polytrafo(poly)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2908 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
2909 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
2910 self.stampItems.append(stitem)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2911 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
2912 return stitem
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 def _clearstamp(self, stampid):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2915 """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
2916 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2917 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
2918 if isinstance(stampid, tuple):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2919 for subitem in stampid:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2920 self.screen._delete(subitem)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2921 else:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2922 self.screen._delete(stampid)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2923 self.stampItems.remove(stampid)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2924 # 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
2925 # 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
2926 item = ("stamp", stampid)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2927 buf = self.undobuffer
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2928 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
2929 return
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2930 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
2931 buf.buffer.remove(item)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2932 if index <= buf.ptr:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2933 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
2934 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
2935
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2936 def clearstamp(self, stampid):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2937 """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
2938
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2939 Argument:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2940 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
2941
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2942 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
2943 >>> turtle.color("blue")
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2944 >>> astamp = turtle.stamp()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2945 >>> turtle.fd(50)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2946 >>> 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
2947 """
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2948 self._clearstamp(stampid)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2949 self._update()
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 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
2952 """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
2953
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2954 Optional argument:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2955 n -- an integer
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2956
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2957 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
2958 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
2959 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
2960
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2961 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
2962 >>> for i in range(8):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2963 turtle.stamp(); turtle.fd(30)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2964 ...
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2965 >>> turtle.clearstamps(2)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2966 >>> turtle.clearstamps(-2)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2967 >>> turtle.clearstamps()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2968 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2969 if n is None:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2970 toDelete = self.stampItems[:]
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2971 elif n >= 0:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2972 toDelete = self.stampItems[:n]
9585
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
2973 else:
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2974 toDelete = self.stampItems[n:]
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2975 for item in toDelete:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2976 self._clearstamp(item)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2977 self._update()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2978
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2979 def _goto(self, end):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2980 """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
2981 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
2982 on this one.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2983 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2984 ## Version mit undo-stuff
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2985 go_modes = ( self._drawing,
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2986 self._pencolor,
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2987 self._pensize,
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2988 isinstance(self._fillpath, list))
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2989 screen = self.screen
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2990 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
2991 (self.currentLineItem,
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2992 self.currentLine[:],
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2993 screen._pointlist(self.currentLineItem),
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2994 self.items[:])
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2995 )
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2996 if self.undobuffer:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2997 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
2998 start = self._position
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
2999 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
3000 diff = (end-start)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3001 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
3002 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
3003 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
3004 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
3005 if n == 1:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3006 top = True
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3007 else:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3008 top = False
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3009 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
3010 if self._drawing:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3011 screen._drawline(self.drawingLineItem,
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3012 (start, self._position),
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3013 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
3014 self._update()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3015 if self._drawing:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3016 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
3017 fill="", width=self._pensize)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3018 # Turtle now at end,
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3019 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
3020 self.currentLine.append(end)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3021 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
3022 self._fillpath.append(end)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3023 ###### vererbung!!!!!!!!!!!!!!!!!!!!!!
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3024 self._position = end
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3025 if self._creatingPoly:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3026 self._poly.append(end)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3027 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
3028 # 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
3029 self._newLine()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3030 self._update() #count=True)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3031
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3032 def _undogoto(self, entry):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3033 """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
3034 """
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3035 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
3036 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
3037 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
3038 screen = self.screen
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3039 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
3040 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
3041 # restore former situation
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3042 self.currentLineItem = cLI
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3043 self.currentLine = cL
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3044
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3045 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
3046 usepc = ""
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3047 else:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3048 usepc = pc
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3049 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
3050
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3051 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
3052 (screen._type(i) == "line")]
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3053 for i in todelete:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3054 screen._delete(i)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3055 self.items.remove(i)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3056
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3057 start = old
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3058 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
3059 diff = old - new
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3060 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
3061 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
3062 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
3063 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
3064 if n == 1:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3065 top = True
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3066 else:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3067 top = False
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3068 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
3069 if drawing:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3070 screen._drawline(self.drawingLineItem,
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3071 (start, self._position),
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3072 pc, ps, top)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3073 self._update()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3074 if drawing:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3075 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
3076 fill="", width=ps)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3077 # 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
3078 self._position = old
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3079 ## if undo is done during crating a polygon, the last vertex
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3080 ## will be deleted. if the polygon is entirel deleted,
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3081 ## creatigPoly will be set to False.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3082 ## 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
3083 if self._creatingPoly:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3084 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
3085 self._poly.pop()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3086 if self._poly == []:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3087 self._creatingPoly = False
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3088 self._poly = None
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3089 if filling:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3090 if self._fillpath == []:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3091 self._fillpath = None
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3092 print "Unwahrscheinlich in _undogoto!"
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3093 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
3094 self._fillpath.pop()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3095 self._update() #count=True)
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 def _rotate(self, angle):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3098 """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
3099 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3100 if self.undobuffer:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3101 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
3102 angle *= self._degreesPerAU
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3103 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
3104 tracing = self.screen._tracing
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3105 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
3106 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
3107 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
3108 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
3109 for _ in range(steps):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3110 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
3111 self._update()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3112 self._orient = neworient
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3113 self._update()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3114
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3115 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
3116 """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
3117 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
3118 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
3119 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3120 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
3121 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
3122 self._pencolor, self._pensize)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3123 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
3124 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
3125 else:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3126 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
3127 self.currentLine = []
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3128 if usePos:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3129 self.currentLine = [self._position]
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 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
3132 """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
3133
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3134 Optional argument:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3135 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
3136
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3137 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
3138 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
3139 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
3140 False else)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3141
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3142 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
3143 >>> 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
3144 >>> 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
3145 >>> 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
3146 >>> 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
3147 >>> 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
3148 >>> 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
3149 >>> 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
3150 >>> turtle.forward(100)
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3151 >>> 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
3152 """
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3153 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
3154 if flag is None:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3155 return filling
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3156 screen = self.screen
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3157 entry1 = entry2 = ()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3158 if filling:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3159 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
3160 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
3161 fill=self._fillcolor)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3162 entry1 = ("dofill", self._fillitem)
9585
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
3163 if flag:
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3164 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
3165 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
3166 self._fillpath = [self._position]
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3167 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
3168 self._newLine()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3169 else:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3170 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
3171 if self.undobuffer:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3172 if entry1 == ():
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(entry2)
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 if entry2 == ():
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3177 self.undobuffer.push(entry1)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3178 else:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3179 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
3180 self._update()
9585
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
3181
38204
fa3c4ea99d95 Patch #1486962: Several bugs in the turtle Tk demo module were fixed
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
3182 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
3183 """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
3184
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3185 No argument.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3186
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3187 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
3188 >>> 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
3189 >>> 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
3190 >>> 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
3191 >>> 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
3192 >>> 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
3193 >>> 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
3194 >>> 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
3195 >>> 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
3196 >>> 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
3197 """
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3198 self.fill(True)
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 def end_fill(self):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3201 """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
3202
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3203 No argument.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3204
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3205 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
3206 >>> turtle.begin_fill()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3207 >>> turtle.forward(100)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3208 >>> turtle.left(90)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3209 >>> turtle.forward(100)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3210 >>> turtle.left(90)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3211 >>> turtle.forward(100)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3212 >>> turtle.left(90)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3213 >>> turtle.forward(100)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3214 >>> turtle.end_fill()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3215 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3216 self.fill(False)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3217
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3218 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
3219 """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
3220
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3221 Optional argumentS:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3222 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
3223 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
3224
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3225 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
3226 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
3227
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3228 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
3229 >>> turtle.dot()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3230 >>> 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
3231 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3232 #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
3233 if not color:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3234 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
3235 color = self._colorstr(size)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3236 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
3237 else:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3238 color = self._pencolor
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3239 if not size:
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 else:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3242 if size is None:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3243 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
3244 color = self._colorstr(color)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3245 #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
3246 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
3247 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
3248 #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
3249 self.items.append(item)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3250 if self.undobuffer:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3251 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
3252 else:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3253 pen = self.pen()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3254 if self.undobuffer:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3255 self.undobuffer.push(["seq"])
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3256 self.undobuffer.cumulate = True
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3257 try:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3258 if self.resizemode() == 'auto':
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3259 self.ht()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3260 self.pendown()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3261 self.pensize(size)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3262 self.pencolor(color)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3263 self.forward(0)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3264 finally:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3265 self.pen(pen)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3266 if self.undobuffer:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3267 self.undobuffer.cumulate = False
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3268
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3269 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
3270 """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
3271 """
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3272 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
3273 self._pencolor)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3274 self.items.append(item)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3275 if self.undobuffer:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3276 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
3277 return end
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 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
3280 """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
3281
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3282 Arguments:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3283 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
3284 move (optional) -- True/False
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3285 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
3286 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
3287
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3288 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
3289 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
3290 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
3291 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
3292 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
3293
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3294 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
3295 >>> 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
3296 >>> 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
3297 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3298 if self.undobuffer:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3299 self.undobuffer.push(["seq"])
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3300 self.undobuffer.cumulate = True
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3301 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
3302 if move:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3303 x, y = self.pos()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3304 self.setpos(end, y)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3305 if self.undobuffer:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3306 self.undobuffer.cumulate = False
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 def begin_poly(self):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3309 """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
3310
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3311 No argument.
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 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
3314 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
3315
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3316 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
3317 >>> turtle.begin_poly()
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 self._poly = [self._position]
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3320 self._creatingPoly = True
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 def end_poly(self):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3323 """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
3324
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3325 No argument.
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 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
3328 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
3329
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3330 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
3331 >>> turtle.end_poly()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3332 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3333 self._creatingPoly = False
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 def get_poly(self):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3336 """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
3337
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3338 No argument.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3339
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3340 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
3341 >>> p = turtle.get_poly()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3342 >>> 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
3343 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3344 ## 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
3345 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
3346 return tuple(self._poly)
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 def getscreen(self):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3349 """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
3350
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3351 No argument.
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 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
3354 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
3355
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3356 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
3357 >>> ts = turtle.getscreen()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3358 >>> ts
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3359 <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
3360 >>> ts.bgcolor("pink")
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3361 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3362 return self.screen
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 def getturtle(self):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3365 """Return the Turtleobject itself.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3366
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3367 No argument.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3368
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3369 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
3370
fa3c4ea99d95 Patch #1486962: Several bugs in the turtle Tk demo module were fixed
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
3371 Example:
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3372 >>> pet = getturtle()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3373 >>> pet.fd(50)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3374 >>> pet
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3375 <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
3376 >>> turtles()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3377 [<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
3378 """
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3379 return self
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 getpen = getturtle
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3382
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3383
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3384 ################################################################
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3385 ### 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
3386 ################################################################
25360
5255b45fddc2 Add the bulk of SF patch 595111 by Attila Babo.
Guido van Rossum <guido@python.org>
parents: 25340
diff changeset
3387
5255b45fddc2 Add the bulk of SF patch 595111 by Attila Babo.
Guido van Rossum <guido@python.org>
parents: 25340
diff changeset
3388 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
3389 """ 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
3390
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3391 No argument.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3392
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3393 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
3394 >>> 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
3395 640
fa3c4ea99d95 Patch #1486962: Several bugs in the turtle Tk demo module were fixed
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
3396 """
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3397 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
3398
5255b45fddc2 Add the bulk of SF patch 595111 by Attila Babo.
Guido van Rossum <guido@python.org>
parents: 25340
diff changeset
3399 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
3400 """ 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
3401
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3402 No argument.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3403
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3404 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
3405 >>> screen.window_height()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3406 480
38204
fa3c4ea99d95 Patch #1486962: Several bugs in the turtle Tk demo module were fixed
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
3407 """
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3408 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
3409
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3410 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
3411 """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
3412 """
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3413 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
3414
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3415 ##### event binding methods #####
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 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
3418 """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
3419
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3420 Arguments:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3421 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
3422 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
3423 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
3424 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
3425 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
3426
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3427 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
3428
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3429 >>> def turn(x, y):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3430 left(360)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3431
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3432 >>> onclick(turn) # Now clicking into the turtle will turn it.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3433 >>> 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
3434 """
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3435 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
3436 self._update()
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 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
3439 """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
3440
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3441 Arguments:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3442 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
3443 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
3444 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
3445
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3446 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
3447 >>> class MyTurtle(Turtle):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3448 def glow(self,x,y):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3449 self.fillcolor("red")
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3450 def unglow(self,x,y):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3451 self.fillcolor("")
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3452
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3453 >>> joe = MyTurtle()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3454 >>> joe.onclick(joe.glow)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3455 >>> joe.onrelease(joe.unglow)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3456 ### clicking on joe turns fillcolor red,
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3457 ### unclicking turns it to transparent.
38204
fa3c4ea99d95 Patch #1486962: Several bugs in the turtle Tk demo module were fixed
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
3458 """
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3459 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
3460 self._update()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3461
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3462 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
3463 """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
3464
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3465 Arguments:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3466 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
3467 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
3468 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
3469
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3470 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
3471 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
3472
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3473 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
3474 >>> turtle.ondrag(turtle.goto)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3475
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3476 ### Subsequently clicking and dragging a Turtle will
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3477 ### move it across the screen thereby producing handdrawings
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3478 ### (if pen is down).
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 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
3481
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3482
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3483 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
3484 """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
3485 """
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3486 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
3487 return
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3488 if action == "rot":
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3489 angle, degPAU = data
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3490 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
3491 dummy = self.undobuffer.pop()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3492 elif action == "stamp":
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3493 stitem = data[0]
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3494 self.clearstamp(stitem)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3495 elif action == "go":
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3496 self._undogoto(data)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3497 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
3498 item = data[0]
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3499 self.screen._delete(item)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3500 self.items.remove(item)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3501 elif action == "dofill":
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3502 item = data[0]
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3503 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
3504 fill="", outline="")
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3505 elif action == "beginfill":
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3506 item = data[0]
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3507 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
3508 self.screen._delete(item)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3509 self.items.remove(item)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3510 elif action == "pen":
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3511 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
3512 self.undobuffer.pop()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3513
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3514 def undo(self):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3515 """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
3516
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3517 No argument.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3518
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3519 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
3520 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
3521 the undobuffer.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3522
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3523 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
3524 >>> for i in range(4):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3525 turtle.fd(50); turtle.lt(80)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3526
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3527 >>> for i in range(8):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3528 turtle.undo()
38204
fa3c4ea99d95 Patch #1486962: Several bugs in the turtle Tk demo module were fixed
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
3529 """
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3530 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
3531 return
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3532 item = self.undobuffer.pop()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3533 action = item[0]
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3534 data = item[1:]
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3535 if action == "seq":
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3536 while data:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3537 item = data.pop()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3538 self._undo(item[0], item[1:])
9585
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
3539 else:
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3540 self._undo(action, data)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3541
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3542 turtlesize = shapesize
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3543
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3544 RawPen = RawTurtle
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3545
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
3546 ### 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
3547
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 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
3549 """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
3550 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
3551 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
3552 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
3553 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
3554 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
3555
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
3556 class _Screen(TurtleScreen):
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3557
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3558 _root = None
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3559 _canvas = None
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3560 _title = _CFG["title"]
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3561
9585
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
3562 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
3563 # 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
3564 # 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
3565 # 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
3566 # 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
3567 # 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
3568 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
3569 _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
3570 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
3571 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
3572 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
3573 width = _CFG["width"]
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3574 height = _CFG["height"]
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3575 canvwidth = _CFG["canvwidth"]
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3576 canvheight = _CFG["canvheight"]
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3577 leftright = _CFG["leftright"]
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3578 topbottom = _CFG["topbottom"]
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3579 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
3580 _Screen._canvas = self._root._getcanvas()
56267
56b530029bac Merged revisions 75115 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 55049
diff changeset
3581 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
3582 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
3583
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3584 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
3585 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
3586 """ 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
3587
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3588 Arguments:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3589 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
3590 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
3591 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
3592 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
3593 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
3594 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
3595 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
3596 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
3597 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
3598 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
3599
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3600 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
3601 >>> 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
3602
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3603 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
3604
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3605 >>> 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
3606
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3607 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
3608 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3609 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
3610 return
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3611 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
3612 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
3613 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
3614 width = sw*width
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3615 if startx is None:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3616 startx = (sw - width) / 2
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3617 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
3618 height = sh*height
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3619 if starty is None:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3620 starty = (sh - height) / 2
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3621 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
3622 self.update()
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3623
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3624 def title(self, titlestring):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3625 """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
3626
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3627 Argument:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3628 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
3629 turtle graphics window.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3630
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3631 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
3632 objects.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3633
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3634 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
3635 >>> 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
3636 """
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
3637 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
3638 _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
3639 _Screen._title = titlestring
9585
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
3640
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
3641 def _destroy(self):
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3642 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
3643 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
3644 Turtle._pen = None
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3645 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
3646 _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
3647 _Screen._canvas = None
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3648 TurtleScreen._RUNNING = True
9585
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
3649 root.destroy()
15308
847ae40074d4 Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents: 14957
diff changeset
3650
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3651 def bye(self):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3652 """Shut the turtlegraphics window.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3653
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3654 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
3655 >>> screen.bye()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3656 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3657 self._destroy()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3658
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3659 def exitonclick(self):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3660 """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
3661
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3662 No arguments.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3663
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3664 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
3665 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
3666 (default value), enter mainloop.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3667 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
3668 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
3669 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
3670
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3671 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
3672 TurtleScreen instances.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3673
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3674 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
3675 >>> screen.exitonclick()
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 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3678 def exitGracefully(x, y):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3679 """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
3680 self.bye()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3681 self.onclick(exitGracefully)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3682 if _CFG["using_IDLE"]:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3683 return
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3684 try:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3685 mainloop()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3686 except AttributeError:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3687 exit(0)
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
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3690 class Turtle(RawTurtle):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3691 """RawTurtle auto-crating (scrolled) canvas.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3692
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3693 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
3694 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
3695 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3696 _pen = None
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3697 _screen = None
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3698
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3699 def __init__(self,
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3700 shape=_CFG["shape"],
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3701 undobuffersize=_CFG["undobuffersize"],
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3702 visible=_CFG["visible"]):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3703 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
3704 Turtle._screen = Screen()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3705 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
3706 shape=shape,
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3707 undobuffersize=undobuffersize,
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3708 visible=visible)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3709
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3710 Pen = Turtle
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3711
9585
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
3712 def _getpen():
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3713 """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
3714 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
3715 Turtle._pen = Turtle()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3716 return Turtle._pen
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3717
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3718 def _getscreen():
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3719 """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
3720 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
3721 Turtle._screen = Screen()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3722 return Turtle._screen
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3723
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3724 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
3725 """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
3726
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3727 Optional argument:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3728 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
3729 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
3730
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3731 Has to be called explicitely, (not used by the turtle-graphics classes)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3732 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
3733 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
3734 into different languages.
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3735 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3736 docsdict = {}
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3737
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3738 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
3739 key = "_Screen."+methodname
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3740 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
3741 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
3742 key = "Turtle."+methodname
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3743 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
3744
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3745 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
3746 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
3747 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
3748 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
3749 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
3750 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
3751 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
3752 key = keys[-1]
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' % repr(key))
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3754 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
3755 f.write("}\n")
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3756 f.close()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3757
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3758 def read_docstrings(lang):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3759 """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
3760
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3761 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
3762 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
3763 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
3764 """
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3765 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
3766 module = __import__(modname)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3767 docsdict = module.docsdict
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3768 for key in docsdict:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3769 #print key
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3770 try:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3771 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
3772 except:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3773 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
3774
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3775 _LANGUAGE = _CFG["language"]
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3776
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3777 try:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3778 if _LANGUAGE != "english":
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3779 read_docstrings(_LANGUAGE)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3780 except ImportError:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3781 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
3782 except:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3783 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
3784 _LANGUAGE)
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
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3787 def getmethparlist(ob):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3788 "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
3789 argText1 = argText2 = ""
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3790 # 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
3791 # 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
3792 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
3793 fob = ob.im_func
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3794 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
3795 else:
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3796 fob = ob
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3797 argOffset = 0
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3798 # 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
3799 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
3800 try:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3801 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
3802 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
3803 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
3804 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
3805 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
3806 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
3807 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
3808 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
3809 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
3810 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
3811 counter += 1
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3812 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
3813 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
3814 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
3815 argText1 = ", ".join(items1)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3816 argText1 = "(%s)" % argText1
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3817 argText2 = ", ".join(items2)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3818 argText2 = "(%s)" % argText2
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3819 except:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3820 pass
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3821 return argText1, argText2
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3822
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3823 def _turtle_docrevise(docstr):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3824 """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
3825 """
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3826 import re
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3827 if docstr is None:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3828 return None
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3829 turtlename = _CFG["exampleturtle"]
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3830 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
3831 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
3832 newdocstr = parexp.sub(":", newdocstr)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3833 return newdocstr
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3834
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3835 def _screen_docrevise(docstr):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3836 """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
3837 """
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3838 import re
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3839 if docstr is None:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3840 return None
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3841 screenname = _CFG["examplescreen"]
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3842 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
3843 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
3844 newdocstr = parexp.sub(":", newdocstr)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3845 return newdocstr
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3846
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3847 ## 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
3848 ## 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
3849 ## 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
3850
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3851
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3852 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
3853 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
3854 if pl1 == "":
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3855 print ">>>>>>", pl1, pl2
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3856 continue
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3857 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
3858 {'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
3859 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
3860 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
3861
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3862 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
3863 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
3864 if pl1 == "":
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3865 print ">>>>>>", pl1, pl2
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3866 continue
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3867 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
3868 {'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
3869 exec defstr
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3870 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
3871
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3872
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3873 done = mainloop = TK.mainloop
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3874 del pl1, pl2, defstr
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3875
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3876 if __name__ == "__main__":
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3877 def switchpen():
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3878 if isdown():
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3879 pu()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3880 else:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3881 pd()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3882
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3883 def demo1():
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3884 """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
3885 reset()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3886 tracer(True)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3887 up()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3888 backward(100)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3889 down()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3890 # 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
3891 width(3)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3892 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
3893 if i == 2:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3894 fill(1)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3895 for _ in range(4):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3896 forward(20)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3897 left(90)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3898 if i == 2:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3899 color("maroon")
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3900 fill(0)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3901 up()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3902 forward(30)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3903 down()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3904 width(1)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3905 color("black")
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3906 # 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
3907 tracer(False)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3908 up()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3909 right(90)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3910 forward(100)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3911 right(90)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3912 forward(100)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3913 right(180)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3914 down()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3915 # some text
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3916 write("startstart", 1)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3917 write("start", 1)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3918 color("red")
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3919 # staircase
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3920 for i in range(5):
9585
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
3921 forward(20)
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
3922 left(90)
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3923 forward(20)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3924 right(90)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3925 # filled staircase
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3926 tracer(True)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3927 fill(1)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3928 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
3929 forward(20)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3930 left(90)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3931 forward(20)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3932 right(90)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3933 fill(0)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3934 # more text
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3935
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3936 def demo2():
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3937 """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
3938 speed(1)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3939 st()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3940 pensize(3)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3941 setheading(towards(0, 0))
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3942 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
3943 rt(90)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3944 for _ in range(18):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3945 switchpen()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3946 circle(radius, 10)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3947 write("wait a moment...")
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3948 while undobufferentries():
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3949 undo()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3950 reset()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3951 lt(90)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3952 colormode(255)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3953 laenge = 10
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3954 pencolor("green")
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3955 pensize(3)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3956 lt(180)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3957 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
3958 if i > 0:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3959 begin_fill()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3960 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
3961 for _ in range(3):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3962 fd(laenge)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3963 lt(120)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3964 laenge += 10
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3965 lt(15)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3966 speed((speed()+1)%12)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3967 end_fill()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3968
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3969 lt(120)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3970 pu()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3971 fd(70)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3972 rt(30)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3973 pd()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3974 color("red","yellow")
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3975 speed(0)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3976 fill(1)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3977 for _ in range(4):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3978 circle(50, 90)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3979 rt(90)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3980 fd(30)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3981 rt(90)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3982 fill(0)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3983 lt(90)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3984 pu()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3985 fd(30)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3986 pd()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3987 shape("turtle")
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3988
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3989 tri = getturtle()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3990 tri.resizemode("auto")
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3991 turtle = Turtle()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3992 turtle.resizemode("auto")
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3993 turtle.shape("turtle")
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3994 turtle.reset()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3995 turtle.left(90)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3996 turtle.speed(0)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3997 turtle.up()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3998 turtle.goto(280, 40)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
3999 turtle.lt(30)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
4000 turtle.down()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
4001 turtle.speed(6)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
4002 turtle.color("blue","orange")
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
4003 turtle.pensize(2)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
4004 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
4005 setheading(towards(turtle))
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
4006 count = 1
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
4007 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
4008 turtle.fd(3.5)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
4009 turtle.lt(0.6)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
4010 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
4011 tri.fd(4)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
4012 if count % 20 == 0:
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
4013 turtle.stamp()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
4014 tri.stamp()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
4015 switchpen()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
4016 count += 1
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
4017 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
4018 tri.pencolor("black")
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
4019 tri.pencolor("red")
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
4020
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
4021 def baba(xdummy, ydummy):
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
4022 clearscreen()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
4023 bye()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
4024
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
4025 time.sleep(2)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
4026
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
4027 while undobufferentries():
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
4028 tri.undo()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
4029 turtle.undo()
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
4030 tri.fd(50)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
4031 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
4032 tri.onclick(baba, 1)
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
4033
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
4034 demo1()
38204
fa3c4ea99d95 Patch #1486962: Several bugs in the turtle Tk demo module were fixed
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
4035 demo2()
47459
ef0cccde8c24 Patch #1513695: New turtle module, with demos.
Martin v. Löwis <martin@v.loewis.de>
parents: 47183
diff changeset
4036 exitonclick()