1================================= 2:mod:`turtle` --- Turtle graphics 3================================= 4 5.. module:: turtle 6 :synopsis: An educational framework for simple graphics applications 7 8.. sectionauthor:: Gregor Lingl <gregor.lingl@aon.at> 9 10**Source code:** :source:`Lib/turtle.py` 11 12.. testsetup:: default 13 14 from turtle import * 15 turtle = Turtle() 16 17-------------- 18 19Introduction 20============ 21 22Turtle graphics is a popular way for introducing programming to kids. It was 23part of the original Logo programming language developed by Wally Feurzeig, 24Seymour Papert and Cynthia Solomon in 1967. 25 26Imagine a robotic turtle starting at (0, 0) in the x-y plane. After an ``import turtle``, give it the 27command ``turtle.forward(15)``, and it moves (on-screen!) 15 pixels in the 28direction it is facing, drawing a line as it moves. Give it the command 29``turtle.right(25)``, and it rotates in-place 25 degrees clockwise. 30 31.. sidebar:: Turtle star 32 33 Turtle can draw intricate shapes using programs that repeat simple 34 moves. 35 36 .. image:: turtle-star.* 37 :align: center 38 39 .. literalinclude:: ../includes/turtle-star.py 40 41By combining together these and similar commands, intricate shapes and pictures 42can easily be drawn. 43 44The :mod:`turtle` module is an extended reimplementation of the same-named 45module from the Python standard distribution up to version Python 2.5. 46 47It tries to keep the merits of the old turtle module and to be (nearly) 100% 48compatible with it. This means in the first place to enable the learning 49programmer to use all the commands, classes and methods interactively when using 50the module from within IDLE run with the ``-n`` switch. 51 52The turtle module provides turtle graphics primitives, in both object-oriented 53and procedure-oriented ways. Because it uses :mod:`tkinter` for the underlying 54graphics, it needs a version of Python installed with Tk support. 55 56The object-oriented interface uses essentially two+two classes: 57 581. The :class:`TurtleScreen` class defines graphics windows as a playground for 59 the drawing turtles. Its constructor needs a :class:`tkinter.Canvas` or a 60 :class:`ScrolledCanvas` as argument. It should be used when :mod:`turtle` is 61 used as part of some application. 62 63 The function :func:`Screen` returns a singleton object of a 64 :class:`TurtleScreen` subclass. This function should be used when 65 :mod:`turtle` is used as a standalone tool for doing graphics. 66 As a singleton object, inheriting from its class is not possible. 67 68 All methods of TurtleScreen/Screen also exist as functions, i.e. as part of 69 the procedure-oriented interface. 70 712. :class:`RawTurtle` (alias: :class:`RawPen`) defines Turtle objects which draw 72 on a :class:`TurtleScreen`. Its constructor needs a Canvas, ScrolledCanvas 73 or TurtleScreen as argument, so the RawTurtle objects know where to draw. 74 75 Derived from RawTurtle is the subclass :class:`Turtle` (alias: :class:`Pen`), 76 which draws on "the" :class:`Screen` instance which is automatically 77 created, if not already present. 78 79 All methods of RawTurtle/Turtle also exist as functions, i.e. part of the 80 procedure-oriented interface. 81 82The procedural interface provides functions which are derived from the methods 83of the classes :class:`Screen` and :class:`Turtle`. They have the same names as 84the corresponding methods. A screen object is automatically created whenever a 85function derived from a Screen method is called. An (unnamed) turtle object is 86automatically created whenever any of the functions derived from a Turtle method 87is called. 88 89To use multiple turtles on a screen one has to use the object-oriented interface. 90 91.. note:: 92 In the following documentation the argument list for functions is given. 93 Methods, of course, have the additional first argument *self* which is 94 omitted here. 95 96 97Overview of available Turtle and Screen methods 98================================================= 99 100Turtle methods 101-------------- 102 103Turtle motion 104 Move and draw 105 | :func:`forward` | :func:`fd` 106 | :func:`backward` | :func:`bk` | :func:`back` 107 | :func:`right` | :func:`rt` 108 | :func:`left` | :func:`lt` 109 | :func:`goto` | :func:`setpos` | :func:`setposition` 110 | :func:`setx` 111 | :func:`sety` 112 | :func:`setheading` | :func:`seth` 113 | :func:`home` 114 | :func:`circle` 115 | :func:`dot` 116 | :func:`stamp` 117 | :func:`clearstamp` 118 | :func:`clearstamps` 119 | :func:`undo` 120 | :func:`speed` 121 122 Tell Turtle's state 123 | :func:`position` | :func:`pos` 124 | :func:`towards` 125 | :func:`xcor` 126 | :func:`ycor` 127 | :func:`heading` 128 | :func:`distance` 129 130 Setting and measurement 131 | :func:`degrees` 132 | :func:`radians` 133 134Pen control 135 Drawing state 136 | :func:`pendown` | :func:`pd` | :func:`down` 137 | :func:`penup` | :func:`pu` | :func:`up` 138 | :func:`pensize` | :func:`width` 139 | :func:`pen` 140 | :func:`isdown` 141 142 Color control 143 | :func:`color` 144 | :func:`pencolor` 145 | :func:`fillcolor` 146 147 Filling 148 | :func:`filling` 149 | :func:`begin_fill` 150 | :func:`end_fill` 151 152 More drawing control 153 | :func:`reset` 154 | :func:`clear` 155 | :func:`write` 156 157Turtle state 158 Visibility 159 | :func:`showturtle` | :func:`st` 160 | :func:`hideturtle` | :func:`ht` 161 | :func:`isvisible` 162 163 Appearance 164 | :func:`shape` 165 | :func:`resizemode` 166 | :func:`shapesize` | :func:`turtlesize` 167 | :func:`shearfactor` 168 | :func:`settiltangle` 169 | :func:`tiltangle` 170 | :func:`tilt` 171 | :func:`shapetransform` 172 | :func:`get_shapepoly` 173 174Using events 175 | :func:`onclick` 176 | :func:`onrelease` 177 | :func:`ondrag` 178 179Special Turtle methods 180 | :func:`begin_poly` 181 | :func:`end_poly` 182 | :func:`get_poly` 183 | :func:`clone` 184 | :func:`getturtle` | :func:`getpen` 185 | :func:`getscreen` 186 | :func:`setundobuffer` 187 | :func:`undobufferentries` 188 189 190Methods of TurtleScreen/Screen 191------------------------------ 192 193Window control 194 | :func:`bgcolor` 195 | :func:`bgpic` 196 | :func:`clearscreen` 197 | :func:`resetscreen` 198 | :func:`screensize` 199 | :func:`setworldcoordinates` 200 201Animation control 202 | :func:`delay` 203 | :func:`tracer` 204 | :func:`update` 205 206Using screen events 207 | :func:`listen` 208 | :func:`onkey` | :func:`onkeyrelease` 209 | :func:`onkeypress` 210 | :func:`onclick` | :func:`onscreenclick` 211 | :func:`ontimer` 212 | :func:`mainloop` | :func:`done` 213 214Settings and special methods 215 | :func:`mode` 216 | :func:`colormode` 217 | :func:`getcanvas` 218 | :func:`getshapes` 219 | :func:`register_shape` | :func:`addshape` 220 | :func:`turtles` 221 | :func:`window_height` 222 | :func:`window_width` 223 224Input methods 225 | :func:`textinput` 226 | :func:`numinput` 227 228Methods specific to Screen 229 | :func:`bye` 230 | :func:`exitonclick` 231 | :func:`setup` 232 | :func:`title` 233 234 235Methods of RawTurtle/Turtle and corresponding functions 236======================================================= 237 238Most of the examples in this section refer to a Turtle instance called 239``turtle``. 240 241Turtle motion 242------------- 243 244.. function:: forward(distance) 245 fd(distance) 246 247 :param distance: a number (integer or float) 248 249 Move the turtle forward by the specified *distance*, in the direction the 250 turtle is headed. 251 252 .. doctest:: 253 :skipif: _tkinter is None 254 255 >>> turtle.position() 256 (0.00,0.00) 257 >>> turtle.forward(25) 258 >>> turtle.position() 259 (25.00,0.00) 260 >>> turtle.forward(-75) 261 >>> turtle.position() 262 (-50.00,0.00) 263 264 265.. function:: back(distance) 266 bk(distance) 267 backward(distance) 268 269 :param distance: a number 270 271 Move the turtle backward by *distance*, opposite to the direction the 272 turtle is headed. Do not change the turtle's heading. 273 274 .. doctest:: 275 :hide: 276 277 >>> turtle.goto(0, 0) 278 279 .. doctest:: 280 :skipif: _tkinter is None 281 282 >>> turtle.position() 283 (0.00,0.00) 284 >>> turtle.backward(30) 285 >>> turtle.position() 286 (-30.00,0.00) 287 288 289.. function:: right(angle) 290 rt(angle) 291 292 :param angle: a number (integer or float) 293 294 Turn turtle right by *angle* units. (Units are by default degrees, but 295 can be set via the :func:`degrees` and :func:`radians` functions.) Angle 296 orientation depends on the turtle mode, see :func:`mode`. 297 298 .. doctest:: 299 :skipif: _tkinter is None 300 :hide: 301 302 >>> turtle.setheading(22) 303 304 .. doctest:: 305 :skipif: _tkinter is None 306 307 >>> turtle.heading() 308 22.0 309 >>> turtle.right(45) 310 >>> turtle.heading() 311 337.0 312 313 314.. function:: left(angle) 315 lt(angle) 316 317 :param angle: a number (integer or float) 318 319 Turn turtle left by *angle* units. (Units are by default degrees, but 320 can be set via the :func:`degrees` and :func:`radians` functions.) Angle 321 orientation depends on the turtle mode, see :func:`mode`. 322 323 .. doctest:: 324 :skipif: _tkinter is None 325 :hide: 326 327 >>> turtle.setheading(22) 328 329 .. doctest:: 330 :skipif: _tkinter is None 331 332 >>> turtle.heading() 333 22.0 334 >>> turtle.left(45) 335 >>> turtle.heading() 336 67.0 337 338 339.. function:: goto(x, y=None) 340 setpos(x, y=None) 341 setposition(x, y=None) 342 343 :param x: a number or a pair/vector of numbers 344 :param y: a number or ``None`` 345 346 If *y* is ``None``, *x* must be a pair of coordinates or a :class:`Vec2D` 347 (e.g. as returned by :func:`pos`). 348 349 Move turtle to an absolute position. If the pen is down, draw line. Do 350 not change the turtle's orientation. 351 352 .. doctest:: 353 :skipif: _tkinter is None 354 :hide: 355 356 >>> turtle.goto(0, 0) 357 358 .. doctest:: 359 :skipif: _tkinter is None 360 361 >>> tp = turtle.pos() 362 >>> tp 363 (0.00,0.00) 364 >>> turtle.setpos(60,30) 365 >>> turtle.pos() 366 (60.00,30.00) 367 >>> turtle.setpos((20,80)) 368 >>> turtle.pos() 369 (20.00,80.00) 370 >>> turtle.setpos(tp) 371 >>> turtle.pos() 372 (0.00,0.00) 373 374 375.. function:: setx(x) 376 377 :param x: a number (integer or float) 378 379 Set the turtle's first coordinate to *x*, leave second coordinate 380 unchanged. 381 382 .. doctest:: 383 :skipif: _tkinter is None 384 :hide: 385 386 >>> turtle.goto(0, 240) 387 388 .. doctest:: 389 :skipif: _tkinter is None 390 391 >>> turtle.position() 392 (0.00,240.00) 393 >>> turtle.setx(10) 394 >>> turtle.position() 395 (10.00,240.00) 396 397 398.. function:: sety(y) 399 400 :param y: a number (integer or float) 401 402 Set the turtle's second coordinate to *y*, leave first coordinate unchanged. 403 404 .. doctest:: 405 :skipif: _tkinter is None 406 :hide: 407 408 >>> turtle.goto(0, 40) 409 410 .. doctest:: 411 :skipif: _tkinter is None 412 413 >>> turtle.position() 414 (0.00,40.00) 415 >>> turtle.sety(-10) 416 >>> turtle.position() 417 (0.00,-10.00) 418 419 420.. function:: setheading(to_angle) 421 seth(to_angle) 422 423 :param to_angle: a number (integer or float) 424 425 Set the orientation of the turtle to *to_angle*. Here are some common 426 directions in degrees: 427 428 =================== ==================== 429 standard mode logo mode 430 =================== ==================== 431 0 - east 0 - north 432 90 - north 90 - east 433 180 - west 180 - south 434 270 - south 270 - west 435 =================== ==================== 436 437 .. doctest:: 438 :skipif: _tkinter is None 439 440 >>> turtle.setheading(90) 441 >>> turtle.heading() 442 90.0 443 444 445.. function:: home() 446 447 Move turtle to the origin -- coordinates (0,0) -- and set its heading to 448 its start-orientation (which depends on the mode, see :func:`mode`). 449 450 .. doctest:: 451 :skipif: _tkinter is None 452 :hide: 453 454 >>> turtle.setheading(90) 455 >>> turtle.goto(0, -10) 456 457 .. doctest:: 458 :skipif: _tkinter is None 459 460 >>> turtle.heading() 461 90.0 462 >>> turtle.position() 463 (0.00,-10.00) 464 >>> turtle.home() 465 >>> turtle.position() 466 (0.00,0.00) 467 >>> turtle.heading() 468 0.0 469 470 471.. function:: circle(radius, extent=None, steps=None) 472 473 :param radius: a number 474 :param extent: a number (or ``None``) 475 :param steps: an integer (or ``None``) 476 477 Draw a circle with given *radius*. The center is *radius* units left of 478 the turtle; *extent* -- an angle -- determines which part of the circle 479 is drawn. If *extent* is not given, draw the entire circle. If *extent* 480 is not a full circle, one endpoint of the arc is the current pen 481 position. Draw the arc in counterclockwise direction if *radius* is 482 positive, otherwise in clockwise direction. Finally the direction of the 483 turtle is changed by the amount of *extent*. 484 485 As the circle is approximated by an inscribed regular polygon, *steps* 486 determines the number of steps to use. If not given, it will be 487 calculated automatically. May be used to draw regular polygons. 488 489 .. doctest:: 490 :skipif: _tkinter is None 491 492 >>> turtle.home() 493 >>> turtle.position() 494 (0.00,0.00) 495 >>> turtle.heading() 496 0.0 497 >>> turtle.circle(50) 498 >>> turtle.position() 499 (-0.00,0.00) 500 >>> turtle.heading() 501 0.0 502 >>> turtle.circle(120, 180) # draw a semicircle 503 >>> turtle.position() 504 (0.00,240.00) 505 >>> turtle.heading() 506 180.0 507 508 509.. function:: dot(size=None, *color) 510 511 :param size: an integer >= 1 (if given) 512 :param color: a colorstring or a numeric color tuple 513 514 Draw a circular dot with diameter *size*, using *color*. If *size* is 515 not given, the maximum of pensize+4 and 2*pensize is used. 516 517 518 .. doctest:: 519 :skipif: _tkinter is None 520 521 >>> turtle.home() 522 >>> turtle.dot() 523 >>> turtle.fd(50); turtle.dot(20, "blue"); turtle.fd(50) 524 >>> turtle.position() 525 (100.00,-0.00) 526 >>> turtle.heading() 527 0.0 528 529 530.. function:: stamp() 531 532 Stamp a copy of the turtle shape onto the canvas at the current turtle 533 position. Return a stamp_id for that stamp, which can be used to delete 534 it by calling ``clearstamp(stamp_id)``. 535 536 .. doctest:: 537 :skipif: _tkinter is None 538 539 >>> turtle.color("blue") 540 >>> turtle.stamp() 541 11 542 >>> turtle.fd(50) 543 544 545.. function:: clearstamp(stampid) 546 547 :param stampid: an integer, must be return value of previous 548 :func:`stamp` call 549 550 Delete stamp with given *stampid*. 551 552 .. doctest:: 553 :skipif: _tkinter is None 554 555 >>> turtle.position() 556 (150.00,-0.00) 557 >>> turtle.color("blue") 558 >>> astamp = turtle.stamp() 559 >>> turtle.fd(50) 560 >>> turtle.position() 561 (200.00,-0.00) 562 >>> turtle.clearstamp(astamp) 563 >>> turtle.position() 564 (200.00,-0.00) 565 566 567.. function:: clearstamps(n=None) 568 569 :param n: an integer (or ``None``) 570 571 Delete all or first/last *n* of turtle's stamps. If *n* is ``None``, delete 572 all stamps, if *n* > 0 delete first *n* stamps, else if *n* < 0 delete 573 last *n* stamps. 574 575 .. doctest:: 576 577 >>> for i in range(8): 578 ... turtle.stamp(); turtle.fd(30) 579 13 580 14 581 15 582 16 583 17 584 18 585 19 586 20 587 >>> turtle.clearstamps(2) 588 >>> turtle.clearstamps(-2) 589 >>> turtle.clearstamps() 590 591 592.. function:: undo() 593 594 Undo (repeatedly) the last turtle action(s). Number of available 595 undo actions is determined by the size of the undobuffer. 596 597 .. doctest:: 598 :skipif: _tkinter is None 599 600 >>> for i in range(4): 601 ... turtle.fd(50); turtle.lt(80) 602 ... 603 >>> for i in range(8): 604 ... turtle.undo() 605 606 607.. function:: speed(speed=None) 608 609 :param speed: an integer in the range 0..10 or a speedstring (see below) 610 611 Set the turtle's speed to an integer value in the range 0..10. If no 612 argument is given, return current speed. 613 614 If input is a number greater than 10 or smaller than 0.5, speed is set 615 to 0. Speedstrings are mapped to speedvalues as follows: 616 617 * "fastest": 0 618 * "fast": 10 619 * "normal": 6 620 * "slow": 3 621 * "slowest": 1 622 623 Speeds from 1 to 10 enforce increasingly faster animation of line drawing 624 and turtle turning. 625 626 Attention: *speed* = 0 means that *no* animation takes 627 place. forward/back makes turtle jump and likewise left/right make the 628 turtle turn instantly. 629 630 .. doctest:: 631 :skipif: _tkinter is None 632 633 >>> turtle.speed() 634 3 635 >>> turtle.speed('normal') 636 >>> turtle.speed() 637 6 638 >>> turtle.speed(9) 639 >>> turtle.speed() 640 9 641 642 643Tell Turtle's state 644------------------- 645 646.. function:: position() 647 pos() 648 649 Return the turtle's current location (x,y) (as a :class:`Vec2D` vector). 650 651 .. doctest:: 652 :skipif: _tkinter is None 653 654 >>> turtle.pos() 655 (440.00,-0.00) 656 657 658.. function:: towards(x, y=None) 659 660 :param x: a number or a pair/vector of numbers or a turtle instance 661 :param y: a number if *x* is a number, else ``None`` 662 663 Return the angle between the line from turtle position to position specified 664 by (x,y), the vector or the other turtle. This depends on the turtle's start 665 orientation which depends on the mode - "standard"/"world" or "logo". 666 667 .. doctest:: 668 :skipif: _tkinter is None 669 670 >>> turtle.goto(10, 10) 671 >>> turtle.towards(0,0) 672 225.0 673 674 675.. function:: xcor() 676 677 Return the turtle's x coordinate. 678 679 .. doctest:: 680 :skipif: _tkinter is None 681 682 >>> turtle.home() 683 >>> turtle.left(50) 684 >>> turtle.forward(100) 685 >>> turtle.pos() 686 (64.28,76.60) 687 >>> print(round(turtle.xcor(), 5)) 688 64.27876 689 690 691.. function:: ycor() 692 693 Return the turtle's y coordinate. 694 695 .. doctest:: 696 :skipif: _tkinter is None 697 698 >>> turtle.home() 699 >>> turtle.left(60) 700 >>> turtle.forward(100) 701 >>> print(turtle.pos()) 702 (50.00,86.60) 703 >>> print(round(turtle.ycor(), 5)) 704 86.60254 705 706 707.. function:: heading() 708 709 Return the turtle's current heading (value depends on the turtle mode, see 710 :func:`mode`). 711 712 .. doctest:: 713 :skipif: _tkinter is None 714 715 >>> turtle.home() 716 >>> turtle.left(67) 717 >>> turtle.heading() 718 67.0 719 720 721.. function:: distance(x, y=None) 722 723 :param x: a number or a pair/vector of numbers or a turtle instance 724 :param y: a number if *x* is a number, else ``None`` 725 726 Return the distance from the turtle to (x,y), the given vector, or the given 727 other turtle, in turtle step units. 728 729 .. doctest:: 730 :skipif: _tkinter is None 731 732 >>> turtle.home() 733 >>> turtle.distance(30,40) 734 50.0 735 >>> turtle.distance((30,40)) 736 50.0 737 >>> joe = Turtle() 738 >>> joe.forward(77) 739 >>> turtle.distance(joe) 740 77.0 741 742 743Settings for measurement 744------------------------ 745 746.. function:: degrees(fullcircle=360.0) 747 748 :param fullcircle: a number 749 750 Set angle measurement units, i.e. set number of "degrees" for a full circle. 751 Default value is 360 degrees. 752 753 .. doctest:: 754 :skipif: _tkinter is None 755 756 >>> turtle.home() 757 >>> turtle.left(90) 758 >>> turtle.heading() 759 90.0 760 761 Change angle measurement unit to grad (also known as gon, 762 grade, or gradian and equals 1/100-th of the right angle.) 763 >>> turtle.degrees(400.0) 764 >>> turtle.heading() 765 100.0 766 >>> turtle.degrees(360) 767 >>> turtle.heading() 768 90.0 769 770 771.. function:: radians() 772 773 Set the angle measurement units to radians. Equivalent to 774 ``degrees(2*math.pi)``. 775 776 .. doctest:: 777 :skipif: _tkinter is None 778 779 >>> turtle.home() 780 >>> turtle.left(90) 781 >>> turtle.heading() 782 90.0 783 >>> turtle.radians() 784 >>> turtle.heading() 785 1.5707963267948966 786 787 .. doctest:: 788 :skipif: _tkinter is None 789 :hide: 790 791 >>> turtle.degrees(360) 792 793 794Pen control 795----------- 796 797Drawing state 798~~~~~~~~~~~~~ 799 800.. function:: pendown() 801 pd() 802 down() 803 804 Pull the pen down -- drawing when moving. 805 806 807.. function:: penup() 808 pu() 809 up() 810 811 Pull the pen up -- no drawing when moving. 812 813 814.. function:: pensize(width=None) 815 width(width=None) 816 817 :param width: a positive number 818 819 Set the line thickness to *width* or return it. If resizemode is set to 820 "auto" and turtleshape is a polygon, that polygon is drawn with the same line 821 thickness. If no argument is given, the current pensize is returned. 822 823 .. doctest:: 824 :skipif: _tkinter is None 825 826 >>> turtle.pensize() 827 1 828 >>> turtle.pensize(10) # from here on lines of width 10 are drawn 829 830 831.. function:: pen(pen=None, **pendict) 832 833 :param pen: a dictionary with some or all of the below listed keys 834 :param pendict: one or more keyword-arguments with the below listed keys as keywords 835 836 Return or set the pen's attributes in a "pen-dictionary" with the following 837 key/value pairs: 838 839 * "shown": True/False 840 * "pendown": True/False 841 * "pencolor": color-string or color-tuple 842 * "fillcolor": color-string or color-tuple 843 * "pensize": positive number 844 * "speed": number in range 0..10 845 * "resizemode": "auto" or "user" or "noresize" 846 * "stretchfactor": (positive number, positive number) 847 * "outline": positive number 848 * "tilt": number 849 850 This dictionary can be used as argument for a subsequent call to :func:`pen` 851 to restore the former pen-state. Moreover one or more of these attributes 852 can be provided as keyword-arguments. This can be used to set several pen 853 attributes in one statement. 854 855 .. doctest:: 856 :skipif: _tkinter is None 857 :options: +NORMALIZE_WHITESPACE 858 859 >>> turtle.pen(fillcolor="black", pencolor="red", pensize=10) 860 >>> sorted(turtle.pen().items()) 861 [('fillcolor', 'black'), ('outline', 1), ('pencolor', 'red'), 862 ('pendown', True), ('pensize', 10), ('resizemode', 'noresize'), 863 ('shearfactor', 0.0), ('shown', True), ('speed', 9), 864 ('stretchfactor', (1.0, 1.0)), ('tilt', 0.0)] 865 >>> penstate=turtle.pen() 866 >>> turtle.color("yellow", "") 867 >>> turtle.penup() 868 >>> sorted(turtle.pen().items())[:3] 869 [('fillcolor', ''), ('outline', 1), ('pencolor', 'yellow')] 870 >>> turtle.pen(penstate, fillcolor="green") 871 >>> sorted(turtle.pen().items())[:3] 872 [('fillcolor', 'green'), ('outline', 1), ('pencolor', 'red')] 873 874.. function:: isdown() 875 876 Return ``True`` if pen is down, ``False`` if it's up. 877 878 .. doctest:: 879 :skipif: _tkinter is None 880 881 >>> turtle.penup() 882 >>> turtle.isdown() 883 False 884 >>> turtle.pendown() 885 >>> turtle.isdown() 886 True 887 888 889Color control 890~~~~~~~~~~~~~ 891 892.. function:: pencolor(*args) 893 894 Return or set the pencolor. 895 896 Four input formats are allowed: 897 898 ``pencolor()`` 899 Return the current pencolor as color specification string or 900 as a tuple (see example). May be used as input to another 901 color/pencolor/fillcolor call. 902 903 ``pencolor(colorstring)`` 904 Set pencolor to *colorstring*, which is a Tk color specification string, 905 such as ``"red"``, ``"yellow"``, or ``"#33cc8c"``. 906 907 ``pencolor((r, g, b))`` 908 Set pencolor to the RGB color represented by the tuple of *r*, *g*, and 909 *b*. Each of *r*, *g*, and *b* must be in the range 0..colormode, where 910 colormode is either 1.0 or 255 (see :func:`colormode`). 911 912 ``pencolor(r, g, b)`` 913 Set pencolor to the RGB color represented by *r*, *g*, and *b*. Each of 914 *r*, *g*, and *b* must be in the range 0..colormode. 915 916 If turtleshape is a polygon, the outline of that polygon is drawn with the 917 newly set pencolor. 918 919 .. doctest:: 920 :skipif: _tkinter is None 921 922 >>> colormode() 923 1.0 924 >>> turtle.pencolor() 925 'red' 926 >>> turtle.pencolor("brown") 927 >>> turtle.pencolor() 928 'brown' 929 >>> tup = (0.2, 0.8, 0.55) 930 >>> turtle.pencolor(tup) 931 >>> turtle.pencolor() 932 (0.2, 0.8, 0.5490196078431373) 933 >>> colormode(255) 934 >>> turtle.pencolor() 935 (51.0, 204.0, 140.0) 936 >>> turtle.pencolor('#32c18f') 937 >>> turtle.pencolor() 938 (50.0, 193.0, 143.0) 939 940 941.. function:: fillcolor(*args) 942 943 Return or set the fillcolor. 944 945 Four input formats are allowed: 946 947 ``fillcolor()`` 948 Return the current fillcolor as color specification string, possibly 949 in tuple format (see example). May be used as input to another 950 color/pencolor/fillcolor call. 951 952 ``fillcolor(colorstring)`` 953 Set fillcolor to *colorstring*, which is a Tk color specification string, 954 such as ``"red"``, ``"yellow"``, or ``"#33cc8c"``. 955 956 ``fillcolor((r, g, b))`` 957 Set fillcolor to the RGB color represented by the tuple of *r*, *g*, and 958 *b*. Each of *r*, *g*, and *b* must be in the range 0..colormode, where 959 colormode is either 1.0 or 255 (see :func:`colormode`). 960 961 ``fillcolor(r, g, b)`` 962 Set fillcolor to the RGB color represented by *r*, *g*, and *b*. Each of 963 *r*, *g*, and *b* must be in the range 0..colormode. 964 965 If turtleshape is a polygon, the interior of that polygon is drawn 966 with the newly set fillcolor. 967 968 .. doctest:: 969 :skipif: _tkinter is None 970 971 >>> turtle.fillcolor("violet") 972 >>> turtle.fillcolor() 973 'violet' 974 >>> turtle.pencolor() 975 (50.0, 193.0, 143.0) 976 >>> turtle.fillcolor((50, 193, 143)) # Integers, not floats 977 >>> turtle.fillcolor() 978 (50.0, 193.0, 143.0) 979 >>> turtle.fillcolor('#ffffff') 980 >>> turtle.fillcolor() 981 (255.0, 255.0, 255.0) 982 983 984.. function:: color(*args) 985 986 Return or set pencolor and fillcolor. 987 988 Several input formats are allowed. They use 0 to 3 arguments as 989 follows: 990 991 ``color()`` 992 Return the current pencolor and the current fillcolor as a pair of color 993 specification strings or tuples as returned by :func:`pencolor` and 994 :func:`fillcolor`. 995 996 ``color(colorstring)``, ``color((r,g,b))``, ``color(r,g,b)`` 997 Inputs as in :func:`pencolor`, set both, fillcolor and pencolor, to the 998 given value. 999 1000 ``color(colorstring1, colorstring2)``, ``color((r1,g1,b1), (r2,g2,b2))`` 1001 Equivalent to ``pencolor(colorstring1)`` and ``fillcolor(colorstring2)`` 1002 and analogously if the other input format is used. 1003 1004 If turtleshape is a polygon, outline and interior of that polygon is drawn 1005 with the newly set colors. 1006 1007 .. doctest:: 1008 :skipif: _tkinter is None 1009 1010 >>> turtle.color("red", "green") 1011 >>> turtle.color() 1012 ('red', 'green') 1013 >>> color("#285078", "#a0c8f0") 1014 >>> color() 1015 ((40.0, 80.0, 120.0), (160.0, 200.0, 240.0)) 1016 1017 1018See also: Screen method :func:`colormode`. 1019 1020 1021Filling 1022~~~~~~~ 1023 1024.. doctest:: 1025 :skipif: _tkinter is None 1026 :hide: 1027 1028 >>> turtle.home() 1029 1030.. function:: filling() 1031 1032 Return fillstate (``True`` if filling, ``False`` else). 1033 1034 .. doctest:: 1035 :skipif: _tkinter is None 1036 1037 >>> turtle.begin_fill() 1038 >>> if turtle.filling(): 1039 ... turtle.pensize(5) 1040 ... else: 1041 ... turtle.pensize(3) 1042 1043 1044 1045.. function:: begin_fill() 1046 1047 To be called just before drawing a shape to be filled. 1048 1049 1050.. function:: end_fill() 1051 1052 Fill the shape drawn after the last call to :func:`begin_fill`. 1053 1054 Whether or not overlap regions for self-intersecting polygons 1055 or multiple shapes are filled depends on the operating system graphics, 1056 type of overlap, and number of overlaps. For example, the Turtle star 1057 above may be either all yellow or have some white regions. 1058 1059 .. doctest:: 1060 :skipif: _tkinter is None 1061 1062 >>> turtle.color("black", "red") 1063 >>> turtle.begin_fill() 1064 >>> turtle.circle(80) 1065 >>> turtle.end_fill() 1066 1067 1068More drawing control 1069~~~~~~~~~~~~~~~~~~~~ 1070 1071.. function:: reset() 1072 1073 Delete the turtle's drawings from the screen, re-center the turtle and set 1074 variables to the default values. 1075 1076 .. doctest:: 1077 :skipif: _tkinter is None 1078 1079 >>> turtle.goto(0,-22) 1080 >>> turtle.left(100) 1081 >>> turtle.position() 1082 (0.00,-22.00) 1083 >>> turtle.heading() 1084 100.0 1085 >>> turtle.reset() 1086 >>> turtle.position() 1087 (0.00,0.00) 1088 >>> turtle.heading() 1089 0.0 1090 1091 1092.. function:: clear() 1093 1094 Delete the turtle's drawings from the screen. Do not move turtle. State and 1095 position of the turtle as well as drawings of other turtles are not affected. 1096 1097 1098.. function:: write(arg, move=False, align="left", font=("Arial", 8, "normal")) 1099 1100 :param arg: object to be written to the TurtleScreen 1101 :param move: True/False 1102 :param align: one of the strings "left", "center" or right" 1103 :param font: a triple (fontname, fontsize, fonttype) 1104 1105 Write text - the string representation of *arg* - at the current turtle 1106 position according to *align* ("left", "center" or "right") and with the given 1107 font. If *move* is true, the pen is moved to the bottom-right corner of the 1108 text. By default, *move* is ``False``. 1109 1110 >>> turtle.write("Home = ", True, align="center") 1111 >>> turtle.write((0,0), True) 1112 1113 1114Turtle state 1115------------ 1116 1117Visibility 1118~~~~~~~~~~ 1119 1120.. function:: hideturtle() 1121 ht() 1122 1123 Make the turtle invisible. It's a good idea to do this while you're in the 1124 middle of doing some complex drawing, because hiding the turtle speeds up the 1125 drawing observably. 1126 1127 .. doctest:: 1128 :skipif: _tkinter is None 1129 1130 >>> turtle.hideturtle() 1131 1132 1133.. function:: showturtle() 1134 st() 1135 1136 Make the turtle visible. 1137 1138 .. doctest:: 1139 :skipif: _tkinter is None 1140 1141 >>> turtle.showturtle() 1142 1143 1144.. function:: isvisible() 1145 1146 Return ``True`` if the Turtle is shown, ``False`` if it's hidden. 1147 1148 >>> turtle.hideturtle() 1149 >>> turtle.isvisible() 1150 False 1151 >>> turtle.showturtle() 1152 >>> turtle.isvisible() 1153 True 1154 1155 1156Appearance 1157~~~~~~~~~~ 1158 1159.. function:: shape(name=None) 1160 1161 :param name: a string which is a valid shapename 1162 1163 Set turtle shape to shape with given *name* or, if name is not given, return 1164 name of current shape. Shape with *name* must exist in the TurtleScreen's 1165 shape dictionary. Initially there are the following polygon shapes: "arrow", 1166 "turtle", "circle", "square", "triangle", "classic". To learn about how to 1167 deal with shapes see Screen method :func:`register_shape`. 1168 1169 .. doctest:: 1170 :skipif: _tkinter is None 1171 1172 >>> turtle.shape() 1173 'classic' 1174 >>> turtle.shape("turtle") 1175 >>> turtle.shape() 1176 'turtle' 1177 1178 1179.. function:: resizemode(rmode=None) 1180 1181 :param rmode: one of the strings "auto", "user", "noresize" 1182 1183 Set resizemode to one of the values: "auto", "user", "noresize". If *rmode* 1184 is not given, return current resizemode. Different resizemodes have the 1185 following effects: 1186 1187 - "auto": adapts the appearance of the turtle corresponding to the value of pensize. 1188 - "user": adapts the appearance of the turtle according to the values of 1189 stretchfactor and outlinewidth (outline), which are set by 1190 :func:`shapesize`. 1191 - "noresize": no adaption of the turtle's appearance takes place. 1192 1193 ``resizemode("user")`` is called by :func:`shapesize` when used with arguments. 1194 1195 .. doctest:: 1196 :skipif: _tkinter is None 1197 1198 >>> turtle.resizemode() 1199 'noresize' 1200 >>> turtle.resizemode("auto") 1201 >>> turtle.resizemode() 1202 'auto' 1203 1204 1205.. function:: shapesize(stretch_wid=None, stretch_len=None, outline=None) 1206 turtlesize(stretch_wid=None, stretch_len=None, outline=None) 1207 1208 :param stretch_wid: positive number 1209 :param stretch_len: positive number 1210 :param outline: positive number 1211 1212 Return or set the pen's attributes x/y-stretchfactors and/or outline. Set 1213 resizemode to "user". If and only if resizemode is set to "user", the turtle 1214 will be displayed stretched according to its stretchfactors: *stretch_wid* is 1215 stretchfactor perpendicular to its orientation, *stretch_len* is 1216 stretchfactor in direction of its orientation, *outline* determines the width 1217 of the shapes's outline. 1218 1219 .. doctest:: 1220 :skipif: _tkinter is None 1221 1222 >>> turtle.shapesize() 1223 (1.0, 1.0, 1) 1224 >>> turtle.resizemode("user") 1225 >>> turtle.shapesize(5, 5, 12) 1226 >>> turtle.shapesize() 1227 (5, 5, 12) 1228 >>> turtle.shapesize(outline=8) 1229 >>> turtle.shapesize() 1230 (5, 5, 8) 1231 1232 1233.. function:: shearfactor(shear=None) 1234 1235 :param shear: number (optional) 1236 1237 Set or return the current shearfactor. Shear the turtleshape according to 1238 the given shearfactor shear, which is the tangent of the shear angle. 1239 Do *not* change the turtle's heading (direction of movement). 1240 If shear is not given: return the current shearfactor, i. e. the 1241 tangent of the shear angle, by which lines parallel to the 1242 heading of the turtle are sheared. 1243 1244 .. doctest:: 1245 :skipif: _tkinter is None 1246 1247 >>> turtle.shape("circle") 1248 >>> turtle.shapesize(5,2) 1249 >>> turtle.shearfactor(0.5) 1250 >>> turtle.shearfactor() 1251 0.5 1252 1253 1254.. function:: tilt(angle) 1255 1256 :param angle: a number 1257 1258 Rotate the turtleshape by *angle* from its current tilt-angle, but do *not* 1259 change the turtle's heading (direction of movement). 1260 1261 .. doctest:: 1262 :skipif: _tkinter is None 1263 1264 >>> turtle.reset() 1265 >>> turtle.shape("circle") 1266 >>> turtle.shapesize(5,2) 1267 >>> turtle.tilt(30) 1268 >>> turtle.fd(50) 1269 >>> turtle.tilt(30) 1270 >>> turtle.fd(50) 1271 1272 1273.. function:: settiltangle(angle) 1274 1275 :param angle: a number 1276 1277 Rotate the turtleshape to point in the direction specified by *angle*, 1278 regardless of its current tilt-angle. *Do not* change the turtle's heading 1279 (direction of movement). 1280 1281 .. doctest:: 1282 :skipif: _tkinter is None 1283 1284 >>> turtle.reset() 1285 >>> turtle.shape("circle") 1286 >>> turtle.shapesize(5,2) 1287 >>> turtle.settiltangle(45) 1288 >>> turtle.fd(50) 1289 >>> turtle.settiltangle(-45) 1290 >>> turtle.fd(50) 1291 1292 .. deprecated:: 3.1 1293 1294 1295.. function:: tiltangle(angle=None) 1296 1297 :param angle: a number (optional) 1298 1299 Set or return the current tilt-angle. If angle is given, rotate the 1300 turtleshape to point in the direction specified by angle, 1301 regardless of its current tilt-angle. Do *not* change the turtle's 1302 heading (direction of movement). 1303 If angle is not given: return the current tilt-angle, i. e. the angle 1304 between the orientation of the turtleshape and the heading of the 1305 turtle (its direction of movement). 1306 1307 .. doctest:: 1308 :skipif: _tkinter is None 1309 1310 >>> turtle.reset() 1311 >>> turtle.shape("circle") 1312 >>> turtle.shapesize(5,2) 1313 >>> turtle.tilt(45) 1314 >>> turtle.tiltangle() 1315 45.0 1316 1317 1318.. function:: shapetransform(t11=None, t12=None, t21=None, t22=None) 1319 1320 :param t11: a number (optional) 1321 :param t12: a number (optional) 1322 :param t21: a number (optional) 1323 :param t12: a number (optional) 1324 1325 Set or return the current transformation matrix of the turtle shape. 1326 1327 If none of the matrix elements are given, return the transformation 1328 matrix as a tuple of 4 elements. 1329 Otherwise set the given elements and transform the turtleshape 1330 according to the matrix consisting of first row t11, t12 and 1331 second row t21, t22. The determinant t11 * t22 - t12 * t21 must not be 1332 zero, otherwise an error is raised. 1333 Modify stretchfactor, shearfactor and tiltangle according to the 1334 given matrix. 1335 1336 .. doctest:: 1337 :skipif: _tkinter is None 1338 1339 >>> turtle = Turtle() 1340 >>> turtle.shape("square") 1341 >>> turtle.shapesize(4,2) 1342 >>> turtle.shearfactor(-0.5) 1343 >>> turtle.shapetransform() 1344 (4.0, -1.0, -0.0, 2.0) 1345 1346 1347.. function:: get_shapepoly() 1348 1349 Return the current shape polygon as tuple of coordinate pairs. This 1350 can be used to define a new shape or components of a compound shape. 1351 1352 .. doctest:: 1353 :skipif: _tkinter is None 1354 1355 >>> turtle.shape("square") 1356 >>> turtle.shapetransform(4, -1, 0, 2) 1357 >>> turtle.get_shapepoly() 1358 ((50, -20), (30, 20), (-50, 20), (-30, -20)) 1359 1360 1361Using events 1362------------ 1363 1364.. function:: onclick(fun, btn=1, add=None) 1365 :noindex: 1366 1367 :param fun: a function with two arguments which will be called with the 1368 coordinates of the clicked point on the canvas 1369 :param btn: number of the mouse-button, defaults to 1 (left mouse button) 1370 :param add: ``True`` or ``False`` -- if ``True``, a new binding will be 1371 added, otherwise it will replace a former binding 1372 1373 Bind *fun* to mouse-click events on this turtle. If *fun* is ``None``, 1374 existing bindings are removed. Example for the anonymous turtle, i.e. the 1375 procedural way: 1376 1377 .. doctest:: 1378 :skipif: _tkinter is None 1379 1380 >>> def turn(x, y): 1381 ... left(180) 1382 ... 1383 >>> onclick(turn) # Now clicking into the turtle will turn it. 1384 >>> onclick(None) # event-binding will be removed 1385 1386 1387.. function:: onrelease(fun, btn=1, add=None) 1388 1389 :param fun: a function with two arguments which will be called with the 1390 coordinates of the clicked point on the canvas 1391 :param btn: number of the mouse-button, defaults to 1 (left mouse button) 1392 :param add: ``True`` or ``False`` -- if ``True``, a new binding will be 1393 added, otherwise it will replace a former binding 1394 1395 Bind *fun* to mouse-button-release events on this turtle. If *fun* is 1396 ``None``, existing bindings are removed. 1397 1398 .. doctest:: 1399 :skipif: _tkinter is None 1400 1401 >>> class MyTurtle(Turtle): 1402 ... def glow(self,x,y): 1403 ... self.fillcolor("red") 1404 ... def unglow(self,x,y): 1405 ... self.fillcolor("") 1406 ... 1407 >>> turtle = MyTurtle() 1408 >>> turtle.onclick(turtle.glow) # clicking on turtle turns fillcolor red, 1409 >>> turtle.onrelease(turtle.unglow) # releasing turns it to transparent. 1410 1411 1412.. function:: ondrag(fun, btn=1, add=None) 1413 1414 :param fun: a function with two arguments which will be called with the 1415 coordinates of the clicked point on the canvas 1416 :param btn: number of the mouse-button, defaults to 1 (left mouse button) 1417 :param add: ``True`` or ``False`` -- if ``True``, a new binding will be 1418 added, otherwise it will replace a former binding 1419 1420 Bind *fun* to mouse-move events on this turtle. If *fun* is ``None``, 1421 existing bindings are removed. 1422 1423 Remark: Every sequence of mouse-move-events on a turtle is preceded by a 1424 mouse-click event on that turtle. 1425 1426 .. doctest:: 1427 :skipif: _tkinter is None 1428 1429 >>> turtle.ondrag(turtle.goto) 1430 1431 Subsequently, clicking and dragging the Turtle will move it across 1432 the screen thereby producing handdrawings (if pen is down). 1433 1434 1435Special Turtle methods 1436---------------------- 1437 1438.. function:: begin_poly() 1439 1440 Start recording the vertices of a polygon. Current turtle position is first 1441 vertex of polygon. 1442 1443 1444.. function:: end_poly() 1445 1446 Stop recording the vertices of a polygon. Current turtle position is last 1447 vertex of polygon. This will be connected with the first vertex. 1448 1449 1450.. function:: get_poly() 1451 1452 Return the last recorded polygon. 1453 1454 .. doctest:: 1455 :skipif: _tkinter is None 1456 1457 >>> turtle.home() 1458 >>> turtle.begin_poly() 1459 >>> turtle.fd(100) 1460 >>> turtle.left(20) 1461 >>> turtle.fd(30) 1462 >>> turtle.left(60) 1463 >>> turtle.fd(50) 1464 >>> turtle.end_poly() 1465 >>> p = turtle.get_poly() 1466 >>> register_shape("myFavouriteShape", p) 1467 1468 1469.. function:: clone() 1470 1471 Create and return a clone of the turtle with same position, heading and 1472 turtle properties. 1473 1474 .. doctest:: 1475 :skipif: _tkinter is None 1476 1477 >>> mick = Turtle() 1478 >>> joe = mick.clone() 1479 1480 1481.. function:: getturtle() 1482 getpen() 1483 1484 Return the Turtle object itself. Only reasonable use: as a function to 1485 return the "anonymous turtle": 1486 1487 .. doctest:: 1488 :skipif: _tkinter is None 1489 1490 >>> pet = getturtle() 1491 >>> pet.fd(50) 1492 >>> pet 1493 <turtle.Turtle object at 0x...> 1494 1495 1496.. function:: getscreen() 1497 1498 Return the :class:`TurtleScreen` object the turtle is drawing on. 1499 TurtleScreen methods can then be called for that object. 1500 1501 .. doctest:: 1502 :skipif: _tkinter is None 1503 1504 >>> ts = turtle.getscreen() 1505 >>> ts 1506 <turtle._Screen object at 0x...> 1507 >>> ts.bgcolor("pink") 1508 1509 1510.. function:: setundobuffer(size) 1511 1512 :param size: an integer or ``None`` 1513 1514 Set or disable undobuffer. If *size* is an integer, an empty undobuffer of 1515 given size is installed. *size* gives the maximum number of turtle actions 1516 that can be undone by the :func:`undo` method/function. If *size* is 1517 ``None``, the undobuffer is disabled. 1518 1519 .. doctest:: 1520 :skipif: _tkinter is None 1521 1522 >>> turtle.setundobuffer(42) 1523 1524 1525.. function:: undobufferentries() 1526 1527 Return number of entries in the undobuffer. 1528 1529 .. doctest:: 1530 :skipif: _tkinter is None 1531 1532 >>> while undobufferentries(): 1533 ... undo() 1534 1535 1536 1537.. _compoundshapes: 1538 1539Compound shapes 1540--------------- 1541 1542To use compound turtle shapes, which consist of several polygons of different 1543color, you must use the helper class :class:`Shape` explicitly as described 1544below: 1545 15461. Create an empty Shape object of type "compound". 15472. Add as many components to this object as desired, using the 1548 :meth:`addcomponent` method. 1549 1550 For example: 1551 1552 .. doctest:: 1553 :skipif: _tkinter is None 1554 1555 >>> s = Shape("compound") 1556 >>> poly1 = ((0,0),(10,-5),(0,10),(-10,-5)) 1557 >>> s.addcomponent(poly1, "red", "blue") 1558 >>> poly2 = ((0,0),(10,-5),(-10,-5)) 1559 >>> s.addcomponent(poly2, "blue", "red") 1560 15613. Now add the Shape to the Screen's shapelist and use it: 1562 1563 .. doctest:: 1564 :skipif: _tkinter is None 1565 1566 >>> register_shape("myshape", s) 1567 >>> shape("myshape") 1568 1569 1570.. note:: 1571 1572 The :class:`Shape` class is used internally by the :func:`register_shape` 1573 method in different ways. The application programmer has to deal with the 1574 Shape class *only* when using compound shapes like shown above! 1575 1576 1577Methods of TurtleScreen/Screen and corresponding functions 1578========================================================== 1579 1580Most of the examples in this section refer to a TurtleScreen instance called 1581``screen``. 1582 1583.. doctest:: 1584 :skipif: _tkinter is None 1585 :hide: 1586 1587 >>> screen = Screen() 1588 1589Window control 1590-------------- 1591 1592.. function:: bgcolor(*args) 1593 1594 :param args: a color string or three numbers in the range 0..colormode or a 1595 3-tuple of such numbers 1596 1597 1598 Set or return background color of the TurtleScreen. 1599 1600 .. doctest:: 1601 :skipif: _tkinter is None 1602 1603 >>> screen.bgcolor("orange") 1604 >>> screen.bgcolor() 1605 'orange' 1606 >>> screen.bgcolor("#800080") 1607 >>> screen.bgcolor() 1608 (128.0, 0.0, 128.0) 1609 1610 1611.. function:: bgpic(picname=None) 1612 1613 :param picname: a string, name of a gif-file or ``"nopic"``, or ``None`` 1614 1615 Set background image or return name of current backgroundimage. If *picname* 1616 is a filename, set the corresponding image as background. If *picname* is 1617 ``"nopic"``, delete background image, if present. If *picname* is ``None``, 1618 return the filename of the current backgroundimage. :: 1619 1620 >>> screen.bgpic() 1621 'nopic' 1622 >>> screen.bgpic("landscape.gif") 1623 >>> screen.bgpic() 1624 "landscape.gif" 1625 1626 1627.. function:: clear() 1628 :noindex: 1629 1630 .. note:: 1631 This TurtleScreen method is available as a global function only under the 1632 name ``clearscreen``. The global function ``clear`` is a different one 1633 derived from the Turtle method ``clear``. 1634 1635 1636.. function:: clearscreen() 1637 1638 Delete all drawings and all turtles from the TurtleScreen. Reset the now 1639 empty TurtleScreen to its initial state: white background, no background 1640 image, no event bindings and tracing on. 1641 1642 1643.. function:: reset() 1644 :noindex: 1645 1646 .. note:: 1647 This TurtleScreen method is available as a global function only under the 1648 name ``resetscreen``. The global function ``reset`` is another one 1649 derived from the Turtle method ``reset``. 1650 1651 1652.. function:: resetscreen() 1653 1654 Reset all Turtles on the Screen to their initial state. 1655 1656 1657.. function:: screensize(canvwidth=None, canvheight=None, bg=None) 1658 1659 :param canvwidth: positive integer, new width of canvas in pixels 1660 :param canvheight: positive integer, new height of canvas in pixels 1661 :param bg: colorstring or color-tuple, new background color 1662 1663 If no arguments are given, return current (canvaswidth, canvasheight). Else 1664 resize the canvas the turtles are drawing on. Do not alter the drawing 1665 window. To observe hidden parts of the canvas, use the scrollbars. With this 1666 method, one can make visible those parts of a drawing which were outside the 1667 canvas before. 1668 1669 >>> screen.screensize() 1670 (400, 300) 1671 >>> screen.screensize(2000,1500) 1672 >>> screen.screensize() 1673 (2000, 1500) 1674 1675 e.g. to search for an erroneously escaped turtle ;-) 1676 1677 1678.. function:: setworldcoordinates(llx, lly, urx, ury) 1679 1680 :param llx: a number, x-coordinate of lower left corner of canvas 1681 :param lly: a number, y-coordinate of lower left corner of canvas 1682 :param urx: a number, x-coordinate of upper right corner of canvas 1683 :param ury: a number, y-coordinate of upper right corner of canvas 1684 1685 Set up user-defined coordinate system and switch to mode "world" if 1686 necessary. This performs a ``screen.reset()``. If mode "world" is already 1687 active, all drawings are redrawn according to the new coordinates. 1688 1689 **ATTENTION**: in user-defined coordinate systems angles may appear 1690 distorted. 1691 1692 .. doctest:: 1693 :skipif: _tkinter is None 1694 1695 >>> screen.reset() 1696 >>> screen.setworldcoordinates(-50,-7.5,50,7.5) 1697 >>> for _ in range(72): 1698 ... left(10) 1699 ... 1700 >>> for _ in range(8): 1701 ... left(45); fd(2) # a regular octagon 1702 1703 .. doctest:: 1704 :skipif: _tkinter is None 1705 :hide: 1706 1707 >>> screen.reset() 1708 >>> for t in turtles(): 1709 ... t.reset() 1710 1711 1712Animation control 1713----------------- 1714 1715.. function:: delay(delay=None) 1716 1717 :param delay: positive integer 1718 1719 Set or return the drawing *delay* in milliseconds. (This is approximately 1720 the time interval between two consecutive canvas updates.) The longer the 1721 drawing delay, the slower the animation. 1722 1723 Optional argument: 1724 1725 .. doctest:: 1726 :skipif: _tkinter is None 1727 1728 >>> screen.delay() 1729 10 1730 >>> screen.delay(5) 1731 >>> screen.delay() 1732 5 1733 1734 1735.. function:: tracer(n=None, delay=None) 1736 1737 :param n: nonnegative integer 1738 :param delay: nonnegative integer 1739 1740 Turn turtle animation on/off and set delay for update drawings. If 1741 *n* is given, only each n-th regular screen update is really 1742 performed. (Can be used to accelerate the drawing of complex 1743 graphics.) When called without arguments, returns the currently 1744 stored value of n. Second argument sets delay value (see 1745 :func:`delay`). 1746 1747 .. doctest:: 1748 :skipif: _tkinter is None 1749 1750 >>> screen.tracer(8, 25) 1751 >>> dist = 2 1752 >>> for i in range(200): 1753 ... fd(dist) 1754 ... rt(90) 1755 ... dist += 2 1756 1757 1758.. function:: update() 1759 1760 Perform a TurtleScreen update. To be used when tracer is turned off. 1761 1762See also the RawTurtle/Turtle method :func:`speed`. 1763 1764 1765Using screen events 1766------------------- 1767 1768.. function:: listen(xdummy=None, ydummy=None) 1769 1770 Set focus on TurtleScreen (in order to collect key-events). Dummy arguments 1771 are provided in order to be able to pass :func:`listen` to the onclick method. 1772 1773 1774.. function:: onkey(fun, key) 1775 onkeyrelease(fun, key) 1776 1777 :param fun: a function with no arguments or ``None`` 1778 :param key: a string: key (e.g. "a") or key-symbol (e.g. "space") 1779 1780 Bind *fun* to key-release event of key. If *fun* is ``None``, event bindings 1781 are removed. Remark: in order to be able to register key-events, TurtleScreen 1782 must have the focus. (See method :func:`listen`.) 1783 1784 .. doctest:: 1785 :skipif: _tkinter is None 1786 1787 >>> def f(): 1788 ... fd(50) 1789 ... lt(60) 1790 ... 1791 >>> screen.onkey(f, "Up") 1792 >>> screen.listen() 1793 1794 1795.. function:: onkeypress(fun, key=None) 1796 1797 :param fun: a function with no arguments or ``None`` 1798 :param key: a string: key (e.g. "a") or key-symbol (e.g. "space") 1799 1800 Bind *fun* to key-press event of key if key is given, 1801 or to any key-press-event if no key is given. 1802 Remark: in order to be able to register key-events, TurtleScreen 1803 must have focus. (See method :func:`listen`.) 1804 1805 .. doctest:: 1806 :skipif: _tkinter is None 1807 1808 >>> def f(): 1809 ... fd(50) 1810 ... 1811 >>> screen.onkey(f, "Up") 1812 >>> screen.listen() 1813 1814 1815.. function:: onclick(fun, btn=1, add=None) 1816 onscreenclick(fun, btn=1, add=None) 1817 1818 :param fun: a function with two arguments which will be called with the 1819 coordinates of the clicked point on the canvas 1820 :param btn: number of the mouse-button, defaults to 1 (left mouse button) 1821 :param add: ``True`` or ``False`` -- if ``True``, a new binding will be 1822 added, otherwise it will replace a former binding 1823 1824 Bind *fun* to mouse-click events on this screen. If *fun* is ``None``, 1825 existing bindings are removed. 1826 1827 Example for a TurtleScreen instance named ``screen`` and a Turtle instance 1828 named ``turtle``: 1829 1830 .. doctest:: 1831 :skipif: _tkinter is None 1832 1833 >>> screen.onclick(turtle.goto) # Subsequently clicking into the TurtleScreen will 1834 >>> # make the turtle move to the clicked point. 1835 >>> screen.onclick(None) # remove event binding again 1836 1837 .. note:: 1838 This TurtleScreen method is available as a global function only under the 1839 name ``onscreenclick``. The global function ``onclick`` is another one 1840 derived from the Turtle method ``onclick``. 1841 1842 1843.. function:: ontimer(fun, t=0) 1844 1845 :param fun: a function with no arguments 1846 :param t: a number >= 0 1847 1848 Install a timer that calls *fun* after *t* milliseconds. 1849 1850 .. doctest:: 1851 :skipif: _tkinter is None 1852 1853 >>> running = True 1854 >>> def f(): 1855 ... if running: 1856 ... fd(50) 1857 ... lt(60) 1858 ... screen.ontimer(f, 250) 1859 >>> f() ### makes the turtle march around 1860 >>> running = False 1861 1862 1863.. function:: mainloop() 1864 done() 1865 1866 Starts event loop - calling Tkinter's mainloop function. 1867 Must be the last statement in a turtle graphics program. 1868 Must *not* be used if a script is run from within IDLE in -n mode 1869 (No subprocess) - for interactive use of turtle graphics. :: 1870 1871 >>> screen.mainloop() 1872 1873 1874Input methods 1875------------- 1876 1877.. function:: textinput(title, prompt) 1878 1879 :param title: string 1880 :param prompt: string 1881 1882 Pop up a dialog window for input of a string. Parameter title is 1883 the title of the dialog window, prompt is a text mostly describing 1884 what information to input. 1885 Return the string input. If the dialog is canceled, return ``None``. :: 1886 1887 >>> screen.textinput("NIM", "Name of first player:") 1888 1889 1890.. function:: numinput(title, prompt, default=None, minval=None, maxval=None) 1891 1892 :param title: string 1893 :param prompt: string 1894 :param default: number (optional) 1895 :param minval: number (optional) 1896 :param maxval: number (optional) 1897 1898 Pop up a dialog window for input of a number. title is the title of the 1899 dialog window, prompt is a text mostly describing what numerical information 1900 to input. default: default value, minval: minimum value for input, 1901 maxval: maximum value for input 1902 The number input must be in the range minval .. maxval if these are 1903 given. If not, a hint is issued and the dialog remains open for 1904 correction. 1905 Return the number input. If the dialog is canceled, return ``None``. :: 1906 1907 >>> screen.numinput("Poker", "Your stakes:", 1000, minval=10, maxval=10000) 1908 1909 1910Settings and special methods 1911---------------------------- 1912 1913.. function:: mode(mode=None) 1914 1915 :param mode: one of the strings "standard", "logo" or "world" 1916 1917 Set turtle mode ("standard", "logo" or "world") and perform reset. If mode 1918 is not given, current mode is returned. 1919 1920 Mode "standard" is compatible with old :mod:`turtle`. Mode "logo" is 1921 compatible with most Logo turtle graphics. Mode "world" uses user-defined 1922 "world coordinates". **Attention**: in this mode angles appear distorted if 1923 ``x/y`` unit-ratio doesn't equal 1. 1924 1925 ============ ========================= =================== 1926 Mode Initial turtle heading positive angles 1927 ============ ========================= =================== 1928 "standard" to the right (east) counterclockwise 1929 "logo" upward (north) clockwise 1930 ============ ========================= =================== 1931 1932 .. doctest:: 1933 :skipif: _tkinter is None 1934 1935 >>> mode("logo") # resets turtle heading to north 1936 >>> mode() 1937 'logo' 1938 1939 1940.. function:: colormode(cmode=None) 1941 1942 :param cmode: one of the values 1.0 or 255 1943 1944 Return the colormode or set it to 1.0 or 255. Subsequently *r*, *g*, *b* 1945 values of color triples have to be in the range 0..\ *cmode*. 1946 1947 .. doctest:: 1948 :skipif: _tkinter is None 1949 1950 >>> screen.colormode(1) 1951 >>> turtle.pencolor(240, 160, 80) 1952 Traceback (most recent call last): 1953 ... 1954 TurtleGraphicsError: bad color sequence: (240, 160, 80) 1955 >>> screen.colormode() 1956 1.0 1957 >>> screen.colormode(255) 1958 >>> screen.colormode() 1959 255 1960 >>> turtle.pencolor(240,160,80) 1961 1962 1963.. function:: getcanvas() 1964 1965 Return the Canvas of this TurtleScreen. Useful for insiders who know what to 1966 do with a Tkinter Canvas. 1967 1968 .. doctest:: 1969 :skipif: _tkinter is None 1970 1971 >>> cv = screen.getcanvas() 1972 >>> cv 1973 <turtle.ScrolledCanvas object ...> 1974 1975 1976.. function:: getshapes() 1977 1978 Return a list of names of all currently available turtle shapes. 1979 1980 .. doctest:: 1981 :skipif: _tkinter is None 1982 1983 >>> screen.getshapes() 1984 ['arrow', 'blank', 'circle', ..., 'turtle'] 1985 1986 1987.. function:: register_shape(name, shape=None) 1988 addshape(name, shape=None) 1989 1990 There are three different ways to call this function: 1991 1992 (1) *name* is the name of a gif-file and *shape* is ``None``: Install the 1993 corresponding image shape. :: 1994 1995 >>> screen.register_shape("turtle.gif") 1996 1997 .. note:: 1998 Image shapes *do not* rotate when turning the turtle, so they do not 1999 display the heading of the turtle! 2000 2001 (2) *name* is an arbitrary string and *shape* is a tuple of pairs of 2002 coordinates: Install the corresponding polygon shape. 2003 2004 .. doctest:: 2005 :skipif: _tkinter is None 2006 2007 >>> screen.register_shape("triangle", ((5,-3), (0,5), (-5,-3))) 2008 2009 (3) *name* is an arbitrary string and shape is a (compound) :class:`Shape` 2010 object: Install the corresponding compound shape. 2011 2012 Add a turtle shape to TurtleScreen's shapelist. Only thusly registered 2013 shapes can be used by issuing the command ``shape(shapename)``. 2014 2015 2016.. function:: turtles() 2017 2018 Return the list of turtles on the screen. 2019 2020 .. doctest:: 2021 :skipif: _tkinter is None 2022 2023 >>> for turtle in screen.turtles(): 2024 ... turtle.color("red") 2025 2026 2027.. function:: window_height() 2028 2029 Return the height of the turtle window. :: 2030 2031 >>> screen.window_height() 2032 480 2033 2034 2035.. function:: window_width() 2036 2037 Return the width of the turtle window. :: 2038 2039 >>> screen.window_width() 2040 640 2041 2042 2043.. _screenspecific: 2044 2045Methods specific to Screen, not inherited from TurtleScreen 2046----------------------------------------------------------- 2047 2048.. function:: bye() 2049 2050 Shut the turtlegraphics window. 2051 2052 2053.. function:: exitonclick() 2054 2055 Bind ``bye()`` method to mouse clicks on the Screen. 2056 2057 2058 If the value "using_IDLE" in the configuration dictionary is ``False`` 2059 (default value), also enter mainloop. Remark: If IDLE with the ``-n`` switch 2060 (no subprocess) is used, this value should be set to ``True`` in 2061 :file:`turtle.cfg`. In this case IDLE's own mainloop is active also for the 2062 client script. 2063 2064 2065.. function:: setup(width=_CFG["width"], height=_CFG["height"], startx=_CFG["leftright"], starty=_CFG["topbottom"]) 2066 2067 Set the size and position of the main window. Default values of arguments 2068 are stored in the configuration dictionary and can be changed via a 2069 :file:`turtle.cfg` file. 2070 2071 :param width: if an integer, a size in pixels, if a float, a fraction of the 2072 screen; default is 50% of screen 2073 :param height: if an integer, the height in pixels, if a float, a fraction of 2074 the screen; default is 75% of screen 2075 :param startx: if positive, starting position in pixels from the left 2076 edge of the screen, if negative from the right edge, if ``None``, 2077 center window horizontally 2078 :param starty: if positive, starting position in pixels from the top 2079 edge of the screen, if negative from the bottom edge, if ``None``, 2080 center window vertically 2081 2082 .. doctest:: 2083 :skipif: _tkinter is None 2084 2085 >>> screen.setup (width=200, height=200, startx=0, starty=0) 2086 >>> # sets window to 200x200 pixels, in upper left of screen 2087 >>> screen.setup(width=.75, height=0.5, startx=None, starty=None) 2088 >>> # sets window to 75% of screen by 50% of screen and centers 2089 2090 2091.. function:: title(titlestring) 2092 2093 :param titlestring: a string that is shown in the titlebar of the turtle 2094 graphics window 2095 2096 Set title of turtle window to *titlestring*. 2097 2098 .. doctest:: 2099 :skipif: _tkinter is None 2100 2101 >>> screen.title("Welcome to the turtle zoo!") 2102 2103 2104Public classes 2105============== 2106 2107 2108.. class:: RawTurtle(canvas) 2109 RawPen(canvas) 2110 2111 :param canvas: a :class:`tkinter.Canvas`, a :class:`ScrolledCanvas` or a 2112 :class:`TurtleScreen` 2113 2114 Create a turtle. The turtle has all methods described above as "methods of 2115 Turtle/RawTurtle". 2116 2117 2118.. class:: Turtle() 2119 2120 Subclass of RawTurtle, has the same interface but draws on a default 2121 :class:`Screen` object created automatically when needed for the first time. 2122 2123 2124.. class:: TurtleScreen(cv) 2125 2126 :param cv: a :class:`tkinter.Canvas` 2127 2128 Provides screen oriented methods like :func:`setbg` etc. that are described 2129 above. 2130 2131.. class:: Screen() 2132 2133 Subclass of TurtleScreen, with :ref:`four methods added <screenspecific>`. 2134 2135 2136.. class:: ScrolledCanvas(master) 2137 2138 :param master: some Tkinter widget to contain the ScrolledCanvas, i.e. 2139 a Tkinter-canvas with scrollbars added 2140 2141 Used by class Screen, which thus automatically provides a ScrolledCanvas as 2142 playground for the turtles. 2143 2144.. class:: Shape(type_, data) 2145 2146 :param type\_: one of the strings "polygon", "image", "compound" 2147 2148 Data structure modeling shapes. The pair ``(type_, data)`` must follow this 2149 specification: 2150 2151 2152 =========== =========== 2153 *type_* *data* 2154 =========== =========== 2155 "polygon" a polygon-tuple, i.e. a tuple of pairs of coordinates 2156 "image" an image (in this form only used internally!) 2157 "compound" ``None`` (a compound shape has to be constructed using the 2158 :meth:`addcomponent` method) 2159 =========== =========== 2160 2161 .. method:: addcomponent(poly, fill, outline=None) 2162 2163 :param poly: a polygon, i.e. a tuple of pairs of numbers 2164 :param fill: a color the *poly* will be filled with 2165 :param outline: a color for the poly's outline (if given) 2166 2167 Example: 2168 2169 .. doctest:: 2170 :skipif: _tkinter is None 2171 2172 >>> poly = ((0,0),(10,-5),(0,10),(-10,-5)) 2173 >>> s = Shape("compound") 2174 >>> s.addcomponent(poly, "red", "blue") 2175 >>> # ... add more components and then use register_shape() 2176 2177 See :ref:`compoundshapes`. 2178 2179 2180.. class:: Vec2D(x, y) 2181 2182 A two-dimensional vector class, used as a helper class for implementing 2183 turtle graphics. May be useful for turtle graphics programs too. Derived 2184 from tuple, so a vector is a tuple! 2185 2186 Provides (for *a*, *b* vectors, *k* number): 2187 2188 * ``a + b`` vector addition 2189 * ``a - b`` vector subtraction 2190 * ``a * b`` inner product 2191 * ``k * a`` and ``a * k`` multiplication with scalar 2192 * ``abs(a)`` absolute value of a 2193 * ``a.rotate(angle)`` rotation 2194 2195 2196Help and configuration 2197====================== 2198 2199How to use help 2200--------------- 2201 2202The public methods of the Screen and Turtle classes are documented extensively 2203via docstrings. So these can be used as online-help via the Python help 2204facilities: 2205 2206- When using IDLE, tooltips show the signatures and first lines of the 2207 docstrings of typed in function-/method calls. 2208 2209- Calling :func:`help` on methods or functions displays the docstrings:: 2210 2211 >>> help(Screen.bgcolor) 2212 Help on method bgcolor in module turtle: 2213 2214 bgcolor(self, *args) unbound turtle.Screen method 2215 Set or return backgroundcolor of the TurtleScreen. 2216 2217 Arguments (if given): a color string or three numbers 2218 in the range 0..colormode or a 3-tuple of such numbers. 2219 2220 2221 >>> screen.bgcolor("orange") 2222 >>> screen.bgcolor() 2223 "orange" 2224 >>> screen.bgcolor(0.5,0,0.5) 2225 >>> screen.bgcolor() 2226 "#800080" 2227 2228 >>> help(Turtle.penup) 2229 Help on method penup in module turtle: 2230 2231 penup(self) unbound turtle.Turtle method 2232 Pull the pen up -- no drawing when moving. 2233 2234 Aliases: penup | pu | up 2235 2236 No argument 2237 2238 >>> turtle.penup() 2239 2240- The docstrings of the functions which are derived from methods have a modified 2241 form:: 2242 2243 >>> help(bgcolor) 2244 Help on function bgcolor in module turtle: 2245 2246 bgcolor(*args) 2247 Set or return backgroundcolor of the TurtleScreen. 2248 2249 Arguments (if given): a color string or three numbers 2250 in the range 0..colormode or a 3-tuple of such numbers. 2251 2252 Example:: 2253 2254 >>> bgcolor("orange") 2255 >>> bgcolor() 2256 "orange" 2257 >>> bgcolor(0.5,0,0.5) 2258 >>> bgcolor() 2259 "#800080" 2260 2261 >>> help(penup) 2262 Help on function penup in module turtle: 2263 2264 penup() 2265 Pull the pen up -- no drawing when moving. 2266 2267 Aliases: penup | pu | up 2268 2269 No argument 2270 2271 Example: 2272 >>> penup() 2273 2274These modified docstrings are created automatically together with the function 2275definitions that are derived from the methods at import time. 2276 2277 2278Translation of docstrings into different languages 2279-------------------------------------------------- 2280 2281There is a utility to create a dictionary the keys of which are the method names 2282and the values of which are the docstrings of the public methods of the classes 2283Screen and Turtle. 2284 2285.. function:: write_docstringdict(filename="turtle_docstringdict") 2286 2287 :param filename: a string, used as filename 2288 2289 Create and write docstring-dictionary to a Python script with the given 2290 filename. This function has to be called explicitly (it is not used by the 2291 turtle graphics classes). The docstring dictionary will be written to the 2292 Python script :file:`{filename}.py`. It is intended to serve as a template 2293 for translation of the docstrings into different languages. 2294 2295If you (or your students) want to use :mod:`turtle` with online help in your 2296native language, you have to translate the docstrings and save the resulting 2297file as e.g. :file:`turtle_docstringdict_german.py`. 2298 2299If you have an appropriate entry in your :file:`turtle.cfg` file this dictionary 2300will be read in at import time and will replace the original English docstrings. 2301 2302At the time of this writing there are docstring dictionaries in German and in 2303Italian. (Requests please to glingl@aon.at.) 2304 2305 2306 2307How to configure Screen and Turtles 2308----------------------------------- 2309 2310The built-in default configuration mimics the appearance and behaviour of the 2311old turtle module in order to retain best possible compatibility with it. 2312 2313If you want to use a different configuration which better reflects the features 2314of this module or which better fits to your needs, e.g. for use in a classroom, 2315you can prepare a configuration file ``turtle.cfg`` which will be read at import 2316time and modify the configuration according to its settings. 2317 2318The built in configuration would correspond to the following turtle.cfg:: 2319 2320 width = 0.5 2321 height = 0.75 2322 leftright = None 2323 topbottom = None 2324 canvwidth = 400 2325 canvheight = 300 2326 mode = standard 2327 colormode = 1.0 2328 delay = 10 2329 undobuffersize = 1000 2330 shape = classic 2331 pencolor = black 2332 fillcolor = black 2333 resizemode = noresize 2334 visible = True 2335 language = english 2336 exampleturtle = turtle 2337 examplescreen = screen 2338 title = Python Turtle Graphics 2339 using_IDLE = False 2340 2341Short explanation of selected entries: 2342 2343- The first four lines correspond to the arguments of the :meth:`Screen.setup` 2344 method. 2345- Line 5 and 6 correspond to the arguments of the method 2346 :meth:`Screen.screensize`. 2347- *shape* can be any of the built-in shapes, e.g: arrow, turtle, etc. For more 2348 info try ``help(shape)``. 2349- If you want to use no fillcolor (i.e. make the turtle transparent), you have 2350 to write ``fillcolor = ""`` (but all nonempty strings must not have quotes in 2351 the cfg-file). 2352- If you want to reflect the turtle its state, you have to use ``resizemode = 2353 auto``. 2354- If you set e.g. ``language = italian`` the docstringdict 2355 :file:`turtle_docstringdict_italian.py` will be loaded at import time (if 2356 present on the import path, e.g. in the same directory as :mod:`turtle`. 2357- The entries *exampleturtle* and *examplescreen* define the names of these 2358 objects as they occur in the docstrings. The transformation of 2359 method-docstrings to function-docstrings will delete these names from the 2360 docstrings. 2361- *using_IDLE*: Set this to ``True`` if you regularly work with IDLE and its -n 2362 switch ("no subprocess"). This will prevent :func:`exitonclick` to enter the 2363 mainloop. 2364 2365There can be a :file:`turtle.cfg` file in the directory where :mod:`turtle` is 2366stored and an additional one in the current working directory. The latter will 2367override the settings of the first one. 2368 2369The :file:`Lib/turtledemo` directory contains a :file:`turtle.cfg` file. You can 2370study it as an example and see its effects when running the demos (preferably 2371not from within the demo-viewer). 2372 2373 2374:mod:`turtledemo` --- Demo scripts 2375================================== 2376 2377.. module:: turtledemo 2378 :synopsis: A viewer for example turtle scripts 2379 2380The :mod:`turtledemo` package includes a set of demo scripts. These 2381scripts can be run and viewed using the supplied demo viewer as follows:: 2382 2383 python -m turtledemo 2384 2385Alternatively, you can run the demo scripts individually. For example, :: 2386 2387 python -m turtledemo.bytedesign 2388 2389The :mod:`turtledemo` package directory contains: 2390 2391- A demo viewer :file:`__main__.py` which can be used to view the sourcecode 2392 of the scripts and run them at the same time. 2393- Multiple scripts demonstrating different features of the :mod:`turtle` 2394 module. Examples can be accessed via the Examples menu. They can also 2395 be run standalone. 2396- A :file:`turtle.cfg` file which serves as an example of how to write 2397 and use such files. 2398 2399The demo scripts are: 2400 2401.. tabularcolumns:: |l|L|L| 2402 2403+----------------+------------------------------+-----------------------+ 2404| Name | Description | Features | 2405+================+==============================+=======================+ 2406| bytedesign | complex classical | :func:`tracer`, delay,| 2407| | turtle graphics pattern | :func:`update` | 2408+----------------+------------------------------+-----------------------+ 2409| chaos | graphs Verhulst dynamics, | world coordinates | 2410| | shows that computer's | | 2411| | computations can generate | | 2412| | results sometimes against the| | 2413| | common sense expectations | | 2414+----------------+------------------------------+-----------------------+ 2415| clock | analog clock showing time | turtles as clock's | 2416| | of your computer | hands, ontimer | 2417+----------------+------------------------------+-----------------------+ 2418| colormixer | experiment with r, g, b | :func:`ondrag` | 2419+----------------+------------------------------+-----------------------+ 2420| forest | 3 breadth-first trees | randomization | 2421+----------------+------------------------------+-----------------------+ 2422| fractalcurves | Hilbert & Koch curves | recursion | 2423+----------------+------------------------------+-----------------------+ 2424| lindenmayer | ethnomathematics | L-System | 2425| | (indian kolams) | | 2426+----------------+------------------------------+-----------------------+ 2427| minimal_hanoi | Towers of Hanoi | Rectangular Turtles | 2428| | | as Hanoi discs | 2429| | | (shape, shapesize) | 2430+----------------+------------------------------+-----------------------+ 2431| nim | play the classical nim game | turtles as nimsticks, | 2432| | with three heaps of sticks | event driven (mouse, | 2433| | against the computer. | keyboard) | 2434+----------------+------------------------------+-----------------------+ 2435| paint | super minimalistic | :func:`onclick` | 2436| | drawing program | | 2437+----------------+------------------------------+-----------------------+ 2438| peace | elementary | turtle: appearance | 2439| | | and animation | 2440+----------------+------------------------------+-----------------------+ 2441| penrose | aperiodic tiling with | :func:`stamp` | 2442| | kites and darts | | 2443+----------------+------------------------------+-----------------------+ 2444| planet_and_moon| simulation of | compound shapes, | 2445| | gravitational system | :class:`Vec2D` | 2446+----------------+------------------------------+-----------------------+ 2447| round_dance | dancing turtles rotating | compound shapes, clone| 2448| | pairwise in opposite | shapesize, tilt, | 2449| | direction | get_shapepoly, update | 2450+----------------+------------------------------+-----------------------+ 2451| sorting_animate| visual demonstration of | simple alignment, | 2452| | different sorting methods | randomization | 2453+----------------+------------------------------+-----------------------+ 2454| tree | a (graphical) breadth | :func:`clone` | 2455| | first tree (using generators)| | 2456+----------------+------------------------------+-----------------------+ 2457| two_canvases | simple design | turtles on two | 2458| | | canvases | 2459+----------------+------------------------------+-----------------------+ 2460| wikipedia | a pattern from the wikipedia | :func:`clone`, | 2461| | article on turtle graphics | :func:`undo` | 2462+----------------+------------------------------+-----------------------+ 2463| yinyang | another elementary example | :func:`circle` | 2464+----------------+------------------------------+-----------------------+ 2465 2466Have fun! 2467 2468 2469Changes since Python 2.6 2470======================== 2471 2472- The methods :meth:`Turtle.tracer`, :meth:`Turtle.window_width` and 2473 :meth:`Turtle.window_height` have been eliminated. 2474 Methods with these names and functionality are now available only 2475 as methods of :class:`Screen`. The functions derived from these remain 2476 available. (In fact already in Python 2.6 these methods were merely 2477 duplications of the corresponding 2478 :class:`TurtleScreen`/:class:`Screen`-methods.) 2479 2480- The method :meth:`Turtle.fill` has been eliminated. 2481 The behaviour of :meth:`begin_fill` and :meth:`end_fill` 2482 have changed slightly: now every filling-process must be completed with an 2483 ``end_fill()`` call. 2484 2485- A method :meth:`Turtle.filling` has been added. It returns a boolean 2486 value: ``True`` if a filling process is under way, ``False`` otherwise. 2487 This behaviour corresponds to a ``fill()`` call without arguments in 2488 Python 2.6. 2489 2490Changes since Python 3.0 2491======================== 2492 2493- The methods :meth:`Turtle.shearfactor`, :meth:`Turtle.shapetransform` and 2494 :meth:`Turtle.get_shapepoly` have been added. Thus the full range of 2495 regular linear transforms is now available for transforming turtle shapes. 2496 :meth:`Turtle.tiltangle` has been enhanced in functionality: it now can 2497 be used to get or set the tiltangle. :meth:`Turtle.settiltangle` has been 2498 deprecated. 2499 2500- The method :meth:`Screen.onkeypress` has been added as a complement to 2501 :meth:`Screen.onkey` which in fact binds actions to the keyrelease event. 2502 Accordingly the latter has got an alias: :meth:`Screen.onkeyrelease`. 2503 2504- The method :meth:`Screen.mainloop` has been added. So when working only 2505 with Screen and Turtle objects one must not additionally import 2506 :func:`mainloop` anymore. 2507 2508- Two input methods has been added :meth:`Screen.textinput` and 2509 :meth:`Screen.numinput`. These popup input dialogs and return 2510 strings and numbers respectively. 2511 2512- Two example scripts :file:`tdemo_nim.py` and :file:`tdemo_round_dance.py` 2513 have been added to the :file:`Lib/turtledemo` directory. 2514 2515 2516.. doctest:: 2517 :skipif: _tkinter is None 2518 :hide: 2519 2520 >>> for turtle in turtles(): 2521 ... turtle.reset() 2522 >>> turtle.penup() 2523 >>> turtle.goto(-200,25) 2524 >>> turtle.pendown() 2525 >>> turtle.write("No one expects the Spanish Inquisition!", 2526 ... font=("Arial", 20, "normal")) 2527 >>> turtle.penup() 2528 >>> turtle.goto(-100,-50) 2529 >>> turtle.pendown() 2530 >>> turtle.write("Our two chief Turtles are...", 2531 ... font=("Arial", 16, "normal")) 2532 >>> turtle.penup() 2533 >>> turtle.goto(-450,-75) 2534 >>> turtle.write(str(turtles())) 2535