annotate Lib/lib-tk/turtle.py @ 54294:dba767bc3155 legacy-trunk

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