turtle.py revision 0fc61ccbba42dcebdf97a981e39d8522b24d60bc
197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis# 297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis# turtle.py: a Tkinter based turtle graphics module for Python 397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis# Version 1.0b1 - 31. 5. 2008 497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis# 597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis# Copyright (C) 2006 - 2008 Gregor Lingl 697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis# email: glingl@aon.at 797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis# 897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis# This software is provided 'as-is', without any express or implied 997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis# warranty. In no event will the authors be held liable for any damages 1097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis# arising from the use of this software. 1197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis# 1297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis# Permission is granted to anyone to use this software for any purpose, 1397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis# including commercial applications, and to alter it and redistribute it 1497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis# freely, subject to the following restrictions: 1597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis# 1697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis# 1. The origin of this software must not be misrepresented; you must not 1797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis# claim that you wrote the original software. If you use this software 1897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis# in a product, an acknowledgment in the product documentation would be 1997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis# appreciated but is not required. 2097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis# 2. Altered source versions must be plainly marked as such, and must not be 2197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis# misrepresented as being the original software. 2297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis# 3. This notice may not be removed or altered from any source distribution. 2397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 24b241b67b8954b0679377af00d668e3dc92f4c858Guido van Rossum 25477c8d5e70240744d24631b18341ad892c8a8e1cThomas Wouters""" 26477c8d5e70240744d24631b18341ad892c8a8e1cThomas WoutersTurtle graphics is a popular way for introducing programming to 27477c8d5e70240744d24631b18341ad892c8a8e1cThomas Wouterskids. It was part of the original Logo programming language developed 2897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwisby Wally Feurzig and Seymour Papert in 1966. 29477c8d5e70240744d24631b18341ad892c8a8e1cThomas Wouters 30477c8d5e70240744d24631b18341ad892c8a8e1cThomas WoutersImagine a robotic turtle starting at (0, 0) in the x-y plane. Give it 31477c8d5e70240744d24631b18341ad892c8a8e1cThomas Woutersthe command turtle.forward(15), and it moves (on-screen!) 15 pixels in 32477c8d5e70240744d24631b18341ad892c8a8e1cThomas Woutersthe direction it is facing, drawing a line as it moves. Give it the 33477c8d5e70240744d24631b18341ad892c8a8e1cThomas Wouterscommand turtle.left(25), and it rotates in-place 25 degrees clockwise. 34477c8d5e70240744d24631b18341ad892c8a8e1cThomas Wouters 35477c8d5e70240744d24631b18341ad892c8a8e1cThomas WoutersBy combining together these and similar commands, intricate shapes and 36477c8d5e70240744d24631b18341ad892c8a8e1cThomas Wouterspictures can easily be drawn. 3797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 3897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis----- turtle.py 3997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 4097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. LöwisThis module is an extended reimplementation of turtle.py from the 4197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. LöwisPython standard distribution up to Python 2.5. (See: http:\\www.python.org) 4297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 4397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. LöwisIt tries to keep the merits of turtle.py and to be (nearly) 100% 4497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwiscompatible with it. This means in the first place to enable the 4597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwislearning programmer to use all the commands, classes and methods 4697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwisinteractively when using the module from within IDLE run with 4797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwisthe -n switch. 4897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 4997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. LöwisRoughly it has the following features added: 5097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 5197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis- Better animation of the turtle movements, especially of turning the 5297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis turtle. So the turtles can more easily be used as a visual feedback 5397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis instrument by the (beginning) programmer. 5497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 5597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis- Different turtle shapes, gif-images as turtle shapes, user defined 5697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis and user controllable turtle shapes, among them compound 5797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis (multicolored) shapes. Turtle shapes can be stgretched and tilted, which 580fc61ccbba42dcebdf97a981e39d8522b24d60bcMark Dickinson makes turtles very versatile geometrical objects. 5997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 6097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis- Fine control over turtle movement and screen updates via delay(), 6197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis and enhanced tracer() and speed() methods. 6297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 6397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis- Aliases for the most commonly used commands, like fd for forward etc., 6497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis following the early Logo traditions. This reduces the boring work of 6597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis typing long sequences of commands, which often occur in a natural way 6697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis when kids try to program fancy pictures on their first encounter with 6797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis turtle graphcis. 6897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 6997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis- Turtles now have an undo()-method with configurable undo-buffer. 7097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 7197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis- Some simple commands/methods for creating event driven programs 7297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis (mouse-, key-, timer-events). Especially useful for programming games. 7397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 7497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis- A scrollable Canvas class. The default scrollable Canvas can be 7597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis extended interactively as needed while playing around with the turtle(s). 7697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 7797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis- A TurtleScreen class with methods controlling background color or 7897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis background image, window and canvas size and other properties of the 7997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis TurtleScreen. 8097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 8197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis- There is a method, setworldcoordinates(), to install a user defined 8297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis coordinate-system for the TurtleScreen. 8397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 8497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis- The implementation uses a 2-vector class named Vec2D, derived from tuple. 8597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis This class is public, so it can be imported by the application programmer, 8697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis which makes certain types of computations very natural and compact. 8797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 8897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis- Appearance of the TurtleScreen and the Turtles at startup/import can be 8997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis configured by means of a turtle.cfg configuration file. 9097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis The default configuration mimics the appearance of the old turtle module. 9197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 9297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis- If configured appropriately the module reads in docstrings from a docstring 9397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis dictionary in some different language, supplied separately and replaces 9497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis the english ones by those read in. There is a utility function 9597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis write_docstringdict() to write a dictionary with the original (english) 9697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis docstrings to disc, so it can serve as a template for translations. 9797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 9897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. LöwisBehind the scenes there are some features included with possible 9997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwisextensionsin in mind. These will be commented and documented elsewhere. 10097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 101477c8d5e70240744d24631b18341ad892c8a8e1cThomas Wouters""" 102477c8d5e70240744d24631b18341ad892c8a8e1cThomas Wouters 10397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis_ver = "turtle 1.0b1- - for Python 3.0 - 9. 6. 2008, 01:15" 10497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 10597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis# print(_ver) 10697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 10797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwisimport tkinter as TK 10897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwisimport types 10997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwisimport math 11097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwisimport time 11197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwisimport os 11297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 11397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwisfrom os.path import isfile, split, join 11497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwisfrom copy import deepcopy 11597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 11697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis#from math import * ## for compatibility with old turtle module 11797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 11897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis_tg_classes = ['ScrolledCanvas', 'TurtleScreen', 'Screen', 11997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 'RawTurtle', 'Turtle', 'RawPen', 'Pen', 'Shape', 'Vec2D'] 12097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis_tg_screen_functions = ['addshape', 'bgcolor', 'bgpic', 'bye', 12197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 'clearscreen', 'colormode', 'delay', 'exitonclick', 'getcanvas', 12297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 'getshapes', 'listen', 'mode', 'onkey', 'onscreenclick', 'ontimer', 12397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 'register_shape', 'resetscreen', 'screensize', 'setup', 12497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 'setworldcoordinates', 'title', 'tracer', 'turtles', 'update', 12597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 'window_height', 'window_width'] 12697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis_tg_turtle_functions = ['back', 'backward', 'begin_fill', 'begin_poly', 'bk', 12797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 'circle', 'clear', 'clearstamp', 'clearstamps', 'clone', 'color', 12897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 'degrees', 'distance', 'dot', 'down', 'end_fill', 'end_poly', 'fd', 12997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis #'fill', 13097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 'fillcolor', 'forward', 'get_poly', 'getpen', 'getscreen', 13197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 'getturtle', 'goto', 'heading', 'hideturtle', 'home', 'ht', 'isdown', 13297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 'isvisible', 'left', 'lt', 'onclick', 'ondrag', 'onrelease', 'pd', 13397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 'pen', 'pencolor', 'pendown', 'pensize', 'penup', 'pos', 'position', 13497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 'pu', 'radians', 'right', 'reset', 'resizemode', 'rt', 13597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 'seth', 'setheading', 'setpos', 'setposition', 'settiltangle', 13697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 'setundobuffer', 'setx', 'sety', 'shape', 'shapesize', 'showturtle', 13797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 'speed', 'st', 'stamp', 'tilt', 'tiltangle', 'towards', #'tracer', 13897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 'turtlesize', 'undo', 'undobufferentries', 'up', 'width', 13997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis #'window_height', 'window_width', 14097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 'write', 'xcor', 'ycor'] 14197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis_tg_utilities = ['write_docstringdict', 'done', 'mainloop'] 14297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis##_math_functions = ['acos', 'asin', 'atan', 'atan2', 'ceil', 'cos', 'cosh', 14397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis## 'e', 'exp', 'fabs', 'floor', 'fmod', 'frexp', 'hypot', 'ldexp', 'log', 14497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis## 'log10', 'modf', 'pi', 'pow', 'sin', 'sinh', 'sqrt', 'tan', 'tanh'] 14597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 14697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis__all__ = (_tg_classes + _tg_screen_functions + _tg_turtle_functions + 14797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis _tg_utilities) # + _math_functions) 14897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 14997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis_alias_list = ['addshape', 'backward', 'bk', 'fd', 'ht', 'lt', 'pd', 'pos', 15097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 'pu', 'rt', 'seth', 'setpos', 'setposition', 'st', 15197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 'turtlesize', 'up', 'width'] 15297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 15397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis_CFG = {"width" : 0.5, # Screen 15497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis "height" : 0.75, 15597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis "canvwidth" : 400, 15697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis "canvheight": 300, 15797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis "leftright": None, 15897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis "topbottom": None, 15997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis "mode": "standard", # TurtleScreen 16097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis "colormode": 1.0, 16197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis "delay": 10, 16297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis "undobuffersize": 1000, # RawTurtle 16397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis "shape": "classic", 16497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis "pencolor" : "black", 16597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis "fillcolor" : "black", 16697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis "resizemode" : "noresize", 16797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis "visible" : True, 16897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis "language": "english", # docstrings 16997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis "exampleturtle": "turtle", 17097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis "examplescreen": "screen", 17197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis "title": "Python Turtle Graphics", 17297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis "using_IDLE": False 17397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis } 17497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 17597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis##print "cwd:", os.getcwd() 17697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis##print "__file__:", __file__ 17797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis## 17897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis##def show(dictionary): 17997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis## print "==========================" 18097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis## for key in sorted(dictionary.keys()): 18197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis## print key, ":", dictionary[key] 18297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis## print "==========================" 18397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis## print 18497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 18597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwisdef config_dict(filename): 18697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """Convert content of config-file into dictionary.""" 18797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis f = open(filename, "r") 18897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis cfglines = f.readlines() 18997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis f.close() 19097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis cfgdict = {} 19197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis for line in cfglines: 19297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis line = line.strip() 19397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if not line or line.startswith("#"): 19497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis continue 19597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis try: 19697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis key, value = line.split("=") 19797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis except: 19897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis print("Bad line in config-file %s:\n%s" % (filename,line)) 19997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis continue 20097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis key = key.strip() 20197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis value = value.strip() 20297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if value in ["True", "False", "None", "''", '""']: 20397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis value = eval(value) 20497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis else: 20597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis try: 20697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if "." in value: 20797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis value = float(value) 20897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis else: 20997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis value = int(value) 21097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis except: 21197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis pass # value need not be converted 21297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis cfgdict[key] = value 21397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis return cfgdict 21497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 21597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwisdef readconfig(cfgdict): 21697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """Read config-files, change configuration-dict accordingly. 21797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 21897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis If there is a turtle.cfg file in the current working directory, 21997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis read it from there. If this contains an importconfig-value, 22097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis say 'myway', construct filename turtle_mayway.cfg else use 22197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis turtle.cfg and read it from the import-directory, where 22297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis turtle.py is located. 22397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Update configuration dictionary first according to config-file, 22497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis in the import directory, then according to config-file in the 22597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis current working directory. 22697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis If no config-file is found, the default configuration is used. 22797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ 22897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis default_cfg = "turtle.cfg" 22997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis cfgdict1 = {} 23097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis cfgdict2 = {} 23197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if isfile(default_cfg): 23297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis cfgdict1 = config_dict(default_cfg) 23397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis #print "1. Loading config-file %s from: %s" % (default_cfg, os.getcwd()) 23497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if "importconfig" in cfgdict1: 23597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis default_cfg = "turtle_%s.cfg" % cfgdict1["importconfig"] 23697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis try: 23797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis head, tail = split(__file__) 23897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis cfg_file2 = join(head, default_cfg) 23997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis except: 24097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis cfg_file2 = "" 24197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if isfile(cfg_file2): 24297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis #print "2. Loading config-file %s:" % cfg_file2 24397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis cfgdict2 = config_dict(cfg_file2) 24497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis## show(_CFG) 24597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis## show(cfgdict2) 24697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis _CFG.update(cfgdict2) 24797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis## show(_CFG) 24897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis## show(cfgdict1) 24997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis _CFG.update(cfgdict1) 25097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis## show(_CFG) 25197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 25297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwistry: 25397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis readconfig(_CFG) 25497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwisexcept: 25597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis print ("No configfile read, reason unknown") 25697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 25797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 25897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwisclass Vec2D(tuple): 25997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """A 2 dimensional vector class, used as a helper class 26097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis for implementing turtle graphics. 26197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis May be useful for turtle graphics programs also. 26297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Derived from tuple, so a vector is a tuple! 26397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 26497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Provides (for a, b vectors, k number): 26597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis a+b vector addition 26697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis a-b vector subtraction 26797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis a*b inner product 26897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis k*a and a*k multiplication with scalar 26997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis |a| absolute value of a 27097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis a.rotate(angle) rotation 27197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ 27297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def __new__(cls, x, y): 27397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis return tuple.__new__(cls, (x, y)) 27497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def __add__(self, other): 27597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis return Vec2D(self[0]+other[0], self[1]+other[1]) 27697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def __mul__(self, other): 27797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if isinstance(other, Vec2D): 27897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis return self[0]*other[0]+self[1]*other[1] 27997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis return Vec2D(self[0]*other, self[1]*other) 28097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def __rmul__(self, other): 28197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if isinstance(other, int) or isinstance(other, float): 28297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis return Vec2D(self[0]*other, self[1]*other) 28397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def __sub__(self, other): 28497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis return Vec2D(self[0]-other[0], self[1]-other[1]) 28597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def __neg__(self): 28697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis return Vec2D(-self[0], -self[1]) 28797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def __abs__(self): 28897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis return (self[0]**2 + self[1]**2)**0.5 28997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def rotate(self, angle): 29097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """rotate self counterclockwise by angle 29197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ 29297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis perp = Vec2D(-self[1], self[0]) 29397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis angle = angle * math.pi / 180.0 29497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis c, s = math.cos(angle), math.sin(angle) 29597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis return Vec2D(self[0]*c+perp[0]*s, self[1]*c+perp[1]*s) 29697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def __getnewargs__(self): 29797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis return (self[0], self[1]) 29897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def __repr__(self): 29997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis return "(%.2f,%.2f)" % self 30097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 30197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 30297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis############################################################################## 30397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis### From here up to line : Tkinter - Interface for turtle.py ### 30497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis### May be replaced by an interface to some different graphcis-toolkit ### 30597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis############################################################################## 30697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 30797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis## helper functions for Scrolled Canvas, to forward Canvas-methods 30897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis## to ScrolledCanvas class 30997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 31097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwisdef __methodDict(cls, _dict): 31197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """helper function for Scrolled Canvas""" 31297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis baseList = list(cls.__bases__) 31397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis baseList.reverse() 31497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis for _super in baseList: 31597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis __methodDict(_super, _dict) 31697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis for key, value in cls.__dict__.items(): 31797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if type(value) == types.FunctionType: 31897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis _dict[key] = value 31997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 32097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwisdef __methods(cls): 32197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """helper function for Scrolled Canvas""" 32297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis _dict = {} 32397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis __methodDict(cls, _dict) 32497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis return _dict.keys() 32597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 32697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis__stringBody = ( 32797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 'def %(method)s(self, *args, **kw): return ' + 32897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 'self.%(attribute)s.%(method)s(*args, **kw)') 32997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 33097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwisdef __forwardmethods(fromClass, toClass, toPart, exclude = ()): 33197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis ### MANY CHANGES ### 33297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis _dict_1 = {} 33397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis __methodDict(toClass, _dict_1) 33497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis _dict = {} 33597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis mfc = __methods(fromClass) 33697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis for ex in _dict_1.keys(): 33797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if ex[:1] == '_' or ex[-1:] == '_' or ex in exclude or ex in mfc: 33897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis pass 33997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis else: 34097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis _dict[ex] = _dict_1[ex] 34197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 34297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis for method, func in _dict.items(): 34397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis d = {'method': method, 'func': func} 34497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if isinstance(toPart, str): 34597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis execString = \ 34697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis __stringBody % {'method' : method, 'attribute' : toPart} 34797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis exec(execString, d) 34897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis setattr(fromClass, method, d[method]) ### NEWU! 34997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 35097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 35197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwisclass ScrolledCanvas(TK.Frame): 35297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """Modeled after the scrolled canvas class from Grayons's Tkinter book. 35397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 35497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Used as the default canvas, which pops up automatically when 35597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis using turtle graphics functions or the Turtle class. 35697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ 35797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def __init__(self, master, width=500, height=350, 35897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis canvwidth=600, canvheight=500): 35997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis TK.Frame.__init__(self, master, width=width, height=height) 36022d297be51567eb193e792a0da52b93e9a45b9a4Martin v. Löwis self._rootwindow = self.winfo_toplevel() 36197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.width, self.height = width, height 36297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.canvwidth, self.canvheight = canvwidth, canvheight 36397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.bg = "white" 36497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._canvas = TK.Canvas(master, width=width, height=height, 36597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis bg=self.bg, relief=TK.SUNKEN, borderwidth=2) 36697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.hscroll = TK.Scrollbar(master, command=self._canvas.xview, 36797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis orient=TK.HORIZONTAL) 36897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.vscroll = TK.Scrollbar(master, command=self._canvas.yview) 36997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._canvas.configure(xscrollcommand=self.hscroll.set, 37097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis yscrollcommand=self.vscroll.set) 37197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.rowconfigure(0, weight=1, minsize=0) 37297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.columnconfigure(0, weight=1, minsize=0) 37397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._canvas.grid(padx=1, in_ = self, pady=1, row=0, 37497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis column=0, rowspan=1, columnspan=1, sticky='news') 37597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.vscroll.grid(padx=1, in_ = self, pady=1, row=0, 37697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis column=1, rowspan=1, columnspan=1, sticky='news') 37797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.hscroll.grid(padx=1, in_ = self, pady=1, row=1, 37897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis column=0, rowspan=1, columnspan=1, sticky='news') 37997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.reset() 38022d297be51567eb193e792a0da52b93e9a45b9a4Martin v. Löwis self._rootwindow.bind('<Configure>', self.onResize) 38197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 38297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def reset(self, canvwidth=None, canvheight=None, bg = None): 38397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """Ajust canvas and scrollbars according to given canvas size.""" 38497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if canvwidth: 38597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.canvwidth = canvwidth 38697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if canvheight: 38797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.canvheight = canvheight 38897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if bg: 38997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.bg = bg 39097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._canvas.config(bg=bg, 39197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis scrollregion=(-self.canvwidth//2, -self.canvheight//2, 39297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.canvwidth//2, self.canvheight//2)) 39397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._canvas.xview_moveto(0.5*(self.canvwidth - self.width + 30) / 39497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.canvwidth) 39597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._canvas.yview_moveto(0.5*(self.canvheight- self.height + 30) / 39697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.canvheight) 39797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.adjustScrolls() 39897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 39997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 40097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def adjustScrolls(self): 40197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ Adjust scrollbars according to window- and canvas-size. 40297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ 40397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis cwidth = self._canvas.winfo_width() 40497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis cheight = self._canvas.winfo_height() 40597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._canvas.xview_moveto(0.5*(self.canvwidth-cwidth)/self.canvwidth) 40697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._canvas.yview_moveto(0.5*(self.canvheight-cheight)/self.canvheight) 40797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if cwidth < self.canvwidth or cheight < self.canvheight: 40897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.hscroll.grid(padx=1, in_ = self, pady=1, row=1, 40997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis column=0, rowspan=1, columnspan=1, sticky='news') 41097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.vscroll.grid(padx=1, in_ = self, pady=1, row=0, 41197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis column=1, rowspan=1, columnspan=1, sticky='news') 41297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis else: 41397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.hscroll.grid_forget() 41497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.vscroll.grid_forget() 41597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 41697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def onResize(self, event): 41797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """self-explanatory""" 41897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.adjustScrolls() 41997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 42097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def bbox(self, *args): 42197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ 'forward' method, which canvas itself has inherited... 42297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ 42397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis return self._canvas.bbox(*args) 42497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 42597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def cget(self, *args, **kwargs): 42697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ 'forward' method, which canvas itself has inherited... 42797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ 42897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis return self._canvas.cget(*args, **kwargs) 42997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 43097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def config(self, *args, **kwargs): 43197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ 'forward' method, which canvas itself has inherited... 43297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ 43397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._canvas.config(*args, **kwargs) 43497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 43597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def bind(self, *args, **kwargs): 43697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ 'forward' method, which canvas itself has inherited... 43797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ 43897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._canvas.bind(*args, **kwargs) 43997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 44097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def unbind(self, *args, **kwargs): 44197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ 'forward' method, which canvas itself has inherited... 44297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ 44397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._canvas.unbind(*args, **kwargs) 44497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 44597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def focus_force(self): 44697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ 'forward' method, which canvas itself has inherited... 44797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ 44897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._canvas.focus_force() 44997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 45097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis__forwardmethods(ScrolledCanvas, TK.Canvas, '_canvas') 45197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 45297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 45397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwisclass _Root(TK.Tk): 45497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """Root class for Screen based on Tkinter.""" 45597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def __init__(self): 45697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis TK.Tk.__init__(self) 45797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 45897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def setupcanvas(self, width, height, cwidth, cheight): 45997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._canvas = ScrolledCanvas(self, width, height, cwidth, cheight) 46097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._canvas.pack(expand=1, fill="both") 46197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 46297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def _getcanvas(self): 46397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis return self._canvas 464fd2ede2aa84e77dac30e297a04fc49dd3bb61c68Guido van Rossum 46597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def set_geometry(self, width, height, startx, starty): 46697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.geometry("%dx%d%+d%+d"%(width, height, startx, starty)) 467477c8d5e70240744d24631b18341ad892c8a8e1cThomas Wouters 46897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def ondestroy(self, destroy): 46997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.wm_protocol("WM_DELETE_WINDOW", destroy) 47097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 47197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def win_width(self): 47297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis return self.winfo_screenwidth() 47397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 47497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def win_height(self): 47597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis return self.winfo_screenheight() 47697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 47797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. LöwisCanvas = TK.Canvas 47897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 47997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 48097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwisclass TurtleScreenBase(object): 48197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """Provide the basic graphics functionality. 48297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Interface between Tkinter and turtle.py. 48397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 48497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis To port turtle.py to some different graphics toolkit 48597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis a corresponding TurtleScreenBase class has to be implemented. 48697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ 48797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 48897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis @staticmethod 48997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def _blankimage(): 49097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """return a blank image object 49197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ 49297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis img = TK.PhotoImage(width=1, height=1) 49397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis img.blank() 49497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis return img 49597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 49697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis @staticmethod 49797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def _image(filename): 49897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """return an image object containing the 49997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis imagedata from a gif-file named filename. 50097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ 50197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis return TK.PhotoImage(file=filename) 50297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 50397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def __init__(self, cv): 50497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.cv = cv 50597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if isinstance(cv, ScrolledCanvas): 50697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis w = self.cv.canvwidth 50797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis h = self.cv.canvheight 50897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis else: # expected: ordinary TK.Canvas 50997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis w = int(self.cv.cget("width")) 51097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis h = int(self.cv.cget("height")) 51197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.cv.config(scrollregion = (-w//2, -h//2, w//2, h//2 )) 51297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.canvwidth = w 51397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.canvheight = h 51497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.xscale = self.yscale = 1.0 51597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 51697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def _createpoly(self): 51797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """Create an invisible polygon item on canvas self.cv) 51897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ 51997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis return self.cv.create_polygon((0, 0, 0, 0, 0, 0), fill="", outline="") 52097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 52197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def _drawpoly(self, polyitem, coordlist, fill=None, 52297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis outline=None, width=None, top=False): 52397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """Configure polygonitem polyitem according to provided 52497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis arguments: 52597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis coordlist is sequence of coordinates 52697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis fill is filling color 52797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis outline is outline color 52897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis top is a boolean value, which specifies if polyitem 52997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis will be put on top of the canvas' displaylist so it 53097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis will not be covered by other items. 53197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ 53297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis cl = [] 53397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis for x, y in coordlist: 53497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis cl.append(x * self.xscale) 53597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis cl.append(-y * self.yscale) 53697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.cv.coords(polyitem, *cl) 53797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if fill is not None: 53897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.cv.itemconfigure(polyitem, fill=fill) 53997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if outline is not None: 54097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.cv.itemconfigure(polyitem, outline=outline) 54197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if width is not None: 54297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.cv.itemconfigure(polyitem, width=width) 54397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if top: 54497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.cv.tag_raise(polyitem) 54597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 54697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def _createline(self): 54797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """Create an invisible line item on canvas self.cv) 54897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ 54997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis return self.cv.create_line(0, 0, 0, 0, fill="", width=2, 55097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis capstyle = TK.ROUND) 55197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 55297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def _drawline(self, lineitem, coordlist=None, 55397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis fill=None, width=None, top=False): 55497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """Configure lineitem according to provided arguments: 55597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis coordlist is sequence of coordinates 55697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis fill is drawing color 55797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis width is width of drawn line. 55897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis top is a boolean value, which specifies if polyitem 55997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis will be put on top of the canvas' displaylist so it 56097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis will not be covered by other items. 56197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ 56297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if coordlist is not None: 56397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis cl = [] 56497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis for x, y in coordlist: 56597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis cl.append(x * self.xscale) 56697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis cl.append(-y * self.yscale) 56797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.cv.coords(lineitem, *cl) 56897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if fill is not None: 56997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.cv.itemconfigure(lineitem, fill=fill) 57097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if width is not None: 57197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.cv.itemconfigure(lineitem, width=width) 57297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if top: 57397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.cv.tag_raise(lineitem) 57497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 57597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def _delete(self, item): 57697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """Delete graphics item from canvas. 57797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis If item is"all" delete all graphics items. 57897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ 57997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.cv.delete(item) 58097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 58197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def _update(self): 58297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """Redraw graphics items on canvas 58397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ 58497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.cv.update() 58597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 58697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def _delay(self, delay): 58797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """Delay subsequent canvas actions for delay ms.""" 58897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.cv.after(delay) 58997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 59097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def _iscolorstring(self, color): 59197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """Check if the string color is a legal Tkinter color string. 59297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ 59397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis try: 59497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis rgb = self.cv.winfo_rgb(color) 59597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis ok = True 59697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis except TK.TclError: 59797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis ok = False 59897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis return ok 59997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 60097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def _bgcolor(self, color=None): 60197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """Set canvas' backgroundcolor if color is not None, 60297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis else return backgroundcolor.""" 60397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if color is not None: 60497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.cv.config(bg = color) 60597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._update() 60697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis else: 60797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis return self.cv.cget("bg") 60897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 60997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def _write(self, pos, txt, align, font, pencolor): 61097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """Write txt at pos in canvas with specified font 61197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis and color. 61297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Return text item and x-coord of right bottom corner 61397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis of text's bounding box.""" 61497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis x, y = pos 61597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis x = x * self.xscale 61697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis y = y * self.yscale 61797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis anchor = {"left":"sw", "center":"s", "right":"se" } 61897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis item = self.cv.create_text(x-1, -y, text = txt, anchor = anchor[align], 61997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis fill = pencolor, font = font) 62097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis x0, y0, x1, y1 = self.cv.bbox(item) 62197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.cv.update() 62297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis return item, x1-1 62397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 62497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis## def _dot(self, pos, size, color): 62597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis## """may be implemented for some other graphics toolkit""" 62697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 62797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def _onclick(self, item, fun, num=1, add=None): 62897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """Bind fun to mouse-click event on turtle. 62997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis fun must be a function with two arguments, the coordinates 63097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis of the clicked point on the canvas. 63197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis num, the number of the mouse-button defaults to 1 63297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ 63397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if fun is None: 63497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.cv.tag_unbind(item, "<Button-%s>" % num) 63597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis else: 63697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def eventfun(event): 63797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis x, y = (self.cv.canvasx(event.x)/self.xscale, 63897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis -self.cv.canvasy(event.y)/self.yscale) 63997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis fun(x, y) 64097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.cv.tag_bind(item, "<Button-%s>" % num, eventfun, add) 64197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 64297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def _onrelease(self, item, fun, num=1, add=None): 64397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """Bind fun to mouse-button-release event on turtle. 64497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis fun must be a function with two arguments, the coordinates 64597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis of the point on the canvas where mouse button is released. 64697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis num, the number of the mouse-button defaults to 1 64797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 64897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis If a turtle is clicked, first _onclick-event will be performed, 64997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis then _onscreensclick-event. 65097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ 65197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if fun is None: 65297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.cv.tag_unbind(item, "<Button%s-ButtonRelease>" % num) 65397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis else: 65497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def eventfun(event): 65597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis x, y = (self.cv.canvasx(event.x)/self.xscale, 65697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis -self.cv.canvasy(event.y)/self.yscale) 65797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis fun(x, y) 65897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.cv.tag_bind(item, "<Button%s-ButtonRelease>" % num, 65997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis eventfun, add) 66097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 66197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def _ondrag(self, item, fun, num=1, add=None): 66297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """Bind fun to mouse-move-event (with pressed mouse button) on turtle. 66397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis fun must be a function with two arguments, the coordinates of the 66497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis actual mouse position on the canvas. 66597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis num, the number of the mouse-button defaults to 1 66697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 66797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Every sequence of mouse-move-events on a turtle is preceded by a 66897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis mouse-click event on that turtle. 66997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ 67097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if fun is None: 67197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.cv.tag_unbind(item, "<Button%s-Motion>" % num) 67297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis else: 67397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def eventfun(event): 67497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis try: 67597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis x, y = (self.cv.canvasx(event.x)/self.xscale, 67697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis -self.cv.canvasy(event.y)/self.yscale) 67797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis fun(x, y) 67897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis except: 67997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis pass 68097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.cv.tag_bind(item, "<Button%s-Motion>" % num, eventfun, add) 68197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 68297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def _onscreenclick(self, fun, num=1, add=None): 68397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """Bind fun to mouse-click event on canvas. 68497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis fun must be a function with two arguments, the coordinates 68597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis of the clicked point on the canvas. 68697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis num, the number of the mouse-button defaults to 1 68797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 68897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis If a turtle is clicked, first _onclick-event will be performed, 68997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis then _onscreensclick-event. 69097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ 69197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if fun is None: 69297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.cv.unbind("<Button-%s>" % num) 69397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis else: 69497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def eventfun(event): 69597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis x, y = (self.cv.canvasx(event.x)/self.xscale, 69697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis -self.cv.canvasy(event.y)/self.yscale) 69797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis fun(x, y) 69897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.cv.bind("<Button-%s>" % num, eventfun, add) 69997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 70097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def _onkey(self, fun, key): 70197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """Bind fun to key-release event of key. 70297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Canvas must have focus. See method listen 70397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ 70497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if fun is None: 70597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.cv.unbind("<KeyRelease-%s>" % key, None) 70697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis else: 70797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def eventfun(event): 70897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis fun() 70997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.cv.bind("<KeyRelease-%s>" % key, eventfun) 71097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 71197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def _listen(self): 71297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """Set focus on canvas (in order to collect key-events) 71397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ 71497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.cv.focus_force() 71597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 71697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def _ontimer(self, fun, t): 71797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """Install a timer, which calls fun after t milliseconds. 71897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ 71997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if t == 0: 72097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.cv.after_idle(fun) 72197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis else: 72297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.cv.after(t, fun) 72397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 72497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def _createimage(self, image): 72597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """Create and return image item on canvas. 72697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ 72797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis return self.cv.create_image(0, 0, image=image) 72897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 72997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def _drawimage(self, item, pos, image): 73097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """Configure image item as to draw image object 73197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis at position (x,y) on canvas) 73297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ 73397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis x, y = pos 73497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.cv.coords(item, (x * self.xscale, -y * self.yscale)) 73597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.cv.itemconfig(item, image=image) 73697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 73797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def _setbgpic(self, item, image): 73897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """Configure image item as to draw image object 73997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis at center of canvas. Set item to the first item 74097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis in the displaylist, so it will be drawn below 74197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis any other item .""" 74297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.cv.itemconfig(item, image=image) 74397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.cv.tag_lower(item) 74497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 74597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def _type(self, item): 74697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """Return 'line' or 'polygon' or 'image' depending on 74797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis type of item. 74897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ 74997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis return self.cv.type(item) 75097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 75197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def _pointlist(self, item): 75297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """returns list of coordinate-pairs of points of item 75397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Example (for insiders): 75497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> from turtle import * 75597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> getscreen()._pointlist(getturtle().turtle._item) 75697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis [(0.0, 9.9999999999999982), (0.0, -9.9999999999999982), 75797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis (9.9999999999999982, 0.0)] 75897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> """ 75997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis cl = list(self.cv.coords(item)) 76097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis pl = [(cl[i], -cl[i+1]) for i in range(0, len(cl), 2)] 76197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis return pl 76297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 76397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def _setscrollregion(self, srx1, sry1, srx2, sry2): 76497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.cv.config(scrollregion=(srx1, sry1, srx2, sry2)) 76597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 76697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def _rescale(self, xscalefactor, yscalefactor): 76797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis items = self.cv.find_all() 76897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis for item in items: 76997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis coordinates = list(self.cv.coords(item)) 77097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis newcoordlist = [] 77197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis while coordinates: 77297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis x, y = coordinates[:2] 77397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis newcoordlist.append(x * xscalefactor) 77497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis newcoordlist.append(y * yscalefactor) 77597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis coordinates = coordinates[2:] 77697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.cv.coords(item, *newcoordlist) 77797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 77897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def _resize(self, canvwidth=None, canvheight=None, bg=None): 77997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """Resize the canvas, the turtles are drawing on. Does 78097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis not alter the drawing window. 78197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ 78297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis # needs amendment 78397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if not isinstance(self.cv, ScrolledCanvas): 78497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis return self.canvwidth, self.canvheight 78597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if canvwidth is None and canvheight is None and bg is None: 78697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis return self.cv.canvwidth, self.cv.canvheight 78797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if canvwidth is not None: 78897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.canvwidth = canvwidth 78997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if canvheight is not None: 79097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.canvheight = canvheight 79197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.cv.reset(canvwidth, canvheight, bg) 79297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 79397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def _window_size(self): 79497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ Return the width and height of the turtle window. 79597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ 79697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis width = self.cv.winfo_width() 79797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if width <= 1: # the window isn't managed by a geometry manager 79897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis width = self.cv['width'] 79997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis height = self.cv.winfo_height() 80097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if height <= 1: # the window isn't managed by a geometry manager 80197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis height = self.cv['height'] 80297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis return width, height 80397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 80497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 80597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis############################################################################## 80697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis### End of Tkinter - interface ### 80797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis############################################################################## 80897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 80997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 81097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwisclass Terminator (Exception): 81197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """Will be raised in TurtleScreen.update, if _RUNNING becomes False. 81297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 81397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Thus stops execution of turtle graphics script. Main purpose: use in 81497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis in the Demo-Viewer turtle.Demo.py. 81597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ 8164b6ea798cbb6ef6058d97794c652e4acfd2ac165Martin v. Löwis pass 817b241b67b8954b0679377af00d668e3dc92f4c858Guido van Rossum 818b241b67b8954b0679377af00d668e3dc92f4c858Guido van Rossum 81997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwisclass TurtleGraphicsError(Exception): 82097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """Some TurtleGraphics Error 82197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ 82297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 82397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 82497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwisclass Shape(object): 82597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """Data structure modeling shapes. 82697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 82797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis attribute _type is one of "polygon", "image", "compound" 82897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis attribute _data is - depending on _type a poygon-tuple, 82997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis an image or a list constructed using the addcomponent method. 83097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ 83197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def __init__(self, type_, data=None): 83297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._type = type_ 83397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if type_ == "polygon": 83497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if isinstance(data, list): 83597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis data = tuple(data) 83697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis elif type_ == "image": 83797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if isinstance(data, str): 83897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if data.lower().endswith(".gif") and isfile(data): 83997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis data = TurtleScreen._image(data) 84097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis # else data assumed to be Photoimage 84197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis elif type_ == "compound": 84297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis data = [] 84397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis else: 84497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis raise TurtleGraphicsError("There is no shape type %s" % type_) 84597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._data = data 84697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 84797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def addcomponent(self, poly, fill, outline=None): 84897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """Add component to a shape of type compound. 84997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 85097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Arguments: poly is a polygon, i. e. a tuple of number pairs. 85197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis fill is the fillcolor of the component, 85297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis outline is the outline color of the component. 85397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 85497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis call (for a Shapeobject namend s): 85597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis -- s.addcomponent(((0,0), (10,10), (-10,10)), "red", "blue") 85697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 85797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Example: 85897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> poly = ((0,0),(10,-5),(0,10),(-10,-5)) 85997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> s = Shape("compound") 86097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> s.addcomponent(poly, "red", "blue") 86197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis ### .. add more components and then use register_shape() 86297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ 86397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if self._type != "compound": 86497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis raise TurtleGraphicsError("Cannot add component to %s Shape" 86597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis % self._type) 86697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if outline is None: 86797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis outline = fill 86897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._data.append([poly, fill, outline]) 86997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 87097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 87197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwisclass Tbuffer(object): 87297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """Ring buffer used as undobuffer for RawTurtle objects.""" 87397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def __init__(self, bufsize=10): 87497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.bufsize = bufsize 87597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.buffer = [[None]] * bufsize 87697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.ptr = -1 87797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.cumulate = False 87897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def reset(self, bufsize=None): 87997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if bufsize is None: 88097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis for i in range(self.bufsize): 88197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.buffer[i] = [None] 88297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis else: 88397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.bufsize = bufsize 88497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.buffer = [[None]] * bufsize 88597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.ptr = -1 88697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def push(self, item): 88797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if self.bufsize > 0: 88897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if not self.cumulate: 88997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.ptr = (self.ptr + 1) % self.bufsize 89097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.buffer[self.ptr] = item 89197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis else: 89297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.buffer[self.ptr].append(item) 89397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def pop(self): 89497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if self.bufsize > 0: 89597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis item = self.buffer[self.ptr] 89697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if item is None: 89797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis return None 89897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis else: 89997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.buffer[self.ptr] = [None] 90097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.ptr = (self.ptr - 1) % self.bufsize 90197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis return (item) 90297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def nr_of_items(self): 90397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis return self.bufsize - self.buffer.count([None]) 90497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def __repr__(self): 90597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis return str(self.buffer) + " " + str(self.ptr) 90697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 90797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 90897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 90997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwisclass TurtleScreen(TurtleScreenBase): 91097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """Provides screen oriented methods like setbg etc. 91197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 91297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Only relies upon the methods of TurtleScreenBase and NOT 91397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis upon components of the underlying graphics toolkit - 91497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis which is Tkinter in this case. 91597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ 91697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis# _STANDARD_DELAY = 5 91797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis _RUNNING = True 91897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 91997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def __init__(self, cv, mode=_CFG["mode"], 92097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis colormode=_CFG["colormode"], delay=_CFG["delay"]): 92197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._shapes = { 92297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis "arrow" : Shape("polygon", ((-10,0), (10,0), (0,10))), 92397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis "turtle" : Shape("polygon", ((0,16), (-2,14), (-1,10), (-4,7), 92497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis (-7,9), (-9,8), (-6,5), (-7,1), (-5,-3), (-8,-6), 92597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis (-6,-8), (-4,-5), (0,-7), (4,-5), (6,-8), (8,-6), 92697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis (5,-3), (7,1), (6,5), (9,8), (7,9), (4,7), (1,10), 92797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis (2,14))), 92897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis "circle" : Shape("polygon", ((10,0), (9.51,3.09), (8.09,5.88), 92997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis (5.88,8.09), (3.09,9.51), (0,10), (-3.09,9.51), 93097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis (-5.88,8.09), (-8.09,5.88), (-9.51,3.09), (-10,0), 93197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis (-9.51,-3.09), (-8.09,-5.88), (-5.88,-8.09), 93297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis (-3.09,-9.51), (-0.00,-10.00), (3.09,-9.51), 93397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis (5.88,-8.09), (8.09,-5.88), (9.51,-3.09))), 93497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis "square" : Shape("polygon", ((10,-10), (10,10), (-10,10), 93597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis (-10,-10))), 93697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis "triangle" : Shape("polygon", ((10,-5.77), (0,11.55), 93797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis (-10,-5.77))), 93897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis "classic": Shape("polygon", ((0,0),(-5,-9),(0,-7),(5,-9))), 93997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis "blank" : Shape("image", self._blankimage()) 94097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis } 94197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 94297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._bgpics = {"nopic" : ""} 94397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 94497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis TurtleScreenBase.__init__(self, cv) 94597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._mode = mode 94697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._delayvalue = delay 94797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._colormode = _CFG["colormode"] 94897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._keys = [] 94997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.clear() 95097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 95197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def clear(self): 95297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """Delete all drawings and all turtles from the TurtleScreen. 95397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 95497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Reset empty TurtleScreen to it's initial state: white background, 95597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis no backgroundimage, no eventbindings and tracing on. 95697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 95797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis No argument. 95897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 95997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Example (for a TurtleScreen instance named screen): 96097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis screen.clear() 96197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 96297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Note: this method is not available as function. 96397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ 96497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._delayvalue = _CFG["delay"] 96597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._colormode = _CFG["colormode"] 96697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._delete("all") 96797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._bgpic = self._createimage("") 96897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._bgpicname = "nopic" 969b241b67b8954b0679377af00d668e3dc92f4c858Guido van Rossum self._tracing = 1 97097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._updatecounter = 0 97197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._turtles = [] 97297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.bgcolor("white") 97397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis for btn in 1, 2, 3: 97497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.onclick(None, btn) 97597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis for key in self._keys[:]: 97697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.onkey(None, key) 97797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Turtle._pen = None 97897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 97997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def mode(self, mode=None): 98097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """Set turtle-mode ('standard', 'logo' or 'world') and perform reset. 98197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 98297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Optional argument: 98397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis mode -- on of the strings 'standard', 'logo' or 'world' 98497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 98597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Mode 'standard' is compatible with turtle.py. 98697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Mode 'logo' is compatible with most Logo-Turtle-Graphics. 98797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Mode 'world' uses userdefined 'worldcoordinates'. *Attention*: in 98897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis this mode angles appear distorted if x/y unit-ratio doesn't equal 1. 98997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis If mode is not given, return the current mode. 99097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 99197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Mode Initial turtle heading positive angles 99297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis ------------|-------------------------|------------------- 99397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 'standard' to the right (east) counterclockwise 99497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 'logo' upward (north) clockwise 99597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 99697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Examples: 99797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> mode('logo') # resets turtle heading to north 99897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> mode() 99997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 'logo' 100097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ 100197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if mode == None: 100297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis return self._mode 100397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis mode = mode.lower() 100497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if mode not in ["standard", "logo", "world"]: 100597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis raise TurtleGraphicsError("No turtle-graphics-mode %s" % mode) 100697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._mode = mode 100797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if mode in ["standard", "logo"]: 100897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._setscrollregion(-self.canvwidth//2, -self.canvheight//2, 100997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.canvwidth//2, self.canvheight//2) 101097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.xscale = self.yscale = 1.0 1011b241b67b8954b0679377af00d668e3dc92f4c858Guido van Rossum self.reset() 1012b241b67b8954b0679377af00d668e3dc92f4c858Guido van Rossum 101397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def setworldcoordinates(self, llx, lly, urx, ury): 101497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """Set up a user defined coordinate-system. 101597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 101697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Arguments: 101797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis llx -- a number, x-coordinate of lower left corner of canvas 101897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis lly -- a number, y-coordinate of lower left corner of canvas 101997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis urx -- a number, x-coordinate of upper right corner of canvas 102097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis ury -- a number, y-coordinate of upper right corner of canvas 102197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 102297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Set up user coodinat-system and switch to mode 'world' if necessary. 102397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis This performs a screen.reset. If mode 'world' is already active, 102497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis all drawings are redrawn according to the new coordinates. 102597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 102697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis But ATTENTION: in user-defined coordinatesystems angles may appear 102797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis distorted. (see Screen.mode()) 102897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 102997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Example (for a TurtleScreen instance named screen): 103097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> screen.setworldcoordinates(-10,-0.5,50,1.5) 103197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> for _ in range(36): 103297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis left(10) 103397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis forward(0.5) 103497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ 103597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if self.mode() != "world": 103697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.mode("world") 103797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis xspan = float(urx - llx) 103897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis yspan = float(ury - lly) 103997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis wx, wy = self._window_size() 104097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.screensize(wx-20, wy-20) 104197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis oldxscale, oldyscale = self.xscale, self.yscale 104297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.xscale = self.canvwidth / xspan 104397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.yscale = self.canvheight / yspan 104497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis srx1 = llx * self.xscale 104597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis sry1 = -ury * self.yscale 104697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis srx2 = self.canvwidth + srx1 104797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis sry2 = self.canvheight + sry1 104897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._setscrollregion(srx1, sry1, srx2, sry2) 104997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._rescale(self.xscale/oldxscale, self.yscale/oldyscale) 105097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.update() 105197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 105297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def register_shape(self, name, shape=None): 105397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """Adds a turtle shape to TurtleScreen's shapelist. 105497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 105597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Arguments: 105697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis (1) name is the name of a gif-file and shape is None. 105797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Installs the corresponding image shape. 105897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis !! Image-shapes DO NOT rotate when turning the turtle, 105997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis !! so they do not display the heading of the turtle! 106097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis (2) name is an arbitrary string and shape is a tuple 106197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis of pairs of coordinates. Installs the corresponding 106297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis polygon shape 106397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis (3) name is an arbitrary string and shape is a 106497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis (compound) Shape object. Installs the corresponding 106597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis compound shape. 106697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis To use a shape, you have to issue the command shape(shapename). 106797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 106897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis call: register_shape("turtle.gif") 106997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis --or: register_shape("tri", ((0,0), (10,10), (-10,10))) 107097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 107197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Example (for a TurtleScreen instance named screen): 107297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> screen.register_shape("triangle", ((5,-3),(0,5),(-5,-3))) 107397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 107497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ 107597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if shape is None: 107697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis # image 107797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if name.lower().endswith(".gif"): 107897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis shape = Shape("image", self._image(name)) 107997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis else: 108097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis raise TurtleGraphicsError("Bad arguments for register_shape.\n" 108197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis + "Use help(register_shape)" ) 108297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis elif isinstance(shape, tuple): 108397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis shape = Shape("polygon", shape) 108497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis ## else shape assumed to be Shape-instance 108597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._shapes[name] = shape 108697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis # print "shape added:" , self._shapes 108797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 108897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def _colorstr(self, color): 108997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """Return color string corresponding to args. 109097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 109197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Argument may be a string or a tuple of three 109297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis numbers corresponding to actual colormode, 109397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis i.e. in the range 0<=n<=colormode. 109497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 109597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis If the argument doesn't represent a color, 109697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis an error is raised. 109797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ 109897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if len(color) == 1: 109997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis color = color[0] 110097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if isinstance(color, str): 110197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if self._iscolorstring(color) or color == "": 110297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis return color 110397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis else: 110497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis raise TurtleGraphicsError("bad color string: %s" % str(color)) 110597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis try: 110697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis r, g, b = color 110797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis except: 110897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis raise TurtleGraphicsError("bad color arguments: %s" % str(color)) 110997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if self._colormode == 1.0: 111097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis r, g, b = [round(255.0*x) for x in (r, g, b)] 111197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if not ((0 <= r <= 255) and (0 <= g <= 255) and (0 <= b <= 255)): 111297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis raise TurtleGraphicsError("bad color sequence: %s" % str(color)) 111397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis return "#%02x%02x%02x" % (r, g, b) 111497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 111597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def _color(self, cstr): 111697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if not cstr.startswith("#"): 111797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis return cstr 111897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if len(cstr) == 7: 111997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis cl = [int(cstr[i:i+2], 16) for i in (1, 3, 5)] 112097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis elif len(cstr) == 4: 112197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis cl = [16*int(cstr[h], 16) for h in cstr[1:]] 112297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis else: 112397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis raise TurtleGraphicsError("bad colorstring: %s" % cstr) 112497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis return tuple([c * self._colormode/255 for c in cl]) 112597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 112697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def colormode(self, cmode=None): 112797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """Return the colormode or set it to 1.0 or 255. 112897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 112997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Optional argument: 113097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis cmode -- one of the values 1.0 or 255 113197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 113297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis r, g, b values of colortriples have to be in range 0..cmode. 113397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 113497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Example (for a TurtleScreen instance named screen): 113597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> screen.colormode() 113697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 1.0 113797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> screen.colormode(255) 113897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> turtle.pencolor(240,160,80) 113997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ 114097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if cmode is None: 114197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis return self._colormode 114297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if cmode == 1.0: 114397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._colormode = float(cmode) 114497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis elif cmode == 255: 114597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._colormode = int(cmode) 114697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 114797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def reset(self): 114897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """Reset all Turtles on the Screen to their initial state. 114997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 115097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis No argument. 115197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 115297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Example (for a TurtleScreen instance named screen): 115397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> screen.reset() 115497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ 115597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis for turtle in self._turtles: 115697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis turtle._setmode(self._mode) 115797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis turtle.reset() 115897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 115997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def turtles(self): 116097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """Return the list of turtles on the screen. 116197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 116297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Example (for a TurtleScreen instance named screen): 116397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> screen.turtles() 116497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis [<turtle.Turtle object at 0x00E11FB0>] 116597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ 116697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis return self._turtles 116797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 116897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def bgcolor(self, *args): 116997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """Set or return backgroundcolor of the TurtleScreen. 117097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 117197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Arguments (if given): a color string or three numbers 117297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis in the range 0..colormode or a 3-tuple of such numbers. 117397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 117497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Example (for a TurtleScreen instance named screen): 117597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> screen.bgcolor("orange") 117697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> screen.bgcolor() 117797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 'orange' 117897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> screen.bgcolor(0.5,0,0.5) 117997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> screen.bgcolor() 118097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis '#800080' 118197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ 118297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if args: 118397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis color = self._colorstr(args) 118497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis else: 118597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis color = None 118697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis color = self._bgcolor(color) 118797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if color is not None: 118897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis color = self._color(color) 118997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis return color 119097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 119197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def tracer(self, n=None, delay=None): 119297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """Turns turtle animation on/off and set delay for update drawings. 119397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 119497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Optional arguments: 119597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis n -- nonnegative integer 119697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis delay -- nonnegative integer 119797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 119897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis If n is given, only each n-th regular screen update is really performed. 119997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis (Can be used to accelerate the drawing of complex graphics.) 120097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Second arguments sets delay value (see RawTurtle.delay()) 120197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 120297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Example (for a TurtleScreen instance named screen): 120397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> screen.tracer(8, 25) 120497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> dist = 2 120597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> for i in range(200): 120697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis fd(dist) 120797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis rt(90) 120897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis dist += 2 120997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ 121097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if n is None: 121197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis return self._tracing 121297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._tracing = int(n) 121397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._updatecounter = 0 121497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if delay is not None: 121597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._delayvalue = int(delay) 121697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if self._tracing: 121797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.update() 121897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 121997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def delay(self, delay=None): 122097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ Return or set the drawing delay in milliseconds. 122197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 122297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Optional argument: 122397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis delay -- positive integer 122497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 122597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Example (for a TurtleScreen instance named screen): 122697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> screen.delay(15) 122797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> screen.delay() 122897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 15 122997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ 123097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if delay is None: 123197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis return self._delayvalue 123297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._delayvalue = int(delay) 123397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 123497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def _incrementudc(self): 123597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis "Increment upadate counter.""" 123697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if not TurtleScreen._RUNNING: 123797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis TurtleScreen._RUNNNING = True 123897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis raise Terminator 123997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if self._tracing > 0: 124097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._updatecounter += 1 124197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._updatecounter %= self._tracing 124297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 124397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def update(self): 124497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """Perform a TurtleScreen update. 124597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ 124697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis for t in self.turtles(): 124797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis t._update_data() 124897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis t._drawturtle() 124997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._update() 125097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 125197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def window_width(self): 125297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ Return the width of the turtle window. 125397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 125497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Example (for a TurtleScreen instance named screen): 125597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> screen.window_width() 125697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 640 125797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ 125897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis return self._window_size()[0] 125997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 126097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def window_height(self): 126197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ Return the height of the turtle window. 126297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 126397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Example (for a TurtleScreen instance named screen): 126497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> screen.window_height() 126597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 480 126697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ 126797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis return self._window_size()[1] 126897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 126997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def getcanvas(self): 127097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """Return the Canvas of this TurtleScreen. 127197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 127297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis No argument. 127397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 127497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Example (for a Screen instance named screen): 127597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> cv = screen.getcanvas() 127697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> cv 127797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis <turtle.ScrolledCanvas instance at 0x010742D8> 127897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ 127997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis return self.cv 128097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 128197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def getshapes(self): 128297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """Return a list of names of all currently available turtle shapes. 128397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 128497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis No argument. 128597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 128697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Example (for a TurtleScreen instance named screen): 128797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> screen.getshapes() 128897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis ['arrow', 'blank', 'circle', ... , 'turtle'] 128997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ 129097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis return sorted(self._shapes.keys()) 129197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 129297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def onclick(self, fun, btn=1, add=None): 129397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """Bind fun to mouse-click event on canvas. 129497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 129597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Arguments: 129697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis fun -- a function with two arguments, the coordinates of the 129797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis clicked point on the canvas. 129897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis num -- the number of the mouse-button, defaults to 1 129997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 130097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Example (for a TurtleScreen instance named screen 130197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis and a Turtle instance named turtle): 130297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 130397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> screen.onclick(turtle.goto) 130497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 130597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis ### Subsequently clicking into the TurtleScreen will 130697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis ### make the turtle move to the clicked point. 130797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> screen.onclick(None) 130897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 130997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis ### event-binding will be removed 131097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ 131197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._onscreenclick(fun, btn, add) 131297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 131397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def onkey(self, fun, key): 131497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """Bind fun to key-release event of key. 131597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 131697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Arguments: 131797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis fun -- a function with no arguments 131897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis key -- a string: key (e.g. "a") or key-symbol (e.g. "space") 131997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 132097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis In order ro be able to register key-events, TurtleScreen 132197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis must have focus. (See method listen.) 132297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 132397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Example (for a TurtleScreen instance named screen 132497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis and a Turtle instance named turtle): 132597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 132697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> def f(): 132797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis fd(50) 132897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis lt(60) 132997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 133097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 133197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> screen.onkey(f, "Up") 133297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> screen.listen() 133397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 133497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis ### Subsequently the turtle can be moved by 133597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis ### repeatedly pressing the up-arrow key, 133697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis ### consequently drawing a hexagon 133797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ 133897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if fun == None: 133997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._keys.remove(key) 134097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis elif key not in self._keys: 134197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._keys.append(key) 134297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._onkey(fun, key) 134397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 134497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def listen(self, xdummy=None, ydummy=None): 134597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """Set focus on TurtleScreen (in order to collect key-events) 134697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 134797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis No arguments. 134897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Dummy arguments are provided in order 134997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis to be able to pass listen to the onclick method. 135097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 135197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Example (for a TurtleScreen instance named screen): 135297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> screen.listen() 135397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ 135497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._listen() 135597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 135697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def ontimer(self, fun, t=0): 135797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """Install a timer, which calls fun after t milliseconds. 135897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 135997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Arguments: 136097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis fun -- a function with no arguments. 136197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis t -- a number >= 0 136297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 136397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Example (for a TurtleScreen instance named screen): 136497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 136597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> running = True 136697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> def f(): 136797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if running: 136897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis fd(50) 136997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis lt(60) 137097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis screen.ontimer(f, 250) 137197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 137297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> f() ### makes the turtle marching around 137397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> running = False 137497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ 137597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._ontimer(fun, t) 137697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 137797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def bgpic(self, picname=None): 137897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """Set background image or return name of current backgroundimage. 137997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 138097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Optional argument: 138197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis picname -- a string, name of a gif-file or "nopic". 138297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 138397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis If picname is a filename, set the corresponing image as background. 138497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis If picname is "nopic", delete backgroundimage, if present. 138597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis If picname is None, return the filename of the current backgroundimage. 138697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 138797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Example (for a TurtleScreen instance named screen): 138897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> screen.bgpic() 138997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 'nopic' 139097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> screen.bgpic("landscape.gif") 139197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> screen.bgpic() 139297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 'landscape.gif' 139397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ 139497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if picname is None: 139597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis return self._bgpicname 139697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if picname not in self._bgpics: 139797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._bgpics[picname] = self._image(picname) 139897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._setbgpic(self._bgpic, self._bgpics[picname]) 139997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._bgpicname = picname 140097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 140197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def screensize(self, canvwidth=None, canvheight=None, bg=None): 140297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """Resize the canvas, the turtles are drawing on. 140397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 140497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Optional arguments: 140597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis canvwidth -- positive integer, new width of canvas in pixels 140697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis canvheight -- positive integer, new height of canvas in pixels 140797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis bg -- colorstring or color-tupel, new backgroundcolor 140897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis If no arguments are given, return current (canvaswidth, canvasheight) 140997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 141097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Do not alter the drawing window. To observe hidden parts of 141197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis the canvas use the scrollbars. (Can make visible those parts 141297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis of a drawing, which were outside the canvas before!) 141397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 141497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Example (for a Turtle instance named turtle): 141597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> turtle.screensize(2000,1500) 141697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis ### e. g. to search for an erroneously escaped turtle ;-) 141797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ 141897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis return self._resize(canvwidth, canvheight, bg) 141997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 142097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis onscreenclick = onclick 142197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis resetscreen = reset 142297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis clearscreen = clear 142397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis addshape = register_shape 142497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 142597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwisclass TNavigator(object): 142697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """Navigation part of the RawTurtle. 142797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Implements methods for turtle movement. 142897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ 142997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis START_ORIENTATION = { 143097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis "standard": Vec2D(1.0, 0.0), 143197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis "world" : Vec2D(1.0, 0.0), 143297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis "logo" : Vec2D(0.0, 1.0) } 143397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis DEFAULT_MODE = "standard" 143497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis DEFAULT_ANGLEOFFSET = 0 143597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis DEFAULT_ANGLEORIENT = 1 143697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 143797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def __init__(self, mode=DEFAULT_MODE): 143897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._angleOffset = self.DEFAULT_ANGLEOFFSET 143997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._angleOrient = self.DEFAULT_ANGLEORIENT 144097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._mode = mode 144197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.undobuffer = None 144297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.degrees() 144397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._mode = None 144497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._setmode(mode) 144597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis TNavigator.reset(self) 144697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 144797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def reset(self): 144897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """reset turtle to its initial values 144997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 145097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Will be overwritten by parent class 145197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ 145297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._position = Vec2D(0.0, 0.0) 145397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._orient = TNavigator.START_ORIENTATION[self._mode] 145497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 145597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def _setmode(self, mode=None): 145697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """Set turtle-mode to 'standard', 'world' or 'logo'. 145797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ 145897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if mode == None: 145997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis return self._mode 146097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if mode not in ["standard", "logo", "world"]: 146197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis return 146297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._mode = mode 146397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if mode in ["standard", "world"]: 146497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._angleOffset = 0 146597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._angleOrient = 1 146697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis else: # mode == "logo": 146797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._angleOffset = self._fullcircle/4. 146897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._angleOrient = -1 146997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 147097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def _setDegreesPerAU(self, fullcircle): 147197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """Helper function for degrees() and radians()""" 147297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._fullcircle = fullcircle 147397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._degreesPerAU = 360/fullcircle 147497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if self._mode == "standard": 147597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._angleOffset = 0 147697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis else: 147797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._angleOffset = fullcircle/4. 147897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 1479b241b67b8954b0679377af00d668e3dc92f4c858Guido van Rossum def degrees(self, fullcircle=360.0): 1480477c8d5e70240744d24631b18341ad892c8a8e1cThomas Wouters """ Set angle measurement units to degrees. 1481477c8d5e70240744d24631b18341ad892c8a8e1cThomas Wouters 148297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Optional argument: 148397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis fullcircle - a number 148497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 148597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Set angle measurement units, i. e. set number 148697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis of 'degrees' for a full circle. Dafault value is 148797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 360 degrees. 148897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 148997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Example (for a Turtle instance named turtle): 149097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> turtle.left(90) 149197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> turtle.heading() 149297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 90 149397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> turtle.degrees(400.0) # angle measurement in gon 149497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> turtle.heading() 149597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 100 149697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 149797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ 149897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._setDegreesPerAU(fullcircle) 149997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 150097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def radians(self): 150197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ Set the angle measurement units to radians. 150297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 150397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis No arguments. 150497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 150597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Example (for a Turtle instance named turtle): 150697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> turtle.heading() 150797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 90 150897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> turtle.radians() 150997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> turtle.heading() 151097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 1.5707963267948966 151197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ 151297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._setDegreesPerAU(2*math.pi) 151397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 151497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def _go(self, distance): 151597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """move turtle forward by specified distance""" 151697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis ende = self._position + self._orient * distance 151797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._goto(ende) 151897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 151997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def _rotate(self, angle): 152097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """Turn turtle counterclockwise by specified angle if angle > 0.""" 152197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis angle *= self._degreesPerAU 152297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._orient = self._orient.rotate(angle) 152397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 152497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def _goto(self, end): 152597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """move turtle to position end.""" 152697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._position = end 152797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 152897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def forward(self, distance): 152997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """Move the turtle forward by the specified distance. 153097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 153197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Aliases: forward | fd 153297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 153397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Argument: 153497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis distance -- a number (integer or float) 153597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 153697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Move the turtle forward by the specified distance, in the direction 153797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis the turtle is headed. 153897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 153997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Example (for a Turtle instance named turtle): 154097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> turtle.position() 154197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis (0.00, 0.00) 154297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> turtle.forward(25) 154397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> turtle.position() 154497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis (25.00,0.00) 154597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> turtle.forward(-75) 154697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> turtle.position() 154797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis (-50.00,0.00) 154897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ 154997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._go(distance) 155097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 155197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def back(self, distance): 155297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """Move the turtle backward by distance. 155397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 155497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Aliases: back | backward | bk 155597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 155697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Argument: 155797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis distance -- a number 155897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 155997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Move the turtle backward by distance ,opposite to the direction the 156097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis turtle is headed. Do not change the turtle's heading. 156197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 156297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Example (for a Turtle instance named turtle): 156397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> turtle.position() 156497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis (0.00, 0.00) 156597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> turtle.backward(30) 156697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> turtle.position() 156797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis (-30.00, 0.00) 156897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ 156997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._go(-distance) 157097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 157197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def right(self, angle): 157297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """Turn turtle right by angle units. 157397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 157497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Aliases: right | rt 157597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 157697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Argument: 157797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis angle -- a number (integer or float) 157897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 157997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Turn turtle right by angle units. (Units are by default degrees, 158097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis but can be set via the degrees() and radians() functions.) 158197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Angle orientation depends on mode. (See this.) 158297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 158397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Example (for a Turtle instance named turtle): 158497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> turtle.heading() 158597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 22.0 158697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> turtle.right(45) 158797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> turtle.heading() 158897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 337.0 158997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ 159097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._rotate(-angle) 159197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 159297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def left(self, angle): 159397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """Turn turtle left by angle units. 159497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 159597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Aliases: left | lt 159697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 159797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Argument: 159897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis angle -- a number (integer or float) 159997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 160097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Turn turtle left by angle units. (Units are by default degrees, 160197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis but can be set via the degrees() and radians() functions.) 160297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Angle orientation depends on mode. (See this.) 160397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 160497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Example (for a Turtle instance named turtle): 160597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> turtle.heading() 160697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 22.0 160797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> turtle.left(45) 160897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> turtle.heading() 160997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 67.0 161097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ 161197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._rotate(angle) 161297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 161397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def pos(self): 161497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """Return the turtle's current location (x,y), as a Vec2D-vector. 161597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 161697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Aliases: pos | position 161797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 161897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis No arguments. 161997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 162097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Example (for a Turtle instance named turtle): 162197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> turtle.pos() 162297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis (0.00, 240.00) 162397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ 162497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis return self._position 162597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 162697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def xcor(self): 162797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ Return the turtle's x coordinate. 162897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 162997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis No arguments. 163097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 163197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Example (for a Turtle instance named turtle): 163297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> reset() 163397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> turtle.left(60) 163497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> turtle.forward(100) 163597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> print turtle.xcor() 163697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 50.0 163797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ 163897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis return self._position[0] 163997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 164097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def ycor(self): 164197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ Return the turtle's y coordinate 164297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis --- 164397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis No arguments. 164497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 164597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Example (for a Turtle instance named turtle): 164697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> reset() 164797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> turtle.left(60) 164897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> turtle.forward(100) 164997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> print turtle.ycor() 165097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 86.6025403784 165197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ 165297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis return self._position[1] 165397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 165497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 165597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def goto(self, x, y=None): 165697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """Move turtle to an absolute position. 165797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 165897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Aliases: setpos | setposition | goto: 165997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 166097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Arguments: 166197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis x -- a number or a pair/vector of numbers 166297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis y -- a number None 166397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 166497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis call: goto(x, y) # two coordinates 166597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis --or: goto((x, y)) # a pair (tuple) of coordinates 166697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis --or: goto(vec) # e.g. as returned by pos() 166797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 166897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Move turtle to an absolute position. If the pen is down, 166997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis a line will be drawn. The turtle's orientation does not change. 167097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 167197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Example (for a Turtle instance named turtle): 167297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> tp = turtle.pos() 167397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> tp 167497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis (0.00, 0.00) 167597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> turtle.setpos(60,30) 167697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> turtle.pos() 167797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis (60.00,30.00) 167897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> turtle.setpos((20,80)) 167997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> turtle.pos() 168097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis (20.00,80.00) 168197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> turtle.setpos(tp) 168297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> turtle.pos() 168397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis (0.00,0.00) 168497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ 168597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if y is None: 168697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._goto(Vec2D(*x)) 168797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis else: 168897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._goto(Vec2D(x, y)) 168997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 169097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def home(self): 169197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """Move turtle to the origin - coordinates (0,0). 169297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 169397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis No arguments. 169497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 169597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Move turtle to the origin - coordinates (0,0) and set it's 169697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis heading to it's start-orientation (which depends on mode). 169797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 169897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Example (for a Turtle instance named turtle): 169997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> turtle.home() 170097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ 170197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.goto(0, 0) 170297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.setheading(0) 170397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 170497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def setx(self, x): 170597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """Set the turtle's first coordinate to x 170697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 170797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Argument: 170897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis x -- a number (integer or float) 170997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 171097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Set the turtle's first coordinate to x, leave second coordinate 171197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis unchanged. 171297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 171397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Example (for a Turtle instance named turtle): 171497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> turtle.position() 171597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis (0.00, 240.00) 171697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> turtle.setx(10) 171797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> turtle.position() 171897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis (10.00, 240.00) 171997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ 172097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._goto(Vec2D(x, self._position[1])) 172197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 172297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def sety(self, y): 172397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """Set the turtle's second coordinate to y 172497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 172597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Argument: 172697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis y -- a number (integer or float) 172797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 172897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Set the turtle's first coordinate to x, second coordinate remains 172997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis unchanged. 173097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 173197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Example (for a Turtle instance named turtle): 173297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> turtle.position() 173397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis (0.00, 40.00) 173497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> turtle.sety(-10) 173597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> turtle.position() 173697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis (0.00, -10.00) 173797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ 173897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._goto(Vec2D(self._position[0], y)) 173997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 174097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def distance(self, x, y=None): 174197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """Return the distance from the turtle to (x,y) in turtle step units. 174297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 174397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Arguments: 174497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis x -- a number or a pair/vector of numbers or a turtle instance 174597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis y -- a number None None 174697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 174797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis call: distance(x, y) # two coordinates 174897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis --or: distance((x, y)) # a pair (tuple) of coordinates 174997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis --or: distance(vec) # e.g. as returned by pos() 175097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis --or: distance(mypen) # where mypen is another turtle 175197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 175297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Example (for a Turtle instance named turtle): 175397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> turtle.pos() 175497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis (0.00, 0.00) 175597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> turtle.distance(30,40) 175697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 50.0 175797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> pen = Turtle() 175897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> pen.forward(77) 175997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> turtle.distance(pen) 176097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 77.0 176197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ 176297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if y is not None: 176397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis pos = Vec2D(x, y) 176497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if isinstance(x, Vec2D): 176597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis pos = x 176697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis elif isinstance(x, tuple): 176797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis pos = Vec2D(*x) 176897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis elif isinstance(x, TNavigator): 176997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis pos = x._position 177097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis return abs(pos - self._position) 177197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 177297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def towards(self, x, y=None): 177397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """Return the angle of the line from the turtle's position to (x, y). 177497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 177597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Arguments: 177697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis x -- a number or a pair/vector of numbers or a turtle instance 177797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis y -- a number None None 177897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 177997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis call: distance(x, y) # two coordinates 178097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis --or: distance((x, y)) # a pair (tuple) of coordinates 178197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis --or: distance(vec) # e.g. as returned by pos() 178297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis --or: distance(mypen) # where mypen is another turtle 178397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 178497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Return the angle, between the line from turtle-position to position 178597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis specified by x, y and the turtle's start orientation. (Depends on 178697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis modes - "standard" or "logo") 178797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 178897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Example (for a Turtle instance named turtle): 178997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> turtle.pos() 179097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis (10.00, 10.00) 179197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> turtle.towards(0,0) 179297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 225.0 179397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ 179497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if y is not None: 179597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis pos = Vec2D(x, y) 179697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if isinstance(x, Vec2D): 179797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis pos = x 179897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis elif isinstance(x, tuple): 179997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis pos = Vec2D(*x) 180097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis elif isinstance(x, TNavigator): 180197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis pos = x._position 180297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis x, y = pos - self._position 180397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis result = round(math.atan2(y, x)*180.0/math.pi, 10) % 360.0 180497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis result /= self._degreesPerAU 180597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis return (self._angleOffset + self._angleOrient*result) % self._fullcircle 180697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 180797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def heading(self): 180897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ Return the turtle's current heading. 180997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 181097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis No arguments. 181197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 181297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Example (for a Turtle instance named turtle): 181397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> turtle.left(67) 181497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> turtle.heading() 181597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 67.0 181697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ 181797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis x, y = self._orient 181897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis result = round(math.atan2(y, x)*180.0/math.pi, 10) % 360.0 181997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis result /= self._degreesPerAU 182097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis return (self._angleOffset + self._angleOrient*result) % self._fullcircle 182197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 182297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def setheading(self, to_angle): 182397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """Set the orientation of the turtle to to_angle. 182497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 182597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Aliases: setheading | seth 182697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 182797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Argument: 182897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis to_angle -- a number (integer or float) 182997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 183097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Set the orientation of the turtle to to_angle. 183197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Here are some common directions in degrees: 183297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 183397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis standard - mode: logo-mode: 183497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis -------------------|-------------------- 183597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 0 - east 0 - north 183697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 90 - north 90 - east 183797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 180 - west 180 - south 183897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 270 - south 270 - west 183997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 184097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Example (for a Turtle instance named turtle): 184197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> turtle.setheading(90) 184297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> turtle.heading() 184397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 90 184497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ 184597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis angle = (to_angle - self.heading())*self._angleOrient 184697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis full = self._fullcircle 184797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis angle = (angle+full/2.)%full - full/2. 184897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._rotate(angle) 184997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 185097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def circle(self, radius, extent = None, steps = None): 185197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ Draw a circle with given radius. 185297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 185397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Arguments: 185497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis radius -- a number 185597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis extent (optional) -- a number 185697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis steps (optional) -- an integer 185797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 185897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Draw a circle with given radius. The center is radius units left 185997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis of the turtle; extent - an angle - determines which part of the 186097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis circle is drawn. If extent is not given, draw the entire circle. 186197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis If extent is not a full circle, one endpoint of the arc is the 186297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis current pen position. Draw the arc in counterclockwise direction 186397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if radius is positive, otherwise in clockwise direction. Finally 186497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis the direction of the turtle is changed by the amount of extent. 186597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 186697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis As the circle is approximated by an inscribed regular polygon, 186797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis steps determines the number of steps to use. If not given, 186897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis it will be calculated automatically. Maybe used to draw regular 186997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis polygons. 187097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 187197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis call: circle(radius) # full circle 187297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis --or: circle(radius, extent) # arc 187397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis --or: circle(radius, extent, steps) 187497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis --or: circle(radius, steps=6) # 6-sided polygon 187597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 187697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Example (for a Turtle instance named turtle): 187797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> turtle.circle(50) 187897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> turtle.circle(120, 180) # semicircle 187997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ 188097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if self.undobuffer: 188197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.undobuffer.push(["seq"]) 188297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.undobuffer.cumulate = True 188397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis speed = self.speed() 188497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if extent is None: 188597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis extent = self._fullcircle 188697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if steps is None: 188797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis frac = abs(extent)/self._fullcircle 188897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis steps = 1+int(min(11+abs(radius)/6.0, 59.0)*frac) 188997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis w = 1.0 * extent / steps 189097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis w2 = 0.5 * w 189197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis l = 2.0 * radius * math.sin(w2*math.pi/180.0*self._degreesPerAU) 189297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if radius < 0: 189397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis l, w, w2 = -l, -w, -w2 189497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis tr = self._tracer() 189597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis dl = self._delay() 189697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if speed == 0: 189797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._tracer(0, 0) 189897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis else: 189997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.speed(0) 190097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._rotate(w2) 190197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis for i in range(steps): 190297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.speed(speed) 190397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._go(l) 190497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.speed(0) 190597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._rotate(w) 190697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._rotate(-w2) 190797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if speed == 0: 190897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._tracer(tr, dl) 190997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.speed(speed) 191097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if self.undobuffer: 191197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.undobuffer.cumulate = False 191297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 191397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis## three dummy methods to be implemented by child class: 191497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 191597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def speed(self, s=0): 191697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """dummy method - to be overwritten by child class""" 191797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def _tracer(self, a=None, b=None): 191897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """dummy method - to be overwritten by child class""" 191997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def _delay(self, n=None): 192097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """dummy method - to be overwritten by child class""" 192197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 192297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis fd = forward 192397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis bk = back 192497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis backward = back 192597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis rt = right 192697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis lt = left 192797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis position = pos 192897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis setpos = goto 192997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis setposition = goto 193097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis seth = setheading 193197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 193297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 193397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwisclass TPen(object): 193497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """Drawing part of the RawTurtle. 193597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Implements drawing properties. 193697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ 193797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def __init__(self, resizemode=_CFG["resizemode"]): 193897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._resizemode = resizemode # or "user" or "noresize" 193997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.undobuffer = None 194097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis TPen._reset(self) 194197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 194297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def _reset(self, pencolor=_CFG["pencolor"], 194397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis fillcolor=_CFG["fillcolor"]): 194497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._pensize = 1 194597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._shown = True 194697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._pencolor = pencolor 194797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._fillcolor = fillcolor 194897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._drawing = True 194997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._speed = 3 195097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._stretchfactor = (1, 1) 195197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._tilt = 0 195297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._outlinewidth = 1 195397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis ### self.screen = None # to override by child class 195497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 195597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def resizemode(self, rmode=None): 195697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """Set resizemode to one of the values: "auto", "user", "noresize". 195797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 195897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis (Optional) Argument: 195997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis rmode -- one of the strings "auto", "user", "noresize" 196097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 196197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Different resizemodes have the following effects: 196297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis - "auto" adapts the appearance of the turtle 196397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis corresponding to the value of pensize. 196497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis - "user" adapts the appearance of the turtle according to the 196597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis values of stretchfactor and outlinewidth (outline), 196697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis which are set by shapesize() 196797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis - "noresize" no adaption of the turtle's appearance takes place. 196897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis If no argument is given, return current resizemode. 196997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis resizemode("user") is called by a call of shapesize with arguments. 197097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 197197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 197297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Examples (for a Turtle instance named turtle): 197397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> turtle.resizemode("noresize") 197497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> turtle.resizemode() 197597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 'noresize' 197697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ 197797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if rmode is None: 197897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis return self._resizemode 197997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis rmode = rmode.lower() 198097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if rmode in ["auto", "user", "noresize"]: 198197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.pen(resizemode=rmode) 198297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 198397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def pensize(self, width=None): 198497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """Set or return the line thickness. 198597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 198697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Aliases: pensize | width 198797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 198897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Argument: 198997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis width -- positive number 199097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 199197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Set the line thickness to width or return it. If resizemode is set 199297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis to "auto" and turtleshape is a polygon, that polygon is drawn with 199397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis the same line thickness. If no argument is given, current pensize 199497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis is returned. 199597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 199697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Example (for a Turtle instance named turtle): 199797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> turtle.pensize() 199897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 1 199997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis turtle.pensize(10) # from here on lines of width 10 are drawn 200097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ 200197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if width is None: 200297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis return self._pensize 200397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.pen(pensize=width) 200497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 200597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 200697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def penup(self): 200797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """Pull the pen up -- no drawing when moving. 200897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 200997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Aliases: penup | pu | up 201097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 201197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis No argument 201297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 201397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Example (for a Turtle instance named turtle): 201497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> turtle.penup() 201597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ 201697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if not self._drawing: 201797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis return 201897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.pen(pendown=False) 201997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 202097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def pendown(self): 202197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """Pull the pen down -- drawing when moving. 202297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 202397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Aliases: pendown | pd | down 202497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 202597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis No argument. 202697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 202797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Example (for a Turtle instance named turtle): 202897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> turtle.pendown() 202997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ 203097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if self._drawing: 203197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis return 203297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.pen(pendown=True) 203397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 203497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def isdown(self): 203597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """Return True if pen is down, False if it's up. 203697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 203797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis No argument. 203897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 203997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Example (for a Turtle instance named turtle): 204097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> turtle.penup() 204197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> turtle.isdown() 204297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis False 204397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> turtle.pendown() 204497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> turtle.isdown() 204597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis True 204697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ 204797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis return self._drawing 204897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 204997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def speed(self, speed=None): 205097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ Return or set the turtle's speed. 205197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 205297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Optional argument: 205397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis speed -- an integer in the range 0..10 or a speedstring (see below) 205497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 205597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Set the turtle's speed to an integer value in the range 0 .. 10. 205697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis If no argument is given: return current speed. 205797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 205897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis If input is a number greater than 10 or smaller than 0.5, 205997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis speed is set to 0. 206097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Speedstrings are mapped to speedvalues in the following way: 206197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 'fastest' : 0 206297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 'fast' : 10 206397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 'normal' : 6 206497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 'slow' : 3 206597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 'slowest' : 1 206697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis speeds from 1 to 10 enforce increasingly faster animation of 206797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis line drawing and turtle turning. 206897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 206997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Attention: 207097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis speed = 0 : *no* animation takes place. forward/back makes turtle jump 207197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis and likewise left/right make the turtle turn instantly. 207297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 207397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Example (for a Turtle instance named turtle): 207497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> turtle.speed(3) 207597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ 207697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis speeds = {'fastest':0, 'fast':10, 'normal':6, 'slow':3, 'slowest':1 } 207797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if speed is None: 207897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis return self._speed 207997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if speed in speeds: 208097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis speed = speeds[speed] 208197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis elif 0.5 < speed < 10.5: 208297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis speed = int(round(speed)) 208397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis else: 208497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis speed = 0 208597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.pen(speed=speed) 208697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 208797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def color(self, *args): 208897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """Return or set the pencolor and fillcolor. 208997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 209097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Arguments: 209197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Several input formats are allowed. 209297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis They use 0, 1, 2, or 3 arguments as follows: 209397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 209497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis color() 209597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Return the current pencolor and the current fillcolor 209697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis as a pair of color specification strings as are returned 209797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis by pencolor and fillcolor. 209897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis color(colorstring), color((r,g,b)), color(r,g,b) 209997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis inputs as in pencolor, set both, fillcolor and pencolor, 210097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis to the given value. 210197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis color(colorstring1, colorstring2), 210297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis color((r1,g1,b1), (r2,g2,b2)) 210397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis equivalent to pencolor(colorstring1) and fillcolor(colorstring2) 210497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis and analogously, if the other input format is used. 210597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 210697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis If turtleshape is a polygon, outline and interior of that polygon 210797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis is drawn with the newly set colors. 210897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis For mor info see: pencolor, fillcolor 210997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 211097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Example (for a Turtle instance named turtle): 211197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> turtle.color('red', 'green') 211297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> turtle.color() 211397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis ('red', 'green') 211497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> colormode(255) 211597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> color((40, 80, 120), (160, 200, 240)) 211697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> color() 211797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis ('#285078', '#a0c8f0') 211897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ 211997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if args: 212097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis l = len(args) 212197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if l == 1: 212297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis pcolor = fcolor = args[0] 212397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis elif l == 2: 212497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis pcolor, fcolor = args 212597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis elif l == 3: 212697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis pcolor = fcolor = args 212797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis pcolor = self._colorstr(pcolor) 212897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis fcolor = self._colorstr(fcolor) 212997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.pen(pencolor=pcolor, fillcolor=fcolor) 213097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis else: 213197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis return self._color(self._pencolor), self._color(self._fillcolor) 213297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 213397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def pencolor(self, *args): 213497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ Return or set the pencolor. 213597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 213697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Arguments: 213797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Four input formats are allowed: 213897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis - pencolor() 213997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Return the current pencolor as color specification string, 214097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis possibly in hex-number format (see example). 214197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis May be used as input to another color/pencolor/fillcolor call. 214297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis - pencolor(colorstring) 214397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis s is a Tk color specification string, such as "red" or "yellow" 214497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis - pencolor((r, g, b)) 214597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis *a tuple* of r, g, and b, which represent, an RGB color, 214697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis and each of r, g, and b are in the range 0..colormode, 214797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis where colormode is either 1.0 or 255 214897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis - pencolor(r, g, b) 214997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis r, g, and b represent an RGB color, and each of r, g, and b 215097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis are in the range 0..colormode 215197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 215297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis If turtleshape is a polygon, the outline of that polygon is drawn 215397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis with the newly set pencolor. 215497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 215597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Example (for a Turtle instance named turtle): 215697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> turtle.pencolor('brown') 215797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> tup = (0.2, 0.8, 0.55) 215897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> turtle.pencolor(tup) 215997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> turtle.pencolor() 216097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis '#33cc8c' 216197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ 216297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if args: 216397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis color = self._colorstr(args) 216497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if color == self._pencolor: 216597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis return 216697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.pen(pencolor=color) 216797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis else: 216897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis return self._color(self._pencolor) 216997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 217097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def fillcolor(self, *args): 217197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ Return or set the fillcolor. 217297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 217397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Arguments: 217497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Four input formats are allowed: 217597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis - fillcolor() 217697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Return the current fillcolor as color specification string, 217797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis possibly in hex-number format (see example). 217897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis May be used as input to another color/pencolor/fillcolor call. 217997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis - fillcolor(colorstring) 218097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis s is a Tk color specification string, such as "red" or "yellow" 218197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis - fillcolor((r, g, b)) 218297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis *a tuple* of r, g, and b, which represent, an RGB color, 218397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis and each of r, g, and b are in the range 0..colormode, 218497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis where colormode is either 1.0 or 255 218597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis - fillcolor(r, g, b) 218697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis r, g, and b represent an RGB color, and each of r, g, and b 218797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis are in the range 0..colormode 218897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 218997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis If turtleshape is a polygon, the interior of that polygon is drawn 219097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis with the newly set fillcolor. 219197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 219297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Example (for a Turtle instance named turtle): 219397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> turtle.fillcolor('violet') 219497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> col = turtle.pencolor() 219597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> turtle.fillcolor(col) 219697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> turtle.fillcolor(0, .5, 0) 219797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ 219897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if args: 219997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis color = self._colorstr(args) 220097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if color == self._fillcolor: 220197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis return 220297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.pen(fillcolor=color) 220397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis else: 220497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis return self._color(self._fillcolor) 220597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 220697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def showturtle(self): 220797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """Makes the turtle visible. 220897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 220997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Aliases: showturtle | st 221097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 221197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis No argument. 221297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 221397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Example (for a Turtle instance named turtle): 221497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> turtle.hideturtle() 221597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> turtle.showturtle() 221697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ 221797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.pen(shown=True) 221897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 221997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def hideturtle(self): 222097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """Makes the turtle invisible. 222197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 222297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Aliases: hideturtle | ht 222397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 222497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis No argument. 222597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 222697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis It's a good idea to do this while you're in the 222797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis middle of a complicated drawing, because hiding 222897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis the turtle speeds up the drawing observably. 222997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 223097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Example (for a Turtle instance named turtle): 223197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> turtle.hideturtle() 2232477c8d5e70240744d24631b18341ad892c8a8e1cThomas Wouters """ 223397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.pen(shown=False) 2234b241b67b8954b0679377af00d668e3dc92f4c858Guido van Rossum 223597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def isvisible(self): 223697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """Return True if the Turtle is shown, False if it's hidden. 2237477c8d5e70240744d24631b18341ad892c8a8e1cThomas Wouters 223897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis No argument. 223997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 224097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Example (for a Turtle instance named turtle): 224197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> turtle.hideturtle() 224297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> print turtle.isvisible(): 224397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis False 2244477c8d5e70240744d24631b18341ad892c8a8e1cThomas Wouters """ 224597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis return self._shown 224697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 224797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def pen(self, pen=None, **pendict): 224897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """Return or set the pen's attributes. 224997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 225097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Arguments: 225197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis pen -- a dictionary with some or all of the below listed keys. 225297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis **pendict -- one or more keyword-arguments with the below 225397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis listed keys as keywords. 225497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 225597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Return or set the pen's attributes in a 'pen-dictionary' 225697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis with the following key/value pairs: 225797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis "shown" : True/False 225897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis "pendown" : True/False 225997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis "pencolor" : color-string or color-tuple 226097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis "fillcolor" : color-string or color-tuple 226197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis "pensize" : positive number 226297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis "speed" : number in range 0..10 226397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis "resizemode" : "auto" or "user" or "noresize" 226497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis "stretchfactor": (positive number, positive number) 226597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis "outline" : positive number 226697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis "tilt" : number 226797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 226897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis This dicionary can be used as argument for a subsequent 226997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis pen()-call to restore the former pen-state. Moreover one 227097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis or more of these attributes can be provided as keyword-arguments. 227197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis This can be used to set several pen attributes in one statement. 227297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 227397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 227497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Examples (for a Turtle instance named turtle): 227597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> turtle.pen(fillcolor="black", pencolor="red", pensize=10) 227697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> turtle.pen() 227797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis {'pensize': 10, 'shown': True, 'resizemode': 'auto', 'outline': 1, 227897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 'pencolor': 'red', 'pendown': True, 'fillcolor': 'black', 227997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 'stretchfactor': (1,1), 'speed': 3} 228097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> penstate=turtle.pen() 228197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> turtle.color("yellow","") 228297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> turtle.penup() 228397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> turtle.pen() 228497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis {'pensize': 10, 'shown': True, 'resizemode': 'auto', 'outline': 1, 228597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 'pencolor': 'yellow', 'pendown': False, 'fillcolor': '', 228697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 'stretchfactor': (1,1), 'speed': 3} 228797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> p.pen(penstate, fillcolor="green") 228897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> p.pen() 228997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis {'pensize': 10, 'shown': True, 'resizemode': 'auto', 'outline': 1, 229097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 'pencolor': 'red', 'pendown': True, 'fillcolor': 'green', 229197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 'stretchfactor': (1,1), 'speed': 3} 229297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ 229397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis _pd = {"shown" : self._shown, 229497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis "pendown" : self._drawing, 229597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis "pencolor" : self._pencolor, 229697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis "fillcolor" : self._fillcolor, 229797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis "pensize" : self._pensize, 229897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis "speed" : self._speed, 229997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis "resizemode" : self._resizemode, 230097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis "stretchfactor" : self._stretchfactor, 230197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis "outline" : self._outlinewidth, 230297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis "tilt" : self._tilt 230397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis } 230497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 230597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if not (pen or pendict): 230697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis return _pd 230797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 230897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if isinstance(pen, dict): 230997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis p = pen 231097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis else: 231197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis p = {} 231297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis p.update(pendict) 231397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 231497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis _p_buf = {} 231597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis for key in p: 231697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis _p_buf[key] = _pd[key] 231797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 231897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if self.undobuffer: 231997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.undobuffer.push(("pen", _p_buf)) 232097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 232197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis newLine = False 232297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if "pendown" in p: 232397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if self._drawing != p["pendown"]: 232497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis newLine = True 232597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if "pencolor" in p: 232697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if isinstance(p["pencolor"], tuple): 232797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis p["pencolor"] = self._colorstr((p["pencolor"],)) 232897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if self._pencolor != p["pencolor"]: 232997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis newLine = True 233097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if "pensize" in p: 233197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if self._pensize != p["pensize"]: 233297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis newLine = True 233397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if newLine: 233497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._newLine() 233597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if "pendown" in p: 233697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._drawing = p["pendown"] 233797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if "pencolor" in p: 233897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._pencolor = p["pencolor"] 233997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if "pensize" in p: 234097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._pensize = p["pensize"] 234197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if "fillcolor" in p: 234297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if isinstance(p["fillcolor"], tuple): 234397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis p["fillcolor"] = self._colorstr((p["fillcolor"],)) 234497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._fillcolor = p["fillcolor"] 234597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if "speed" in p: 234697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._speed = p["speed"] 234797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if "resizemode" in p: 234897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._resizemode = p["resizemode"] 234997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if "stretchfactor" in p: 235097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis sf = p["stretchfactor"] 235197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if isinstance(sf, (int, float)): 235297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis sf = (sf, sf) 235397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._stretchfactor = sf 235497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if "outline" in p: 235597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._outlinewidth = p["outline"] 235697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if "shown" in p: 235797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._shown = p["shown"] 235897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if "tilt" in p: 235997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._tilt = p["tilt"] 236097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._update() 236197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 236297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis## three dummy methods to be implemented by child class: 236397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 236497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def _newLine(self, usePos = True): 236597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """dummy method - to be overwritten by child class""" 236697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def _update(self, count=True, forced=False): 236797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """dummy method - to be overwritten by child class""" 236897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def _color(self, args): 236997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """dummy method - to be overwritten by child class""" 237097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def _colorstr(self, args): 237197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """dummy method - to be overwritten by child class""" 237297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 237397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis width = pensize 237497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis up = penup 237597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis pu = penup 237697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis pd = pendown 237797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis down = pendown 237897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis st = showturtle 237997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis ht = hideturtle 238097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 238197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 238297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwisclass _TurtleImage(object): 238397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """Helper class: Datatype to store Turtle attributes 238497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ 238597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 238697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def __init__(self, screen, shapeIndex): 238797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.screen = screen 238897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._type = None 238997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._setshape(shapeIndex) 239097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 239197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def _setshape(self, shapeIndex): 239297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis screen = self.screen # RawTurtle.screens[self.screenIndex] 239397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.shapeIndex = shapeIndex 239497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if self._type == "polygon" == screen._shapes[shapeIndex]._type: 239597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis return 239697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if self._type == "image" == screen._shapes[shapeIndex]._type: 239797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis return 239897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if self._type in ["image", "polygon"]: 239997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis screen._delete(self._item) 240097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis elif self._type == "compound": 240197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis for item in self._item: 240297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis screen._delete(item) 240397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._type = screen._shapes[shapeIndex]._type 240497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if self._type == "polygon": 240597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._item = screen._createpoly() 240697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis elif self._type == "image": 240797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._item = screen._createimage(screen._shapes["blank"]._data) 240897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis elif self._type == "compound": 240997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._item = [screen._createpoly() for item in 241097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis screen._shapes[shapeIndex]._data] 241197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 241297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 241397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwisclass RawTurtle(TPen, TNavigator): 241497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """Animation part of the RawTurtle. 241597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Puts RawTurtle upon a TurtleScreen and provides tools for 241697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis it's animation. 241797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ 241897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis screens = [] 241997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 242097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def __init__(self, canvas=None, 242197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis shape=_CFG["shape"], 242297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis undobuffersize=_CFG["undobuffersize"], 242397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis visible=_CFG["visible"]): 2424601149bb8e4fd200cc5b0b670f34c608c64e0783Martin v. Löwis if isinstance(canvas, _Screen): 242597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.screen = canvas 242697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis elif isinstance(canvas, TurtleScreen): 242797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if canvas not in RawTurtle.screens: 242897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis RawTurtle.screens.append(canvas) 242997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.screen = canvas 243097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis elif isinstance(canvas, (ScrolledCanvas, Canvas)): 243197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis for screen in RawTurtle.screens: 243297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if screen.cv == canvas: 243397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.screen = screen 243497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis break 243597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis else: 243697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.screen = TurtleScreen(canvas) 243797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis RawTurtle.screens.append(self.screen) 243897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis else: 243997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis raise TurtleGraphicsError("bad cavas argument %s" % canvas) 244097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 244197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis screen = self.screen 244297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis TNavigator.__init__(self, screen.mode()) 244397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis TPen.__init__(self) 244497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis screen._turtles.append(self) 244597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.drawingLineItem = screen._createline() 244697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.turtle = _TurtleImage(screen, shape) 244797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._poly = None 244897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._creatingPoly = False 244997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._fillitem = self._fillpath = None 245097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._shown = visible 245197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._hidden_from_screen = False 245297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.currentLineItem = screen._createline() 245397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.currentLine = [self._position] 245497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.items = [self.currentLineItem] 245597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.stampItems = [] 245697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._undobuffersize = undobuffersize 245797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.undobuffer = Tbuffer(undobuffersize) 245897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._update() 2459b241b67b8954b0679377af00d668e3dc92f4c858Guido van Rossum 2460b241b67b8954b0679377af00d668e3dc92f4c858Guido van Rossum def reset(self): 246197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """Delete the turtle's drawings and restore it's default values. 2462477c8d5e70240744d24631b18341ad892c8a8e1cThomas Wouters 246397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis No argument. 246497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis, 246597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Delete the turtle's drawings from the screen, re-center the turtle 246697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis and set variables to the default values. 246797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 246897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Example (for a Turtle instance named turtle): 2469477c8d5e70240744d24631b18341ad892c8a8e1cThomas Wouters >>> turtle.position() 247097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis (0.00,-22.00) 2471477c8d5e70240744d24631b18341ad892c8a8e1cThomas Wouters >>> turtle.heading() 2472477c8d5e70240744d24631b18341ad892c8a8e1cThomas Wouters 100.0 2473477c8d5e70240744d24631b18341ad892c8a8e1cThomas Wouters >>> turtle.reset() 2474477c8d5e70240744d24631b18341ad892c8a8e1cThomas Wouters >>> turtle.position() 247597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis (0.00,0.00) 2476477c8d5e70240744d24631b18341ad892c8a8e1cThomas Wouters >>> turtle.heading() 2477477c8d5e70240744d24631b18341ad892c8a8e1cThomas Wouters 0.0 2478477c8d5e70240744d24631b18341ad892c8a8e1cThomas Wouters """ 247997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis TNavigator.reset(self) 248097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis TPen._reset(self) 248197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._clear() 248297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._drawturtle() 248397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._update() 2484b241b67b8954b0679377af00d668e3dc92f4c858Guido van Rossum 248597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def setundobuffer(self, size): 248697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """Set or disable undobuffer. 2487477c8d5e70240744d24631b18341ad892c8a8e1cThomas Wouters 248897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Argument: 248997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis size -- an integer or None 24903c7a25a4d9ea07f2aa8e4b70fecd84e33de82c2eGuido van Rossum 249197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis If size is an integer an empty undobuffer of given size is installed. 249297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Size gives the maximum number of turtle-actions that can be undone 249397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis by the undo() function. 249497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis If size is None, no undobuffer is present. 2495477c8d5e70240744d24631b18341ad892c8a8e1cThomas Wouters 249697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Example (for a Turtle instance named turtle): 249797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> turtle.setundobuffer(42) 2498477c8d5e70240744d24631b18341ad892c8a8e1cThomas Wouters """ 249997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if size is None: 250097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.undobuffer = None 250197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis else: 250297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.undobuffer = Tbuffer(size) 2503b241b67b8954b0679377af00d668e3dc92f4c858Guido van Rossum 250497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def undobufferentries(self): 250597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """Return count of entries in the undobuffer. 2506477c8d5e70240744d24631b18341ad892c8a8e1cThomas Wouters 250797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis No argument. 250897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 250997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Example (for a Turtle instance named turtle): 251097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> while undobufferentries(): 251197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis undo() 2512477c8d5e70240744d24631b18341ad892c8a8e1cThomas Wouters """ 251397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if self.undobuffer is None: 251497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis return 0 251597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis return self.undobuffer.nr_of_items() 251697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 251797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def _clear(self): 251897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """Delete all of pen's drawings""" 251997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._fillitem = self._fillpath = None 252097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis for item in self.items: 252197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.screen._delete(item) 252297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.currentLineItem = self.screen._createline() 252397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.currentLine = [] 252497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if self._drawing: 252597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.currentLine.append(self._position) 252697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.items = [self.currentLineItem] 252797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.clearstamps() 252897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.setundobuffer(self._undobuffersize) 2529b241b67b8954b0679377af00d668e3dc92f4c858Guido van Rossum 2530477c8d5e70240744d24631b18341ad892c8a8e1cThomas Wouters 253197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def clear(self): 253297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """Delete the turtle's drawings from the screen. Do not move turtle. 2533477c8d5e70240744d24631b18341ad892c8a8e1cThomas Wouters 253497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis No arguments. 2535b241b67b8954b0679377af00d668e3dc92f4c858Guido van Rossum 253697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Delete the turtle's drawings from the screen. Do not move turtle. 253797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis State and position of the turtle as well as drawings of other 253897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis turtles are not affected. 2539477c8d5e70240744d24631b18341ad892c8a8e1cThomas Wouters 254097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Examples (for a Turtle instance named turtle): 254197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> turtle.clear() 254297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ 254397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._clear() 254497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._update() 2545477c8d5e70240744d24631b18341ad892c8a8e1cThomas Wouters 254697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def _update_data(self): 254797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.screen._incrementudc() 254897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if self.screen._updatecounter != 0: 254997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis return 255097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if len(self.currentLine)>1: 255197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.screen._drawline(self.currentLineItem, self.currentLine, 255297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._pencolor, self._pensize) 255397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 255497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def _update(self): 255597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """Perform a Turtle-data update. 2556477c8d5e70240744d24631b18341ad892c8a8e1cThomas Wouters """ 255797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis screen = self.screen 255897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if screen._tracing == 0: 255997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis return 256097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis elif screen._tracing == 1: 256197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._update_data() 256297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._drawturtle() 256397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis screen._update() # TurtleScreenBase 256497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis screen._delay(screen._delayvalue) # TurtleScreenBase 256597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis else: 256697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._update_data() 256797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if screen._updatecounter == 0: 256897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis for t in screen.turtles(): 256997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis t._drawturtle() 257097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis screen._update() 257197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 257297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def _tracer(self, flag=None, delay=None): 257397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """Turns turtle animation on/off and set delay for update drawings. 257497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 257597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Optional arguments: 257697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis n -- nonnegative integer 257797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis delay -- nonnegative integer 257897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 257997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis If n is given, only each n-th regular screen update is really performed. 258097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis (Can be used to accelerate the drawing of complex graphics.) 258197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Second arguments sets delay value (see RawTurtle.delay()) 258297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 258397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Example (for a Turtle instance named turtle): 258497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> turtle.tracer(8, 25) 258597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> dist = 2 258697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> for i in range(200): 258797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis turtle.fd(dist) 258897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis turtle.rt(90) 258997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis dist += 2 259097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ 259197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis return self.screen.tracer(flag, delay) 2592b241b67b8954b0679377af00d668e3dc92f4c858Guido van Rossum 259397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def _color(self, args): 259497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis return self.screen._color(args) 2595477c8d5e70240744d24631b18341ad892c8a8e1cThomas Wouters 259697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def _colorstr(self, args): 259797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis return self.screen._colorstr(args) 2598477c8d5e70240744d24631b18341ad892c8a8e1cThomas Wouters 259997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def _cc(self, args): 260097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """Convert colortriples to hexstrings. 2601477c8d5e70240744d24631b18341ad892c8a8e1cThomas Wouters """ 260297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if isinstance(args, str): 260397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis return args 260497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis try: 260597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis r, g, b = args 260697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis except: 260797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis raise TurtleGraphicsError("bad color arguments: %s" % str(args)) 260897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if self.screen._colormode == 1.0: 260997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis r, g, b = [round(255.0*x) for x in (r, g, b)] 261097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if not ((0 <= r <= 255) and (0 <= g <= 255) and (0 <= b <= 255)): 261197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis raise TurtleGraphicsError("bad color sequence: %s" % str(args)) 261297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis return "#%02x%02x%02x" % (r, g, b) 2613b241b67b8954b0679377af00d668e3dc92f4c858Guido van Rossum 261497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def clone(self): 261597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """Create and return a clone of the turtle. 2616477c8d5e70240744d24631b18341ad892c8a8e1cThomas Wouters 261797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis No argument. 2618b241b67b8954b0679377af00d668e3dc92f4c858Guido van Rossum 261997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Create and return a clone of the turtle with same position, heading 262097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis and turtle properties. 2621477c8d5e70240744d24631b18341ad892c8a8e1cThomas Wouters 262297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Example (for a Turtle instance named mick): 262397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis mick = Turtle() 262497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis joe = mick.clone() 262597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ 262697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis screen = self.screen 262797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._newLine(self._drawing) 262897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 262997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis turtle = self.turtle 263097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.screen = None 263197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.turtle = None # too make self deepcopy-able 263297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 263397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis q = deepcopy(self) 263497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 263597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.screen = screen 263697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.turtle = turtle 263797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 263897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis q.screen = screen 263997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis q.turtle = _TurtleImage(screen, self.turtle.shapeIndex) 264097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 264197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis screen._turtles.append(q) 264297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis ttype = screen._shapes[self.turtle.shapeIndex]._type 264397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if ttype == "polygon": 264497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis q.turtle._item = screen._createpoly() 264597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis elif ttype == "image": 264697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis q.turtle._item = screen._createimage(screen._shapes["blank"]._data) 264797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis elif ttype == "compound": 264897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis q.turtle._item = [screen._createpoly() for item in 264997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis screen._shapes[self.turtle.shapeIndex]._data] 265097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis q.currentLineItem = screen._createline() 265197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis q._update() 265297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis return q 265397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 265497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def shape(self, name=None): 265597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """Set turtle shape to shape with given name / return current shapename. 265697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 265797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Optional argument: 265897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis name -- a string, which is a valid shapename 265997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 266097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Set turtle shape to shape with given name or, if name is not given, 266197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis return name of current shape. 266297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Shape with name must exist in the TurtleScreen's shape dictionary. 266397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Initially there are the following polygon shapes: 266497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 'arrow', 'turtle', 'circle', 'square', 'triangle', 'classic'. 266597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis To learn about how to deal with shapes see Screen-method register_shape. 266697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 266797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Example (for a Turtle instance named turtle): 266897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> turtle.shape() 266997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 'arrow' 267097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> turtle.shape("turtle") 267197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> turtle.shape() 267297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 'turtle' 267397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ 267497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if name is None: 267597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis return self.turtle.shapeIndex 267697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if not name in self.screen.getshapes(): 267797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis raise TurtleGraphicsError("There is no shape named %s" % name) 267897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.turtle._setshape(name) 267997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._update() 268097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 268197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def shapesize(self, stretch_wid=None, stretch_len=None, outline=None): 268297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """Set/return turtle's stretchfactors/outline. Set resizemode to "user". 268397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 268497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Optinonal arguments: 268597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis stretch_wid : positive number 268697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis stretch_len : positive number 268797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis outline : positive number 268897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 268997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Return or set the pen's attributes x/y-stretchfactors and/or outline. 269097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Set resizemode to "user". 269197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis If and only if resizemode is set to "user", the turtle will be displayed 269297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis stretched according to its stretchfactors: 269397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis stretch_wid is stretchfactor perpendicular to orientation 269497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis stretch_len is stretchfactor in direction of turtles orientation. 269597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis outline determines the width of the shapes's outline. 269697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 269797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Examples (for a Turtle instance named turtle): 269897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> turtle.resizemode("user") 269997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> turtle.shapesize(5, 5, 12) 270097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> turtle.shapesize(outline=8) 270197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ 270297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if stretch_wid is None and stretch_len is None and outline == None: 270397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis stretch_wid, stretch_len = self._stretchfactor 270497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis return stretch_wid, stretch_len, self._outlinewidth 270597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if stretch_wid is not None: 270697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if stretch_len is None: 270797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis stretchfactor = stretch_wid, stretch_wid 270897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis else: 270997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis stretchfactor = stretch_wid, stretch_len 271097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis elif stretch_len is not None: 271197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis stretchfactor = self._stretchfactor[0], stretch_len 271297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis else: 271397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis stretchfactor = self._stretchfactor 271497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if outline is None: 271597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis outline = self._outlinewidth 271697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.pen(resizemode="user", 271797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis stretchfactor=stretchfactor, outline=outline) 271897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 271997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def settiltangle(self, angle): 272097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """Rotate the turtleshape to point in the specified direction 272197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 272297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Optional argument: 272397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis angle -- number 272497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 272597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Rotate the turtleshape to point in the direction specified by angle, 272697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis regardless of its current tilt-angle. DO NOT change the turtle's 272797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis heading (direction of movement). 272897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 272997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 273097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Examples (for a Turtle instance named turtle): 273197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> turtle.shape("circle") 273297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> turtle.shapesize(5,2) 273397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> turtle.settiltangle(45) 273497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> stamp() 273597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> turtle.fd(50) 273697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> turtle.settiltangle(-45) 273797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> stamp() 273897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> turtle.fd(50) 2739477c8d5e70240744d24631b18341ad892c8a8e1cThomas Wouters """ 274097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis tilt = -angle * self._degreesPerAU * self._angleOrient 274197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis tilt = (tilt * math.pi / 180.0) % (2*math.pi) 274297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.pen(resizemode="user", tilt=tilt) 2743b241b67b8954b0679377af00d668e3dc92f4c858Guido van Rossum 274497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def tiltangle(self): 274597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """Return the current tilt-angle. 2746477c8d5e70240744d24631b18341ad892c8a8e1cThomas Wouters 274797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis No argument. 2748b241b67b8954b0679377af00d668e3dc92f4c858Guido van Rossum 274997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Return the current tilt-angle, i. e. the angle between the 275097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis orientation of the turtleshape and the heading of the turtle 275197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis (it's direction of movement). 2752477c8d5e70240744d24631b18341ad892c8a8e1cThomas Wouters 275397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Examples (for a Turtle instance named turtle): 275497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> turtle.shape("circle") 275597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> turtle.shapesize(5,2) 275697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> turtle.tilt(45) 275797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> turtle.tiltangle() 275897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> 275997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ 276097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis tilt = -self._tilt * (180.0/math.pi) * self._angleOrient 276197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis return (tilt / self._degreesPerAU) % self._fullcircle 2762477c8d5e70240744d24631b18341ad892c8a8e1cThomas Wouters 276397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def tilt(self, angle): 276497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """Rotate the turtleshape by angle. 2765477c8d5e70240744d24631b18341ad892c8a8e1cThomas Wouters 276697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Argument: 276797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis angle - a number 2768477c8d5e70240744d24631b18341ad892c8a8e1cThomas Wouters 276997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Rotate the turtleshape by angle from its current tilt-angle, 277097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis but do NOT change the turtle's heading (direction of movement). 2771477c8d5e70240744d24631b18341ad892c8a8e1cThomas Wouters 277297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Examples (for a Turtle instance named turtle): 277397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> turtle.shape("circle") 277497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> turtle.shapesize(5,2) 277597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> turtle.tilt(30) 277697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> turtle.fd(50) 277797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> turtle.tilt(30) 277897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> turtle.fd(50) 277997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ 278097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.settiltangle(angle + self.tiltangle()) 2781477c8d5e70240744d24631b18341ad892c8a8e1cThomas Wouters 278297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def _polytrafo(self, poly): 278397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """Computes transformed polygon shapes from a shape 278497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis according to current position and heading. 278597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ 278697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis screen = self.screen 278797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis p0, p1 = self._position 278897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis e0, e1 = self._orient 278997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis e = Vec2D(e0, e1 * screen.yscale / screen.xscale) 279097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis e0, e1 = (1.0 / abs(e)) * e 279197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis return [(p0+(e1*x+e0*y)/screen.xscale, p1+(-e0*x+e1*y)/screen.yscale) 279297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis for (x, y) in poly] 279397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 279497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def _drawturtle(self): 279597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """Manages the correct rendering of the turtle with respect to 279697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis it's shape, resizemode, strech and tilt etc.""" 279797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis screen = self.screen 279897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis shape = screen._shapes[self.turtle.shapeIndex] 279997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis ttype = shape._type 280097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis titem = self.turtle._item 280197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if self._shown and screen._updatecounter == 0 and screen._tracing > 0: 280297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._hidden_from_screen = False 280397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis tshape = shape._data 280497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if ttype == "polygon": 280597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if self._resizemode == "noresize": 280697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis w = 1 280797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis shape = tshape 280897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis else: 280997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if self._resizemode == "auto": 281097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis lx = ly = max(1, self._pensize/5.0) 281197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis w = self._pensize 281297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis tiltangle = 0 281397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis elif self._resizemode == "user": 281497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis lx, ly = self._stretchfactor 281597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis w = self._outlinewidth 281697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis tiltangle = self._tilt 281797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis shape = [(lx*x, ly*y) for (x, y) in tshape] 281897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis t0, t1 = math.sin(tiltangle), math.cos(tiltangle) 281997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis shape = [(t1*x+t0*y, -t0*x+t1*y) for (x, y) in shape] 282097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis shape = self._polytrafo(shape) 282197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis fc, oc = self._fillcolor, self._pencolor 282297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis screen._drawpoly(titem, shape, fill=fc, outline=oc, 282397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis width=w, top=True) 282497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis elif ttype == "image": 282597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis screen._drawimage(titem, self._position, tshape) 282697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis elif ttype == "compound": 282797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis lx, ly = self._stretchfactor 282897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis w = self._outlinewidth 282997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis for item, (poly, fc, oc) in zip(titem, tshape): 283097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis poly = [(lx*x, ly*y) for (x, y) in poly] 283197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis poly = self._polytrafo(poly) 283297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis screen._drawpoly(item, poly, fill=self._cc(fc), 283397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis outline=self._cc(oc), width=w, top=True) 283497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis else: 283597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if self._hidden_from_screen: 2836b241b67b8954b0679377af00d668e3dc92f4c858Guido van Rossum return 283797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if ttype == "polygon": 283897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis screen._drawpoly(titem, ((0, 0), (0, 0), (0, 0)), "", "") 283997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis elif ttype == "image": 284097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis screen._drawimage(titem, self._position, 284197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis screen._shapes["blank"]._data) 284297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis elif ttype == "compound": 284397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis for item in titem: 284497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis screen._drawpoly(item, ((0, 0), (0, 0), (0, 0)), "", "") 284597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._hidden_from_screen = True 284697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 284797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis############################## stamp stuff ############################### 284897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 284997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def stamp(self): 285097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """Stamp a copy of the turtleshape onto the canvas and return it's id. 285197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 285297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis No argument. 285397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 285497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Stamp a copy of the turtle shape onto the canvas at the current 285597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis turtle position. Return a stamp_id for that stamp, which can be 285697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis used to delete it by calling clearstamp(stamp_id). 285797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 285897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Example (for a Turtle instance named turtle): 285997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> turtle.color("blue") 286097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> turtle.stamp() 286197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 13 286297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> turtle.fd(50) 286397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ 286497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis screen = self.screen 286597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis shape = screen._shapes[self.turtle.shapeIndex] 286697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis ttype = shape._type 286797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis tshape = shape._data 286897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if ttype == "polygon": 286997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis stitem = screen._createpoly() 287097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if self._resizemode == "noresize": 287197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis w = 1 287297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis shape = tshape 287397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis else: 287497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if self._resizemode == "auto": 287597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis lx = ly = max(1, self._pensize/5.0) 287697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis w = self._pensize 287797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis tiltangle = 0 287897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis elif self._resizemode == "user": 287997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis lx, ly = self._stretchfactor 288097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis w = self._outlinewidth 288197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis tiltangle = self._tilt 288297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis shape = [(lx*x, ly*y) for (x, y) in tshape] 288397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis t0, t1 = math.sin(tiltangle), math.cos(tiltangle) 288497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis shape = [(t1*x+t0*y, -t0*x+t1*y) for (x, y) in shape] 288597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis shape = self._polytrafo(shape) 288697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis fc, oc = self._fillcolor, self._pencolor 288797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis screen._drawpoly(stitem, shape, fill=fc, outline=oc, 288897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis width=w, top=True) 288997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis elif ttype == "image": 289097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis stitem = screen._createimage("") 289197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis screen._drawimage(stitem, self._position, tshape) 289297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis elif ttype == "compound": 289397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis stitem = [] 289497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis for element in tshape: 289597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis item = screen._createpoly() 289697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis stitem.append(item) 289797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis stitem = tuple(stitem) 289897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis lx, ly = self._stretchfactor 289997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis w = self._outlinewidth 290097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis for item, (poly, fc, oc) in zip(stitem, tshape): 290197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis poly = [(lx*x, ly*y) for (x, y) in poly] 290297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis poly = self._polytrafo(poly) 290397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis screen._drawpoly(item, poly, fill=self._cc(fc), 290497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis outline=self._cc(oc), width=w, top=True) 290597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.stampItems.append(stitem) 290697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.undobuffer.push(("stamp", stitem)) 290797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis return stitem 290897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 290997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def _clearstamp(self, stampid): 291097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """does the work for clearstamp() and clearstamps() 291197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ 291297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if stampid in self.stampItems: 291397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if isinstance(stampid, tuple): 291497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis for subitem in stampid: 291597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.screen._delete(subitem) 291697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis else: 291797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.screen._delete(stampid) 291897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.stampItems.remove(stampid) 291997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis # Delete stampitem from undobuffer if necessary 292097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis # if clearstamp is called directly. 292197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis item = ("stamp", stampid) 292297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis buf = self.undobuffer 292397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if item not in buf.buffer: 292497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis return 292597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis index = buf.buffer.index(item) 292697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis buf.buffer.remove(item) 292797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if index <= buf.ptr: 292897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis buf.ptr = (buf.ptr - 1) % buf.bufsize 292997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis buf.buffer.insert((buf.ptr+1)%buf.bufsize, [None]) 293097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 293197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def clearstamp(self, stampid): 293297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """Delete stamp with given stampid 293397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 293497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Argument: 293597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis stampid - an integer, must be return value of previous stamp() call. 293697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 293797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Example (for a Turtle instance named turtle): 293897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> turtle.color("blue") 293997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> astamp = turtle.stamp() 294097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> turtle.fd(50) 294197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> turtle.clearstamp(astamp) 294297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ 294397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._clearstamp(stampid) 294497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._update() 294597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 294697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def clearstamps(self, n=None): 294797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """Delete all or first/last n of turtle's stamps. 294897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 294997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Optional argument: 295097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis n -- an integer 295197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 295297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis If n is None, delete all of pen's stamps, 295397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis else if n > 0 delete first n stamps 295497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis else if n < 0 delete last n stamps. 295597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 295697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Example (for a Turtle instance named turtle): 295797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> for i in range(8): 295897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis turtle.stamp(); turtle.fd(30) 295997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis ... 296097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> turtle.clearstamps(2) 296197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> turtle.clearstamps(-2) 296297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> turtle.clearstamps() 296397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ 296497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if n is None: 296597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis toDelete = self.stampItems[:] 296697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis elif n >= 0: 296797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis toDelete = self.stampItems[:n] 2968b241b67b8954b0679377af00d668e3dc92f4c858Guido van Rossum else: 296997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis toDelete = self.stampItems[n:] 297097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis for item in toDelete: 297197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._clearstamp(item) 297297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._update() 297397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 297497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def _goto(self, end): 297597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """Move the pen to the point end, thereby drawing a line 297697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if pen is down. All other methodes for turtle movement depend 297797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis on this one. 297897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ 297997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis ## Version mit undo-stuff 298097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis go_modes = ( self._drawing, 298197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._pencolor, 298297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._pensize, 298397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis isinstance(self._fillpath, list)) 298497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis screen = self.screen 298597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis undo_entry = ("go", self._position, end, go_modes, 298697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis (self.currentLineItem, 298797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.currentLine[:], 298897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis screen._pointlist(self.currentLineItem), 298997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.items[:]) 299097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis ) 299197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if self.undobuffer: 299297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.undobuffer.push(undo_entry) 299397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis start = self._position 299497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if self._speed and screen._tracing == 1: 299597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis diff = (end-start) 299697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis diffsq = (diff[0]*screen.xscale)**2 + (diff[1]*screen.yscale)**2 299797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis nhops = 1+int((diffsq**0.5)/(3*(1.1**self._speed)*self._speed)) 299897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis delta = diff * (1.0/nhops) 299997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis for n in range(1, nhops): 300097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if n == 1: 300197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis top = True 300297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis else: 300397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis top = False 300497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._position = start + delta * n 300597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if self._drawing: 300697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis screen._drawline(self.drawingLineItem, 300797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis (start, self._position), 300897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._pencolor, self._pensize, top) 300997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._update() 301097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if self._drawing: 301197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis screen._drawline(self.drawingLineItem, ((0, 0), (0, 0)), 301297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis fill="", width=self._pensize) 301397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis # Turtle now at end, 301497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if self._drawing: # now update currentLine 301597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.currentLine.append(end) 301697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if isinstance(self._fillpath, list): 301797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._fillpath.append(end) 301897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis ###### vererbung!!!!!!!!!!!!!!!!!!!!!! 301997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._position = end 302097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if self._creatingPoly: 302197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._poly.append(end) 302297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if len(self.currentLine) > 42: # 42! answer to the ultimate question 302397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis # of life, the universe and everything 302497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._newLine() 302597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._update() #count=True) 302697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 302797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def _undogoto(self, entry): 302897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """Reverse a _goto. Used for undo() 302997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ 303097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis old, new, go_modes, coodata = entry 303197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis drawing, pc, ps, filling = go_modes 303297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis cLI, cL, pl, items = coodata 303397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis screen = self.screen 303497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if abs(self._position - new) > 0.5: 303597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis print ("undogoto: HALLO-DA-STIMMT-WAS-NICHT!") 303697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis # restore former situation 303797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.currentLineItem = cLI 303897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.currentLine = cL 303997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 304097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if pl == [(0, 0), (0, 0)]: 304197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis usepc = "" 304297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis else: 304397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis usepc = pc 304497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis screen._drawline(cLI, pl, fill=usepc, width=ps) 304597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 304697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis todelete = [i for i in self.items if (i not in items) and 304797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis (screen._type(i) == "line")] 304897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis for i in todelete: 304997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis screen._delete(i) 305097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.items.remove(i) 305197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 305297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis start = old 305397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if self._speed and screen._tracing == 1: 305497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis diff = old - new 305597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis diffsq = (diff[0]*screen.xscale)**2 + (diff[1]*screen.yscale)**2 305697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis nhops = 1+int((diffsq**0.5)/(3*(1.1**self._speed)*self._speed)) 305797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis delta = diff * (1.0/nhops) 305897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis for n in range(1, nhops): 305997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if n == 1: 306097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis top = True 306197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis else: 306297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis top = False 306397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._position = new + delta * n 306497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if drawing: 306597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis screen._drawline(self.drawingLineItem, 306697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis (start, self._position), 306797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis pc, ps, top) 306897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._update() 306997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if drawing: 307097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis screen._drawline(self.drawingLineItem, ((0, 0), (0, 0)), 307197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis fill="", width=ps) 307297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis # Turtle now at position old, 307397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._position = old 307497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis ## if undo is done during crating a polygon, the last vertex 307597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis ## will be deleted. if the polygon is entirel deleted, 307697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis ## creatigPoly will be set to False. 307797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis ## Polygons created before the last one will not be affected by undo() 307897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if self._creatingPoly: 307997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if len(self._poly) > 0: 308097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._poly.pop() 308197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if self._poly == []: 308297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._creatingPoly = False 308397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._poly = None 308497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if filling: 308597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if self._fillpath == []: 308697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._fillpath = None 308797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis print("Unwahrscheinlich in _undogoto!") 308897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis elif self._fillpath is not None: 308997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._fillpath.pop() 309097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._update() #count=True) 309197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 309297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def _rotate(self, angle): 309397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """Turns pen clockwise by angle. 309497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ 309597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if self.undobuffer: 309697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.undobuffer.push(("rot", angle, self._degreesPerAU)) 309797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis angle *= self._degreesPerAU 309897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis neworient = self._orient.rotate(angle) 309997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis tracing = self.screen._tracing 310097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if tracing == 1 and self._speed > 0: 310197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis anglevel = 3.0 * self._speed 310297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis steps = 1 + int(abs(angle)/anglevel) 310397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis delta = 1.0*angle/steps 310497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis for _ in range(steps): 310597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._orient = self._orient.rotate(delta) 310697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._update() 310797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._orient = neworient 310897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._update() 310997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 311097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def _newLine(self, usePos=True): 311197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """Closes current line item and starts a new one. 311297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Remark: if current line became too long, animation 311397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis performance (via _drawline) slowed down considerably. 311497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ 311597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if len(self.currentLine) > 1: 311697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.screen._drawline(self.currentLineItem, self.currentLine, 311797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._pencolor, self._pensize) 311897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.currentLineItem = self.screen._createline() 311997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.items.append(self.currentLineItem) 312097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis else: 312197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.screen._drawline(self.currentLineItem, top=True) 312297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.currentLine = [] 312397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if usePos: 312497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.currentLine = [self._position] 3125477c8d5e70240744d24631b18341ad892c8a8e1cThomas Wouters 312697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def filling(self): 312797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """Return fillstate (True if filling, False else). 3128b241b67b8954b0679377af00d668e3dc92f4c858Guido van Rossum 312997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis No argument. 3130477c8d5e70240744d24631b18341ad892c8a8e1cThomas Wouters 313197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Example (for a Turtle instance named turtle): 313297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> turtle.begin_fill() 313397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> if turtle.filling(): 313497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis turtle.pensize(5) 313597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis else: 313697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis turtle.pensize(3) 313797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ 313897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis return isinstance(self._fillpath, list) 313997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 314097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis## def fill(self, flag=None): 314197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis## """Call fill(True) before drawing a shape to fill, fill(False) when done. 314297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis## 314397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis## Optional argument: 314497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis## flag -- True/False (or 1/0 respectively) 314597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis## 314697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis## Call fill(True) before drawing the shape you want to fill, 314797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis## and fill(False) when done. 314897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis## When used without argument: return fillstate (True if filling, 314997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis## False else) 315097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis## 315197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis## Example (for a Turtle instance named turtle): 315297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis## >>> turtle.fill(True) 315397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis## >>> turtle.forward(100) 315497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis## >>> turtle.left(90) 315597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis## >>> turtle.forward(100) 315697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis## >>> turtle.left(90) 315797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis## >>> turtle.forward(100) 315897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis## >>> turtle.left(90) 315997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis## >>> turtle.forward(100) 316097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis## >>> turtle.fill(False) 316197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis## """ 316297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis## filling = isinstance(self._fillpath, list) 316397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis## if flag is None: 316497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis## return filling 316597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis## screen = self.screen 316697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis## entry1 = entry2 = () 316797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis## if filling: 316897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis## if len(self._fillpath) > 2: 316997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis## self.screen._drawpoly(self._fillitem, self._fillpath, 317097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis## fill=self._fillcolor) 317197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis## entry1 = ("dofill", self._fillitem) 317297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis## if flag: 317397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis## self._fillitem = self.screen._createpoly() 317497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis## self.items.append(self._fillitem) 317597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis## self._fillpath = [self._position] 317697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis## entry2 = ("beginfill", self._fillitem) # , self._fillpath) 317797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis## self._newLine() 317897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis## else: 317997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis## self._fillitem = self._fillpath = None 318097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis## if self.undobuffer: 318197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis## if entry1 == (): 318297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis## if entry2 != (): 318397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis## self.undobuffer.push(entry2) 318497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis## else: 318597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis## if entry2 == (): 318697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis## self.undobuffer.push(entry1) 318797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis## else: 318897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis## self.undobuffer.push(["seq", entry1, entry2]) 318997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis## self._update() 3190b241b67b8954b0679377af00d668e3dc92f4c858Guido van Rossum 3191477c8d5e70240744d24631b18341ad892c8a8e1cThomas Wouters def begin_fill(self): 319297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """Called just before drawing a shape to be filled. 3193477c8d5e70240744d24631b18341ad892c8a8e1cThomas Wouters 319497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis No argument. 319597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 319697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Example (for a Turtle instance named turtle): 319797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> turtle.color("black", "red") 3198477c8d5e70240744d24631b18341ad892c8a8e1cThomas Wouters >>> turtle.begin_fill() 319997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> turtle.circle(60) 3200477c8d5e70240744d24631b18341ad892c8a8e1cThomas Wouters >>> turtle.end_fill() 3201477c8d5e70240744d24631b18341ad892c8a8e1cThomas Wouters """ 320297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if not self.filling(): 320397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._fillitem = self.screen._createpoly() 320497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.items.append(self._fillitem) 320597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._fillpath = [self._position] 320697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._newLine() 320797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if self.undobuffer: 320897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.undobuffer.push(("beginfill", self._fillitem)) 320997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._update() 321097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 3211477c8d5e70240744d24631b18341ad892c8a8e1cThomas Wouters 3212477c8d5e70240744d24631b18341ad892c8a8e1cThomas Wouters def end_fill(self): 321397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """Fill the shape drawn after the call begin_fill(). 3214477c8d5e70240744d24631b18341ad892c8a8e1cThomas Wouters 321597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis No argument. 321697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 321797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Example (for a Turtle instance named turtle): 321897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> turtle.color("black", "red") 3219477c8d5e70240744d24631b18341ad892c8a8e1cThomas Wouters >>> turtle.begin_fill() 322097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> turtle.circle(60) 3221477c8d5e70240744d24631b18341ad892c8a8e1cThomas Wouters >>> turtle.end_fill() 3222477c8d5e70240744d24631b18341ad892c8a8e1cThomas Wouters """ 322397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if self.filling(): 322497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if len(self._fillpath) > 2: 322597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.screen._drawpoly(self._fillitem, self._fillpath, 322697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis fill=self._fillcolor) 322797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if self.undobuffer: 322897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.undobuffer.push(("dofill", self._fillitem)) 322997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._fillitem = self._fillpath = None 323097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._update() 323197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 323297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def dot(self, size=None, *color): 323397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """Draw a dot with diameter size, using color. 323497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 323597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Optional argumentS: 323697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis size -- an integer >= 1 (if given) 323797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis color -- a colorstring or a numeric color tuple 323897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 323997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Draw a circular dot with diameter size, using color. 324097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis If size is not given, the maximum of pensize+4 and 2*pensize is used. 324197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 324297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Example (for a Turtle instance named turtle): 324397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> turtle.dot() 324497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> turtle.fd(50); turtle.dot(20, "blue"); turtle.fd(50) 3245477c8d5e70240744d24631b18341ad892c8a8e1cThomas Wouters """ 324697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis #print "dot-1:", size, color 324797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if not color: 324897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if isinstance(size, (str, tuple)): 324997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis color = self._colorstr(size) 325097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis size = self._pensize + max(self._pensize, 4) 325197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis else: 325297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis color = self._pencolor 325397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if not size: 325497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis size = self._pensize + max(self._pensize, 4) 325597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis else: 325697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if size is None: 325797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis size = self._pensize + max(self._pensize, 4) 325897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis color = self._colorstr(color) 325997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis #print "dot-2:", size, color 326097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if hasattr(self.screen, "_dot"): 326197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis item = self.screen._dot(self._position, size, color) 326297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis #print "dot:", size, color, "item:", item 326397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.items.append(item) 326497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if self.undobuffer: 326597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.undobuffer.push(("dot", item)) 326697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis else: 326797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis pen = self.pen() 326897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if self.undobuffer: 326997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.undobuffer.push(["seq"]) 327097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.undobuffer.cumulate = True 327197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis try: 327297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if self.resizemode() == 'auto': 327397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.ht() 327497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.pendown() 327597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.pensize(size) 327697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.pencolor(color) 327797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.forward(0) 327897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis finally: 327997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.pen(pen) 328097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if self.undobuffer: 328197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.undobuffer.cumulate = False 328297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 328397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def _write(self, txt, align, font): 328497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """Performs the writing for write() 328597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ 328697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis item, end = self.screen._write(self._position, txt, align, font, 328797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._pencolor) 328897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.items.append(item) 328997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if self.undobuffer: 329097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.undobuffer.push(("wri", item)) 329197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis return end 329297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 329397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def write(self, arg, move=False, align="left", font=("Arial", 8, "normal")): 329497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """Write text at the current turtle position. 329597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 329697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Arguments: 329797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis arg -- info, which is to be written to the TurtleScreen 329897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis move (optional) -- True/False 329997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis align (optional) -- one of the strings "left", "center" or right" 330097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis font (optional) -- a triple (fontname, fontsize, fonttype) 330197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 330297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Write text - the string representation of arg - at the current 330397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis turtle position according to align ("left", "center" or right") 330497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis and with the given font. 330597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis If move is True, the pen is moved to the bottom-right corner 330697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis of the text. By default, move is False. 3307477c8d5e70240744d24631b18341ad892c8a8e1cThomas Wouters 330897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Example (for a Turtle instance named turtle): 330997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> turtle.write('Home = ', True, align="center") 331097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> turtle.write((0,0), True) 3311477c8d5e70240744d24631b18341ad892c8a8e1cThomas Wouters """ 331297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if self.undobuffer: 331397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.undobuffer.push(["seq"]) 331497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.undobuffer.cumulate = True 331597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis end = self._write(str(arg), align.lower(), font) 331697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if move: 331797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis x, y = self.pos() 331897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.setpos(end, y) 331997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if self.undobuffer: 332097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.undobuffer.cumulate = False 3321fd2ede2aa84e77dac30e297a04fc49dd3bb61c68Guido van Rossum 332297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def begin_poly(self): 332397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """Start recording the vertices of a polygon. 3324477c8d5e70240744d24631b18341ad892c8a8e1cThomas Wouters 332597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis No argument. 3326477c8d5e70240744d24631b18341ad892c8a8e1cThomas Wouters 332797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Start recording the vertices of a polygon. Current turtle position 332897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis is first point of polygon. 3329477c8d5e70240744d24631b18341ad892c8a8e1cThomas Wouters 333097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Example (for a Turtle instance named turtle): 333197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> turtle.begin_poly() 3332477c8d5e70240744d24631b18341ad892c8a8e1cThomas Wouters """ 333397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._poly = [self._position] 333497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._creatingPoly = True 3335fd2ede2aa84e77dac30e297a04fc49dd3bb61c68Guido van Rossum 333697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def end_poly(self): 333797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """Stop recording the vertices of a polygon. 3338477c8d5e70240744d24631b18341ad892c8a8e1cThomas Wouters 333997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis No argument. 3340fd2ede2aa84e77dac30e297a04fc49dd3bb61c68Guido van Rossum 334197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Stop recording the vertices of a polygon. Current turtle position is 334297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis last point of polygon. This will be connected with the first point. 3343477c8d5e70240744d24631b18341ad892c8a8e1cThomas Wouters 334497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Example (for a Turtle instance named turtle): 334597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> turtle.end_poly() 3346477c8d5e70240744d24631b18341ad892c8a8e1cThomas Wouters """ 334797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._creatingPoly = False 3348fd2ede2aa84e77dac30e297a04fc49dd3bb61c68Guido van Rossum 334997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def get_poly(self): 335097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """Return the lastly recorded polygon. 3351477c8d5e70240744d24631b18341ad892c8a8e1cThomas Wouters 335297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis No argument. 335397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 335497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Example (for a Turtle instance named turtle): 335597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> p = turtle.get_poly() 335697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> turtle.register_shape("myFavouriteShape", p) 3357477c8d5e70240744d24631b18341ad892c8a8e1cThomas Wouters """ 335897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis ## check if there is any poly? -- 1st solution: 335997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if self._poly is not None: 336097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis return tuple(self._poly) 3361fd2ede2aa84e77dac30e297a04fc49dd3bb61c68Guido van Rossum 336297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def getscreen(self): 336397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """Return the TurtleScreen object, the turtle is drawing on. 3364477c8d5e70240744d24631b18341ad892c8a8e1cThomas Wouters 336597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis No argument. 3366fd2ede2aa84e77dac30e297a04fc49dd3bb61c68Guido van Rossum 336797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Return the TurtleScreen object, the turtle is drawing on. 336897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis So TurtleScreen-methods can be called for that object. 3369477c8d5e70240744d24631b18341ad892c8a8e1cThomas Wouters 337097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Example (for a Turtle instance named turtle): 337197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> ts = turtle.getscreen() 337297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> ts 337397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis <turtle.TurtleScreen object at 0x0106B770> 337497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> ts.bgcolor("pink") 3375477c8d5e70240744d24631b18341ad892c8a8e1cThomas Wouters """ 337697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis return self.screen 3377b241b67b8954b0679377af00d668e3dc92f4c858Guido van Rossum 337897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def getturtle(self): 337997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """Return the Turtleobject itself. 3380477c8d5e70240744d24631b18341ad892c8a8e1cThomas Wouters 338197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis No argument. 338297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 338397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Only reasonable use: as a function to return the 'anonymous turtle': 3384477c8d5e70240744d24631b18341ad892c8a8e1cThomas Wouters 3385477c8d5e70240744d24631b18341ad892c8a8e1cThomas Wouters Example: 338697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> pet = getturtle() 338797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> pet.fd(50) 338897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> pet 338997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis <turtle.Turtle object at 0x0187D810> 339097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> turtles() 339197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis [<turtle.Turtle object at 0x0187D810>] 3392477c8d5e70240744d24631b18341ad892c8a8e1cThomas Wouters """ 339397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis return self 339497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 339597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis getpen = getturtle 339697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 339797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 339897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis ################################################################ 339997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis ### screen oriented methods recurring to methods of TurtleScreen 340097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis ################################################################ 340197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 340297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis## def window_width(self): 340397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis## """ Returns the width of the turtle window. 340497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis## 340597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis## No argument. 340697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis## 340797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis## Example (for a TurtleScreen instance named screen): 340897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis## >>> screen.window_width() 340997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis## 640 341097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis## """ 341197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis## return self.screen._window_size()[0] 341297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis## 341397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis## def window_height(self): 341497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis## """ Return the height of the turtle window. 341597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis## 341697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis## No argument. 341797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis## 341897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis## Example (for a TurtleScreen instance named screen): 341997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis## >>> screen.window_height() 342097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis## 480 342197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis## """ 342297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis## return self.screen._window_size()[1] 342397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 342497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def _delay(self, delay=None): 342597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """Set delay value which determines speed of turtle animation. 342697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ 342797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis return self.screen.delay(delay) 3428477c8d5e70240744d24631b18341ad892c8a8e1cThomas Wouters 342997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis ##### event binding methods ##### 3430477c8d5e70240744d24631b18341ad892c8a8e1cThomas Wouters 343197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def onclick(self, fun, btn=1, add=None): 343297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """Bind fun to mouse-click event on this turtle on canvas. 3433477c8d5e70240744d24631b18341ad892c8a8e1cThomas Wouters 343497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Arguments: 343597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis fun -- a function with two arguments, to which will be assigned 343697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis the coordinates of the clicked point on the canvas. 343797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis num -- number of the mouse-button defaults to 1 (left mouse button). 343897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis add -- True or False. If True, new binding will be added, otherwise 343997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis it will replace a former binding. 3440477c8d5e70240744d24631b18341ad892c8a8e1cThomas Wouters 344197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Example for the anonymous turtle, i. e. the procedural way: 3442477c8d5e70240744d24631b18341ad892c8a8e1cThomas Wouters 344397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> def turn(x, y): 344497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis left(360) 3445477c8d5e70240744d24631b18341ad892c8a8e1cThomas Wouters 344697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> onclick(turn) # Now clicking into the turtle will turn it. 344797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> onclick(None) # event-binding will be removed 3448477c8d5e70240744d24631b18341ad892c8a8e1cThomas Wouters """ 344997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.screen._onclick(self.turtle._item, fun, btn, add) 345097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._update() 345197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 345297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def onrelease(self, fun, btn=1, add=None): 345397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """Bind fun to mouse-button-release event on this turtle on canvas. 345497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 345597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Arguments: 345697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis fun -- a function with two arguments, to which will be assigned 345797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis the coordinates of the clicked point on the canvas. 345897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis num -- number of the mouse-button defaults to 1 (left mouse button). 345997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 346097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Example (for a MyTurtle instance named joe): 346197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> class MyTurtle(Turtle): 346297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def glow(self,x,y): 346397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.fillcolor("red") 346497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def unglow(self,x,y): 346597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.fillcolor("") 346697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 346797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> joe = MyTurtle() 346897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> joe.onclick(joe.glow) 346997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> joe.onrelease(joe.unglow) 347097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis ### clicking on joe turns fillcolor red, 347197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis ### unclicking turns it to transparent. 347297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ 347397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.screen._onrelease(self.turtle._item, fun, btn, add) 347497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._update() 3475477c8d5e70240744d24631b18341ad892c8a8e1cThomas Wouters 347697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def ondrag(self, fun, btn=1, add=None): 347797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """Bind fun to mouse-move event on this turtle on canvas. 3478477c8d5e70240744d24631b18341ad892c8a8e1cThomas Wouters 347997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Arguments: 348097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis fun -- a function with two arguments, to which will be assigned 348197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis the coordinates of the clicked point on the canvas. 348297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis num -- number of the mouse-button defaults to 1 (left mouse button). 3483477c8d5e70240744d24631b18341ad892c8a8e1cThomas Wouters 348497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Every sequence of mouse-move-events on a turtle is preceded by a 348597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis mouse-click event on that turtle. 3486477c8d5e70240744d24631b18341ad892c8a8e1cThomas Wouters 348797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Example (for a Turtle instance named turtle): 348897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> turtle.ondrag(turtle.goto) 3489477c8d5e70240744d24631b18341ad892c8a8e1cThomas Wouters 349097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis ### Subsequently clicking and dragging a Turtle will 349197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis ### move it across the screen thereby producing handdrawings 349297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis ### (if pen is down). 349397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ 349497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.screen._ondrag(self.turtle._item, fun, btn, add) 3495477c8d5e70240744d24631b18341ad892c8a8e1cThomas Wouters 3496477c8d5e70240744d24631b18341ad892c8a8e1cThomas Wouters 349797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def _undo(self, action, data): 349897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """Does the main part of the work for undo() 3499477c8d5e70240744d24631b18341ad892c8a8e1cThomas Wouters """ 350097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if self.undobuffer is None: 35013c7a25a4d9ea07f2aa8e4b70fecd84e33de82c2eGuido van Rossum return 350297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if action == "rot": 350397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis angle, degPAU = data 350497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._rotate(-angle*degPAU/self._degreesPerAU) 350597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis dummy = self.undobuffer.pop() 350697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis elif action == "stamp": 350797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis stitem = data[0] 350897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.clearstamp(stitem) 350997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis elif action == "go": 351097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._undogoto(data) 351197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis elif action in ["wri", "dot"]: 351297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis item = data[0] 351397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.screen._delete(item) 351497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.items.remove(item) 351597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis elif action == "dofill": 351697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis item = data[0] 351797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.screen._drawpoly(item, ((0, 0),(0, 0),(0, 0)), 351897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis fill="", outline="") 351997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis elif action == "beginfill": 352097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis item = data[0] 352197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._fillitem = self._fillpath = None 352297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if item in self.items: 352397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.screen._delete(item) 352497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.items.remove(item) 352597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis elif action == "pen": 352697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis TPen.pen(self, data[0]) 352797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.undobuffer.pop() 352897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 352997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def undo(self): 353097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """undo (repeatedly) the last turtle action. 353197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 353297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis No argument. 353397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 353497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis undo (repeatedly) the last turtle action. 353597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Number of available undo actions is determined by the size of 353697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis the undobuffer. 353797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 353897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Example (for a Turtle instance named turtle): 353997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> for i in range(4): 354097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis turtle.fd(50); turtle.lt(80) 354197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 354297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> for i in range(8): 354397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis turtle.undo() 354497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ 354597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if self.undobuffer is None: 354697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis return 354797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis item = self.undobuffer.pop() 354897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis action = item[0] 354997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis data = item[1:] 355097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if action == "seq": 355197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis while data: 355297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis item = data.pop() 355397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._undo(item[0], item[1:]) 355497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis else: 355597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._undo(action, data) 3556b241b67b8954b0679377af00d668e3dc92f4c858Guido van Rossum 355797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis turtlesize = shapesize 3558477c8d5e70240744d24631b18341ad892c8a8e1cThomas Wouters 355997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. LöwisRawPen = RawTurtle 3560477c8d5e70240744d24631b18341ad892c8a8e1cThomas Wouters 3561601149bb8e4fd200cc5b0b670f34c608c64e0783Martin v. Löwis### Screen - Singleton ######################## 3562477c8d5e70240744d24631b18341ad892c8a8e1cThomas Wouters 3563601149bb8e4fd200cc5b0b670f34c608c64e0783Martin v. Löwisdef Screen(): 3564601149bb8e4fd200cc5b0b670f34c608c64e0783Martin v. Löwis """Return the singleton screen object. 3565601149bb8e4fd200cc5b0b670f34c608c64e0783Martin v. Löwis If none exists at the moment, create a new one and return it, 3566601149bb8e4fd200cc5b0b670f34c608c64e0783Martin v. Löwis else return the existing one.""" 3567601149bb8e4fd200cc5b0b670f34c608c64e0783Martin v. Löwis if Turtle._screen is None: 3568601149bb8e4fd200cc5b0b670f34c608c64e0783Martin v. Löwis Turtle._screen = _Screen() 3569601149bb8e4fd200cc5b0b670f34c608c64e0783Martin v. Löwis return Turtle._screen 3570601149bb8e4fd200cc5b0b670f34c608c64e0783Martin v. Löwis 3571601149bb8e4fd200cc5b0b670f34c608c64e0783Martin v. Löwisclass _Screen(TurtleScreen): 3572b241b67b8954b0679377af00d668e3dc92f4c858Guido van Rossum 357397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis _root = None 357497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis _canvas = None 357597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis _title = _CFG["title"] 3576d038ca830fd5a6bafbd24b70eb71aeae83d362d3Fred Drake 357797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def __init__(self): 3578601149bb8e4fd200cc5b0b670f34c608c64e0783Martin v. Löwis # XXX there is no need for this code to be conditional, 3579601149bb8e4fd200cc5b0b670f34c608c64e0783Martin v. Löwis # as there will be only a single _Screen instance, anyway 3580601149bb8e4fd200cc5b0b670f34c608c64e0783Martin v. Löwis # XXX actually, the turtle demo is injecting root window, 3581601149bb8e4fd200cc5b0b670f34c608c64e0783Martin v. Löwis # so perhaps the conditional creation of a root should be 3582601149bb8e4fd200cc5b0b670f34c608c64e0783Martin v. Löwis # preserved (perhaps by passing it as an optional parameter) 3583601149bb8e4fd200cc5b0b670f34c608c64e0783Martin v. Löwis if _Screen._root is None: 3584601149bb8e4fd200cc5b0b670f34c608c64e0783Martin v. Löwis _Screen._root = self._root = _Root() 3585601149bb8e4fd200cc5b0b670f34c608c64e0783Martin v. Löwis self._root.title(_Screen._title) 358697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._root.ondestroy(self._destroy) 3587601149bb8e4fd200cc5b0b670f34c608c64e0783Martin v. Löwis if _Screen._canvas is None: 358897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis width = _CFG["width"] 358997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis height = _CFG["height"] 359097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis canvwidth = _CFG["canvwidth"] 359197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis canvheight = _CFG["canvheight"] 359297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis leftright = _CFG["leftright"] 359397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis topbottom = _CFG["topbottom"] 359497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._root.setupcanvas(width, height, canvwidth, canvheight) 3595601149bb8e4fd200cc5b0b670f34c608c64e0783Martin v. Löwis _Screen._canvas = self._root._getcanvas() 359697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.setup(width, height, leftright, topbottom) 3597601149bb8e4fd200cc5b0b670f34c608c64e0783Martin v. Löwis TurtleScreen.__init__(self, _Screen._canvas) 359897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 359997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def setup(self, width=_CFG["width"], height=_CFG["height"], 360097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis startx=_CFG["leftright"], starty=_CFG["topbottom"]): 360197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ Set the size and position of the main window. 360297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 360397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Arguments: 360497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis width: as integer a size in pixels, as float a fraction of the screen. 360597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Default is 50% of screen. 360697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis height: as integer the height in pixels, as float a fraction of the 360797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis screen. Default is 75% of screen. 360897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis startx: if positive, starting position in pixels from the left 360997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis edge of the screen, if negative from the right edge 361097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Default, startx=None is to center window horizontally. 361197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis starty: if positive, starting position in pixels from the top 361297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis edge of the screen, if negative from the bottom edge 361397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Default, starty=None is to center window vertically. 361497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 361597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Examples (for a Screen instance named screen): 361697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> screen.setup (width=200, height=200, startx=0, starty=0) 361797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 361897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis sets window to 200x200 pixels, in upper left of screen 361997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 362097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> screen.setup(width=.75, height=0.5, startx=None, starty=None) 362197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 362297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis sets window to 75% of screen by 50% of screen and centers 362397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ 362497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if not hasattr(self._root, "set_geometry"): 362597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis return 362697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis sw = self._root.win_width() 362797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis sh = self._root.win_height() 362897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if isinstance(width, float) and 0 <= width <= 1: 362997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis width = sw*width 363097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if startx is None: 363197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis startx = (sw - width) / 2 363297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if isinstance(height, float) and 0 <= height <= 1: 363397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis height = sh*height 363497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if starty is None: 363597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis starty = (sh - height) / 2 363697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._root.set_geometry(width, height, startx, starty) 363797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 363897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def title(self, titlestring): 363997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """Set title of turtle-window 364097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 364197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Argument: 364297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis titlestring -- a string, to appear in the titlebar of the 364397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis turtle graphics window. 364497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 364597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis This is a method of Screen-class. Not available for TurtleScreen- 364697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis objects. 364797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 364897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Example (for a Screen instance named screen): 364997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> screen.title("Welcome to the turtle-zoo!") 365097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ 3651601149bb8e4fd200cc5b0b670f34c608c64e0783Martin v. Löwis if _Screen._root is not None: 3652601149bb8e4fd200cc5b0b670f34c608c64e0783Martin v. Löwis _Screen._root.title(titlestring) 3653601149bb8e4fd200cc5b0b670f34c608c64e0783Martin v. Löwis _Screen._title = titlestring 3654477c8d5e70240744d24631b18341ad892c8a8e1cThomas Wouters 365597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def _destroy(self): 365697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis root = self._root 3657601149bb8e4fd200cc5b0b670f34c608c64e0783Martin v. Löwis if root is _Screen._root: 365897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Turtle._pen = None 365997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Turtle._screen = None 3660601149bb8e4fd200cc5b0b670f34c608c64e0783Martin v. Löwis _Screen._root = None 3661601149bb8e4fd200cc5b0b670f34c608c64e0783Martin v. Löwis _Screen._canvas = None 366297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis TurtleScreen._RUNNING = True 366397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis root.destroy() 3664477c8d5e70240744d24631b18341ad892c8a8e1cThomas Wouters 366597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def bye(self): 366697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """Shut the turtlegraphics window. 3667477c8d5e70240744d24631b18341ad892c8a8e1cThomas Wouters 366897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Example (for a TurtleScreen instance named screen): 366997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> screen.bye() 367097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ 367197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self._destroy() 3672477c8d5e70240744d24631b18341ad892c8a8e1cThomas Wouters 367397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def exitonclick(self): 367497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """Go into mainloop until the mouse is clicked. 3675477c8d5e70240744d24631b18341ad892c8a8e1cThomas Wouters 367697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis No arguments. 3677477c8d5e70240744d24631b18341ad892c8a8e1cThomas Wouters 367897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Bind bye() method to mouseclick on TurtleScreen. 367997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis If "using_IDLE" - value in configuration dictionary is False 368097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis (default value), enter mainloop. 368197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis If IDLE with -n switch (no subprocess) is used, this value should be 368297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis set to True in turtle.cfg. In this case IDLE's mainloop 368397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis is active also for the client script. 3684477c8d5e70240744d24631b18341ad892c8a8e1cThomas Wouters 368597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis This is a method of the Screen-class and not available for 368697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis TurtleScreen instances. 3687477c8d5e70240744d24631b18341ad892c8a8e1cThomas Wouters 368897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Example (for a Screen instance named screen): 368997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis >>> screen.exitonclick() 3690477c8d5e70240744d24631b18341ad892c8a8e1cThomas Wouters 369197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ 369297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def exitGracefully(x, y): 369397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """Screen.bye() with two dummy-parameters""" 369497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.bye() 369597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis self.onclick(exitGracefully) 369697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if _CFG["using_IDLE"]: 369797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis return 369897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis try: 369997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis mainloop() 370097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis except AttributeError: 370197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis exit(0) 3702477c8d5e70240744d24631b18341ad892c8a8e1cThomas Wouters 3703477c8d5e70240744d24631b18341ad892c8a8e1cThomas Wouters 370497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwisclass Turtle(RawTurtle): 370597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """RawTurtle auto-crating (scrolled) canvas. 3706477c8d5e70240744d24631b18341ad892c8a8e1cThomas Wouters 370797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis When a Turtle object is created or a function derived from some 370897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Turtle method is called a TurtleScreen object is automatically created. 370997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ 371097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis _pen = None 371197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis _screen = None 371297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 371397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def __init__(self, 371497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis shape=_CFG["shape"], 371597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis undobuffersize=_CFG["undobuffersize"], 371697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis visible=_CFG["visible"]): 371797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if Turtle._screen is None: 371897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Turtle._screen = Screen() 371997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis RawTurtle.__init__(self, Turtle._screen, 372097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis shape=shape, 372197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis undobuffersize=undobuffersize, 372297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis visible=visible) 372397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 372497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. LöwisPen = Turtle 3725477c8d5e70240744d24631b18341ad892c8a8e1cThomas Wouters 372697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwisdef _getpen(): 372797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """Create the 'anonymous' turtle if not already present.""" 372897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if Turtle._pen is None: 372997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Turtle._pen = Turtle() 373097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis return Turtle._pen 373197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 373297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwisdef _getscreen(): 373397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """Create a TurtleScreen if not already present.""" 373497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if Turtle._screen is None: 373597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Turtle._screen = Screen() 373697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis return Turtle._screen 373797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 373897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwisdef write_docstringdict(filename="turtle_docstringdict"): 373997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """Create and write docstring-dictionary to file. 374097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 374197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Optional argument: 374297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis filename -- a string, used as filename 374397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis default value is turtle_docstringdict 374497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 374597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Has to be called explicitely, (not used by the turtle-graphics classes) 374697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis The docstring dictionary will be written to the Python script <filname>.py 374797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis It is intended to serve as a template for translation of the docstrings 374897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis into different languages. 374997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ 375097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis docsdict = {} 375197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 375297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis for methodname in _tg_screen_functions: 3753601149bb8e4fd200cc5b0b670f34c608c64e0783Martin v. Löwis key = "_Screen."+methodname 375497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis docsdict[key] = eval(key).__doc__ 375597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis for methodname in _tg_turtle_functions: 375697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis key = "Turtle."+methodname 375797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis docsdict[key] = eval(key).__doc__ 375897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 375997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis f = open("%s.py" % filename,"w") 376097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis keys = sorted([x for x in docsdict.keys() 376197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if x.split('.')[1] not in _alias_list]) 376297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis f.write('docsdict = {\n\n') 376397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis for key in keys[:-1]: 376497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis f.write('%s :\n' % repr(key)) 376597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis f.write(' """%s\n""",\n\n' % docsdict[key]) 376697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis key = keys[-1] 376797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis f.write('%s :\n' % repr(key)) 376897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis f.write(' """%s\n"""\n\n' % docsdict[key]) 376997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis f.write("}\n") 377097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis f.close() 377197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 377297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwisdef read_docstrings(lang): 377397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """Read in docstrings from lang-specific docstring dictionary. 377497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 377597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis Transfer docstrings, translated to lang, from a dictionary-file 377697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis to the methods of classes Screen and Turtle and - in revised form - 377797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis to the corresponding functions. 377897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ 377997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis modname = "turtle_docstringdict_%(language)s" % {'language':lang.lower()} 378097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis module = __import__(modname) 378197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis docsdict = module.docsdict 378297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis for key in docsdict: 378397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis try: 378497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis# eval(key).im_func.__doc__ = docsdict[key] 378597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis eval(key).__doc__ = docsdict[key] 378697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis except: 378797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis print("Bad docstring-entry: %s" % key) 378897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 378997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis_LANGUAGE = _CFG["language"] 379097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 379197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwistry: 379297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if _LANGUAGE != "english": 379397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis read_docstrings(_LANGUAGE) 379497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwisexcept ImportError: 379597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis print("Cannot find docsdict for", _LANGUAGE) 379697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwisexcept: 379797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis print ("Unknown Error when trying to import %s-docstring-dictionary" % 379897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis _LANGUAGE) 379997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 380097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 380197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwisdef getmethparlist(ob): 380297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis "Get strings describing the arguments for the given object" 380397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis argText1 = argText2 = "" 380497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis # bit of a hack for methods - turn it into a function 380597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis # but we drop the "self" param. 380697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis## if type(ob)==types.MethodType: 380797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis## fob = ob.im_func 380897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis## argOffset = 1 380997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis## else: 381097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis## fob = ob 381197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis## argOffset = 0 381297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis # Try and build one for Python defined functions 381397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis argOffset = 1 381497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis## if type(fob) in [types.FunctionType, types.LambdaType]: 381597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis## try: 381697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis counter = ob.__code__.co_argcount 381797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis items2 = list(ob.__code__.co_varnames[argOffset:counter]) 381897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis realArgs = ob.__code__.co_varnames[argOffset:counter] 381997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis defaults = ob.__defaults__ or [] 382097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis defaults = list(map(lambda name: "=%s" % repr(name), defaults)) 382197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis defaults = [""] * (len(realArgs)-len(defaults)) + defaults 382297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis items1 = list(map(lambda arg, dflt: arg+dflt, realArgs, defaults)) 382397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if ob.__code__.co_flags & 0x4: 382497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis items1.append("*"+ob.__code__.co_varnames[counter]) 382597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis items2.append("*"+ob.__code__.co_varnames[counter]) 382697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis counter += 1 382797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if ob.__code__.co_flags & 0x8: 382897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis items1.append("**"+ob.__code__.co_varnames[counter]) 382997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis items2.append("**"+ob.__code__.co_varnames[counter]) 383097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis argText1 = ", ".join(items1) 383197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis argText1 = "(%s)" % argText1 383297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis argText2 = ", ".join(items2) 383397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis argText2 = "(%s)" % argText2 383497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis## except: 383597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis## pass 383697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis return argText1, argText2 383797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 383897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwisdef _turtle_docrevise(docstr): 383997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """To reduce docstrings from RawTurtle class for functions 3840477c8d5e70240744d24631b18341ad892c8a8e1cThomas Wouters """ 384197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis import re 384297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if docstr is None: 384397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis return None 384497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis turtlename = _CFG["exampleturtle"] 384597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis newdocstr = docstr.replace("%s." % turtlename,"") 384697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis parexp = re.compile(r' \(.+ %s\):' % turtlename) 384797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis newdocstr = parexp.sub(":", newdocstr) 384897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis return newdocstr 384997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 385097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwisdef _screen_docrevise(docstr): 385197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """To reduce docstrings from TurtleScreen class for functions 385297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """ 385397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis import re 385497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if docstr is None: 385597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis return None 385697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis screenname = _CFG["examplescreen"] 385797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis newdocstr = docstr.replace("%s." % screenname,"") 385897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis parexp = re.compile(r' \(.+ %s\):' % screenname) 385997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis newdocstr = parexp.sub(":", newdocstr) 386097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis return newdocstr 386197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 386297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis## The following mechanism makes all methods of RawTurtle and Turtle available 386397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis## as functions. So we can enhance, change, add, delete methods to these 386497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis## classes and do not need to change anything here. 386597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 386697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 386797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwisfor methodname in _tg_screen_functions: 3868601149bb8e4fd200cc5b0b670f34c608c64e0783Martin v. Löwis pl1, pl2 = getmethparlist(eval('_Screen.' + methodname)) 386997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if pl1 == "": 387097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis print(">>>>>>", pl1, pl2) 387197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis continue 387297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis defstr = ("def %(key)s%(pl1)s: return _getscreen().%(key)s%(pl2)s" % 387397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis {'key':methodname, 'pl1':pl1, 'pl2':pl2}) 387497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis## print("Screen:", defstr) 387597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis exec(defstr) 3876601149bb8e4fd200cc5b0b670f34c608c64e0783Martin v. Löwis eval(methodname).__doc__ = _screen_docrevise(eval('_Screen.'+methodname).__doc__) 387797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 387897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwisfor methodname in _tg_turtle_functions: 387997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis pl1, pl2 = getmethparlist(eval('Turtle.' + methodname)) 388097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if pl1 == "": 388197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis print(">>>>>>", pl1, pl2) 388297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis continue 388397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis defstr = ("def %(key)s%(pl1)s: return _getpen().%(key)s%(pl2)s" % 388497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis {'key':methodname, 'pl1':pl1, 'pl2':pl2}) 388597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis## print("Turtle:", defstr) 388697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis exec(defstr) 388797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis eval(methodname).__doc__ = _turtle_docrevise(eval('Turtle.'+methodname).__doc__) 388897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 388997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 389097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwisdone = mainloop = TK.mainloop 389197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis#del pl1, pl2, defstr 389297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 389397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwisif __name__ == "__main__": 389497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def switchpen(): 389597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if isdown(): 389697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis pu() 389797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis else: 389897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis pd() 3899477c8d5e70240744d24631b18341ad892c8a8e1cThomas Wouters 390097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def demo1(): 390197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """Demo of old turtle.py - module""" 390297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis reset() 390397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis tracer(True) 3904b241b67b8954b0679377af00d668e3dc92f4c858Guido van Rossum up() 390597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis backward(100) 3906b241b67b8954b0679377af00d668e3dc92f4c858Guido van Rossum down() 390797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis # draw 3 squares; the last filled 390897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis width(3) 390997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis for i in range(3): 391097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if i == 2: 391197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis begin_fill() 391297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis for _ in range(4): 391397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis forward(20) 391497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis left(90) 391597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if i == 2: 391697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis color("maroon") 391797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis end_fill() 3918477c8d5e70240744d24631b18341ad892c8a8e1cThomas Wouters up() 391997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis forward(30) 3920477c8d5e70240744d24631b18341ad892c8a8e1cThomas Wouters down() 392197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis width(1) 392297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis color("black") 392397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis # move out of the way 392497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis tracer(False) 392597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis up() 39260e3f591aeeef9ed715f8770320f4c4c7332a8794Thomas Wouters right(90) 392797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis forward(100) 39280e3f591aeeef9ed715f8770320f4c4c7332a8794Thomas Wouters right(90) 392997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis forward(100) 393097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis right(180) 393197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis down() 393297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis # some text 393397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis write("startstart", 1) 393497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis write("start", 1) 393597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis color("red") 393697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis # staircase 393797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis for i in range(5): 393897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis forward(20) 393997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis left(90) 394097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis forward(20) 394197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis right(90) 394297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis # filled staircase 394397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis tracer(True) 394497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis begin_fill() 394597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis for i in range(5): 394697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis forward(20) 394797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis left(90) 394897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis forward(20) 394997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis right(90) 395097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis end_fill() 395197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis # more text 395297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 395397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def demo2(): 395497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis """Demo of some new features.""" 395597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis speed(1) 395697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis st() 395797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis pensize(3) 395897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis setheading(towards(0, 0)) 395997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis radius = distance(0, 0)/2.0 396097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis rt(90) 396197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis for _ in range(18): 396297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis switchpen() 396397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis circle(radius, 10) 396497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis write("wait a moment...") 396597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis while undobufferentries(): 396697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis undo() 396797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis reset() 396897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis lt(90) 396997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis colormode(255) 397097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis laenge = 10 397197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis pencolor("green") 397297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis pensize(3) 397397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis lt(180) 397497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis for i in range(-2, 16): 397597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if i > 0: 397697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis begin_fill() 397797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis fillcolor(255-15*i, 0, 15*i) 397897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis for _ in range(3): 397997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis fd(laenge) 398097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis lt(120) 398197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis end_fill() 398297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis laenge += 10 398397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis lt(15) 398497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis speed((speed()+1)%12) 398597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis #end_fill() 398697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 398797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis lt(120) 398897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis pu() 398997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis fd(70) 399097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis rt(30) 399197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis pd() 399297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis color("red","yellow") 399397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis speed(0) 399497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis begin_fill() 399597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis for _ in range(4): 399697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis circle(50, 90) 399797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis rt(90) 399897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis fd(30) 399997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis rt(90) 400097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis end_fill() 400197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis lt(90) 400297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis pu() 400397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis fd(30) 400497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis pd() 400597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis shape("turtle") 400697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 400797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis tri = getturtle() 400897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis tri.resizemode("auto") 400997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis turtle = Turtle() 401097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis turtle.resizemode("auto") 401197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis turtle.shape("turtle") 401297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis turtle.reset() 401397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis turtle.left(90) 401497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis turtle.speed(0) 401597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis turtle.up() 401697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis turtle.goto(280, 40) 401797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis turtle.lt(30) 401897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis turtle.down() 401997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis turtle.speed(6) 402097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis turtle.color("blue","orange") 402197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis turtle.pensize(2) 402297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis tri.speed(6) 4023477c8d5e70240744d24631b18341ad892c8a8e1cThomas Wouters setheading(towards(turtle)) 402497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis count = 1 402597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis while tri.distance(turtle) > 4: 402697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis turtle.fd(3.5) 402797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis turtle.lt(0.6) 402897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis tri.setheading(tri.towards(turtle)) 402997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis tri.fd(4) 403097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis if count % 20 == 0: 403197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis turtle.stamp() 403297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis tri.stamp() 403397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis switchpen() 403497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis count += 1 403597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis tri.write("CAUGHT! ", font=("Arial", 16, "bold"), align="right") 403697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis tri.pencolor("black") 403797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis tri.pencolor("red") 403897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 403997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis def baba(xdummy, ydummy): 404097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis clearscreen() 404197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis bye() 404297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 404397cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis time.sleep(2) 404497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 404597cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis while undobufferentries(): 404697cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis tri.undo() 404797cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis turtle.undo() 404897cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis tri.fd(50) 404997cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis tri.write(" Click me!", font = ("Courier", 12, "bold") ) 405097cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis tri.onclick(baba, 1) 405197cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis 405297cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis demo1() 4053477c8d5e70240744d24631b18341ad892c8a8e1cThomas Wouters demo2() 405497cf99fc7e8c56868711f7c91c3b1e37e4c66cbaMartin v. Löwis exitonclick() 4055