Martin v. Löwis | 8718459 | 2008-06-04 06:29:55 +0000 | [diff] [blame^] | 1 | ======================================== |
Georg Brandl | 6634bf2 | 2008-05-20 07:13:37 +0000 | [diff] [blame] | 2 | :mod:`turtle` --- Turtle graphics for Tk |
| 3 | ======================================== |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 4 | |
Martin v. Löwis | 8718459 | 2008-06-04 06:29:55 +0000 | [diff] [blame^] | 5 | ------------ |
| 6 | Introduction |
| 7 | ------------ |
| 8 | |
| 9 | Turtle graphics is a popular way for introducing programming to |
| 10 | kids. It was part of the original Logo programming language developed |
| 11 | by Wally Feurzig and Seymour Papert in 1966. |
| 12 | |
| 13 | Imagine a robotic turtle starting at (0, 0) in the x-y plane. Give it |
| 14 | the command turtle.forward(15), and it moves (on-screen!) 15 pixels in |
| 15 | the direction it is facing, drawing a line as it moves. Give it the |
| 16 | command turtle.left(25), and it rotates in-place 25 degrees clockwise. |
| 17 | |
| 18 | By combining together these and similar commands, intricate shapes and |
| 19 | pictures can easily be drawn. |
| 20 | |
| 21 | The module turtle.py is an extended reimplementation of turtle.py from |
| 22 | the Python standard distribution up to version Python 2.5. |
| 23 | |
| 24 | It tries to keep the merits of turtle.py and to be (nearly) 100% |
| 25 | compatible with it. This means in the first place to enable the |
| 26 | learning programmer to use all the commands, classes and methods |
| 27 | interactively when using the module from within IDLE run with |
| 28 | the -n switch. |
| 29 | |
| 30 | The turtle module provides turtle graphics primitives, in both |
| 31 | object-oriented and procedure-oriented ways. Because it uses Tkinter |
| 32 | for the underlying graphics, it needs a version of python installed |
| 33 | with Tk support. |
| 34 | |
| 35 | The objectoriented interface uses essentially two+two classes: |
| 36 | |
| 37 | 1. The TurtleScreen class defines graphics windows as a playground for the |
| 38 | drawing turtles. It's constructor needs a Tk-Canvas or a ScrolledCanvas |
| 39 | as argument. It should be used when turtle.py is used as part of some |
| 40 | application. |
| 41 | |
| 42 | Derived from TurtleScreen is the subclass Screen. Screen is implemented |
| 43 | as sort of singleton, so there can exist only one instance of Screen at a |
| 44 | time. It should be used when turtle.py is used as a standalone tool for |
| 45 | doing graphics. |
| 46 | |
| 47 | All methods of TurtleScreen/Screen also exist as functions, i. e. |
| 48 | as part of the procedure-oriented interface. |
| 49 | |
| 50 | 2. RawTurtle (alias: RawPen) defines Turtle-objects which draw on a |
| 51 | TurtleScreen. It's constructor needs a Canvas/ScrolledCanvas/Turtlescreen |
| 52 | as argument, so the RawTurtle objects know where to draw. |
| 53 | |
| 54 | Derived from RawTurtle is the subclass Turtle (alias: Pen), which |
| 55 | draws on "the" Screen - instance which is automatically created, |
| 56 | if not already present. |
| 57 | |
| 58 | All methods of RawTurtle/Turtle also exist as functions, i. e. |
| 59 | part of the procedure-oriented interface. |
| 60 | |
| 61 | The procedural interface uses functions which are derived from the methods |
| 62 | of the classes Screen and Turtle. They have the same names as the |
| 63 | corresponding methods. A screen-object is automativally created |
| 64 | whenever a function derived form a Screen-method is called. An (unnamed) |
| 65 | turtle object is automatically created whenever any of the functions |
| 66 | derived from a Turtle method is called. |
| 67 | |
| 68 | To use multiple turtles an a screen one has to use the objectoriented |
| 69 | interface. |
| 70 | |
| 71 | |
| 72 | IMPORTANT NOTE! |
| 73 | |
| 74 | In the following documentation the argumentlist for functions is given. |
| 75 | --->> Methods, of course, have the additional first argument self <<--- |
| 76 | --->> which is omitted here. <<--- |
| 77 | |
| 78 | |
| 79 | -------------------------------------------------- |
| 80 | OVERVIEW over available Turtle and Screen methods: |
| 81 | -------------------------------------------------- |
| 82 | |
| 83 | (A) TURTLE METHODS: |
| 84 | =================== |
| 85 | |
| 86 | I. TURTLE MOTION |
| 87 | ----------------- |
| 88 | |
| 89 | MOVE AND DRAW |
| 90 | forward | fd |
| 91 | back | bk | back |
| 92 | right | rt |
| 93 | left | lt |
| 94 | goto | setpos | setposition |
| 95 | setx |
| 96 | sety |
| 97 | setheading | seth |
| 98 | home |
| 99 | circle |
| 100 | dot |
| 101 | stamp |
| 102 | clearstamp |
| 103 | clearstamps |
| 104 | undo |
| 105 | speed |
| 106 | |
| 107 | TELL TURTLE'S STATE |
| 108 | position | pos |
| 109 | towards |
| 110 | xcor |
| 111 | ycor |
| 112 | heading |
| 113 | distance |
| 114 | |
| 115 | SETTING AND MEASUREMENT |
| 116 | degrees |
| 117 | radians |
| 118 | |
| 119 | II. PEN CONTROL |
| 120 | --------------- |
| 121 | |
| 122 | DRAWING STATE |
| 123 | pendown | pd | down |
| 124 | penup | pu | up |
| 125 | pensize | width |
| 126 | pen |
| 127 | isdown |
| 128 | |
| 129 | COLOR CONTROL |
| 130 | color |
| 131 | pencolor |
| 132 | fillcolor |
| 133 | |
| 134 | FILLING |
| 135 | fill |
| 136 | begin_fill |
| 137 | end_fill |
| 138 | |
| 139 | MORE DRAWING CONTROL |
| 140 | reset |
| 141 | clear |
| 142 | write |
| 143 | |
| 144 | III. TURTLE STATE |
| 145 | ----------------- |
| 146 | |
| 147 | VISIBILITY |
| 148 | showturtle | st |
| 149 | hideturtle | ht |
| 150 | isvisible |
| 151 | |
| 152 | APPEARANCE |
| 153 | shape |
| 154 | resizemode |
| 155 | shapesize | turtlesize |
| 156 | settiltangle |
| 157 | tiltangle |
| 158 | tilt |
| 159 | |
| 160 | IV. USING EVENTS |
| 161 | ---------------- |
| 162 | onclick |
| 163 | onrelease |
| 164 | ondrag |
| 165 | |
| 166 | V. SPECIAL TURTLE METHODS |
| 167 | ------------------------- |
| 168 | begin_poly |
| 169 | end_poly |
| 170 | get_poly |
| 171 | clone |
| 172 | getturtle | getpen |
| 173 | getscreen |
| 174 | setundobuffer |
| 175 | undobufferentries |
| 176 | tracer |
| 177 | window_width |
| 178 | window_height |
| 179 | |
| 180 | ..EXCURSUS ABOUT THE USE OF COMPOUND SHAPES |
| 181 | ..----------------------------------------- |
| 182 | |
| 183 | (B) METHODS OF TurtleScreen/Screen |
| 184 | ================================== |
| 185 | |
| 186 | I. WINDOW CONTROL |
| 187 | ----------------- |
| 188 | bgcolor |
| 189 | bgpic |
| 190 | clear | clearscreen |
| 191 | reset | resetscreen |
| 192 | screensize |
| 193 | setworldcoordinates |
| 194 | |
| 195 | II. ANIMATION CONTROL |
| 196 | --------------------- |
| 197 | delay |
| 198 | tracer |
| 199 | update |
| 200 | |
| 201 | III. USING SCREEN EVENTS |
| 202 | ------------------------ |
| 203 | listen |
| 204 | onkey |
| 205 | onclick | onscreenclick |
| 206 | ontimer |
| 207 | |
| 208 | IV. SETTINGS AND SPECIAL METHODS |
| 209 | -------------------------------- |
| 210 | mode |
| 211 | colormode |
| 212 | getcanvas |
| 213 | getshapes |
| 214 | register_shape | addshape |
| 215 | turtles |
| 216 | window_height |
| 217 | window_width |
| 218 | |
| 219 | V. METHODS SPECIFIC TO Screen |
| 220 | ============================= |
| 221 | bye() |
| 222 | exitonclick() |
| 223 | setup() |
| 224 | title() |
| 225 | |
| 226 | ---------------end of OVERVIEW --------------------------- |
| 227 | |
| 228 | |
| 229 | |
| 230 | 2. METHODS OF RawTurtle/Turtle AND CORRESPONDING FUNCTIONS |
| 231 | ========================================================== |
| 232 | |
| 233 | (I) TURTLE MOTION: |
| 234 | ------------------ |
| 235 | |
| 236 | (a) --- MOVE (AND DRAW) |
| 237 | |
| 238 | |
| 239 | .. method:: forward(distance) |
| 240 | .. method:: fd(distance) |
| 241 | distance -- a number (integer or float) |
| 242 | |
| 243 | Move the turtle forward by the specified distance, in the direction |
| 244 | the turtle is headed. |
| 245 | |
| 246 | Example (for a Turtle instance named turtle):: |
| 247 | >>> turtle.position() |
| 248 | (0.00, 0.00) |
| 249 | >>> turtle.forward(25) |
| 250 | >>> turtle.position() |
| 251 | (25.00,0.00) |
| 252 | >>> turtle.forward(-75) |
| 253 | >>> turtle.position() |
| 254 | (-50.00,0.00) |
| 255 | |
| 256 | |
| 257 | .. method:: back(distance) |
| 258 | .. method:: bk(distance) |
| 259 | .. method:: backward(distance) |
| 260 | distance -- a number |
| 261 | |
| 262 | call: back(distance) |
| 263 | --or: bk(distance) |
| 264 | --or: backward(distance) |
| 265 | |
| 266 | Move the turtle backward by distance ,opposite to the direction the |
| 267 | turtle is headed. Do not change the turtle's heading. |
| 268 | |
| 269 | Example (for a Turtle instance named turtle):: |
| 270 | |
| 271 | >>> turtle.position() |
| 272 | (0.00, 0.00) |
| 273 | >>> turtle.backward(30) |
| 274 | >>> turtle.position() |
| 275 | (-30.00, 0.00) |
| 276 | |
| 277 | |
| 278 | .. method:: right(angle) |
| 279 | .. method:: rt(angle) |
| 280 | angle -- a number (integer or float) |
| 281 | |
| 282 | Turn turtle right by angle units. (Units are by default degrees, |
| 283 | but can be set via the degrees() and radians() functions.) |
| 284 | Angle orientation depends on mode. (See this.) |
| 285 | |
| 286 | Example (for a Turtle instance named turtle):: |
| 287 | >>> turtle.heading() |
| 288 | 22.0 |
| 289 | >>> turtle.right(45) |
| 290 | >>> turtle.heading() |
| 291 | 337.0 |
| 292 | |
| 293 | |
| 294 | .. method:: left(angle) |
| 295 | .. method:: lt(angle) |
| 296 | angle -- a number (integer or float) |
| 297 | |
| 298 | Turn turtle left by angle units. (Units are by default degrees, |
| 299 | but can be set via the degrees() and radians() functions.) |
| 300 | Angle orientation depends on mode. (See this.) |
| 301 | |
| 302 | Example (for a Turtle instance named turtle):: |
| 303 | >>> turtle.heading() |
| 304 | 22.0 |
| 305 | >>> turtle.left(45) |
| 306 | >>> turtle.heading() |
| 307 | 67.0 |
| 308 | |
| 309 | .. method:: goto(x, y=None) |
| 310 | .. method:: setpos(x, y=None) |
| 311 | .. method:: setposition(x, y=None) |
| 312 | x -- a number or a pair/vector of numbers |
| 313 | y -- a number None |
| 314 | |
| 315 | call: goto(x, y) # two coordinates |
| 316 | --or: goto((x, y)) # a pair (tuple) of coordinates |
| 317 | --or: goto(vec) # e.g. as returned by pos() |
| 318 | |
| 319 | Move turtle to an absolute position. If the pen is down, |
| 320 | draw line. Do not change the turtle's orientation. |
| 321 | |
| 322 | Example (for a Turtle instance named turtle):: |
| 323 | >>> tp = turtle.pos() |
| 324 | >>> tp |
| 325 | (0.00, 0.00) |
| 326 | >>> turtle.setpos(60,30) |
| 327 | >>> turtle.pos() |
| 328 | (60.00,30.00) |
| 329 | >>> turtle.setpos((20,80)) |
| 330 | >>> turtle.pos() |
| 331 | (20.00,80.00) |
| 332 | >>> turtle.setpos(tp) |
| 333 | >>> turtle.pos() |
| 334 | (0.00,0.00) |
| 335 | |
| 336 | |
| 337 | .. method:: setx(x) |
| 338 | x -- a number (integer or float) |
| 339 | |
| 340 | Set the turtle's first coordinate to x, leave second coordinate |
| 341 | unchanged. |
| 342 | |
| 343 | Example (for a Turtle instance named turtle):: |
| 344 | >>> turtle.position() |
| 345 | (0.00, 240.00) |
| 346 | >>> turtle.setx(10) |
| 347 | >>> turtle.position() |
| 348 | (10.00, 240.00) |
| 349 | |
| 350 | |
| 351 | .. method:: sety(y) |
| 352 | y -- a number (integer or float) |
| 353 | |
| 354 | Set the turtle's first coordinate to x, leave second coordinate |
| 355 | unchanged. |
| 356 | |
| 357 | Example (for a Turtle instance named turtle):: |
| 358 | >>> turtle.position() |
| 359 | (0.00, 40.00) |
| 360 | >>> turtle.sety(-10) |
| 361 | >>> turtle.position() |
| 362 | (0.00, -10.00) |
| 363 | |
| 364 | |
| 365 | .. method:: setheading(to_angle) |
| 366 | .. method:: seth(to_angle) |
| 367 | to_angle -- a number (integer or float) |
| 368 | |
| 369 | Set the orientation of the turtle to to_angle. |
| 370 | Here are some common directions in degrees: |
| 371 | |
| 372 | =================== ==================== |
| 373 | standard - mode logo-mode |
| 374 | =================== ==================== |
| 375 | 0 - east 0 - north |
| 376 | 90 - north 90 - east |
| 377 | 180 - west 180 - south |
| 378 | 270 - south 270 - west |
| 379 | =================== ==================== |
| 380 | |
| 381 | Example (for a Turtle instance named turtle):: |
| 382 | >>> turtle.setheading(90) |
| 383 | >>> turtle.heading() |
| 384 | 90 |
| 385 | |
| 386 | |
| 387 | .. method:: home(): |
| 388 | Move turtle to the origin - coordinates (0,0) and set it's |
| 389 | heading to it's start-orientation (which depends on mode). |
| 390 | |
| 391 | Example (for a Turtle instance named turtle):: |
| 392 | >>> turtle.home() |
| 393 | |
| 394 | |
| 395 | .. method:: circle(radius, extent=None, steps=None) |
| 396 | radius -- a number |
| 397 | extent (optional) -- a number |
| 398 | steps (optional) -- an integer |
| 399 | |
| 400 | Draw a circle with given radius. The center is radius units left |
| 401 | of the turtle; extent - an angle - determines which part of the |
| 402 | circle is drawn. If extent is not given, draw the entire circle. |
| 403 | If extent is not a full circle, one endpoint of the arc is the |
| 404 | current pen position. Draw the arc in counterclockwise direction |
| 405 | if radius is positive, otherwise in clockwise direction. Finally |
| 406 | the direction of the turtle is changed by the amount of extent. |
| 407 | |
| 408 | As the circle is approximated by an inscribed regular polygon, |
| 409 | steps determines the number of steps to use. If not given, |
| 410 | it will be calculated automatically. Maybe used to draw regular |
| 411 | polygons. |
| 412 | |
| 413 | call: circle(radius) # full circle |
| 414 | --or: circle(radius, extent) # arc |
| 415 | --or: circle(radius, extent, steps) |
| 416 | --or: circle(radius, steps=6) # 6-sided polygon |
| 417 | |
| 418 | Example (for a Turtle instance named turtle):: |
| 419 | >>> turtle.circle(50) |
| 420 | >>> turtle.circle(120, 180) # semicircle |
| 421 | |
| 422 | |
| 423 | .. method:: dot(size=None, *color) |
| 424 | size -- an integer >= 1 (if given) |
| 425 | color -- a colorstring or a numeric color tuple |
| 426 | |
| 427 | Draw a circular dot with diameter size, using color. If size |
| 428 | is not given, the maximum of pensize+4 and 2*pensize is used. |
| 429 | |
| 430 | Example (for a Turtle instance named turtle):: |
| 431 | >>> turtle.dot() |
| 432 | >>> turtle.fd(50); turtle.dot(20, "blue"); turtle.fd(50) |
| 433 | |
| 434 | |
| 435 | .. method:: stamp(): |
| 436 | Stamp a copy of the turtle shape onto the canvas at the current |
| 437 | turtle position. Return a stamp_id for that stamp, which can be |
| 438 | used to delete it by calling clearstamp(stamp_id). |
| 439 | |
| 440 | Example (for a Turtle instance named turtle):: |
| 441 | >>> turtle.color("blue") |
| 442 | >>> turtle.stamp() |
| 443 | 13 |
| 444 | >>> turtle.fd(50) |
| 445 | |
| 446 | |
| 447 | .. method:: clearstamp(stampid): |
| 448 | stampid - an integer, must be return value of previous stamp() call. |
| 449 | |
| 450 | Delete stamp with given stampid |
| 451 | |
| 452 | Example (for a Turtle instance named turtle):: |
| 453 | >>> turtle.color("blue") |
| 454 | >>> astamp = turtle.stamp() |
| 455 | >>> turtle.fd(50) |
| 456 | >>> turtle.clearstamp(astamp) |
| 457 | |
| 458 | |
| 459 | .. method:: clearstamps(n=None): |
| 460 | n -- an integer |
| 461 | |
| 462 | Delete all or first/last n of turtle's stamps. |
| 463 | If n is None, delete all of pen's stamps, |
| 464 | else if n > 0 delete first n stamps |
| 465 | else if n < 0 delete last n stamps. |
| 466 | |
| 467 | Example (for a Turtle instance named turtle):: |
| 468 | >>> for i in range(8): |
| 469 | ... turtle.stamp(); turtle.fd(30) |
| 470 | >>> turtle.clearstamps(2) |
| 471 | >>> turtle.clearstamps(-2) |
| 472 | >>> turtle.clearstamps() |
| 473 | |
| 474 | |
| 475 | .. method:: undo(): |
| 476 | undo (repeatedly) the last turtle action(s). Number of available |
| 477 | undo actions is determined by the size of the undobuffer. |
| 478 | |
| 479 | Example (for a Turtle instance named turtle):: |
| 480 | >>> for i in range(4): |
| 481 | turtle.fd(50); turtle.lt(80) |
| 482 | |
| 483 | >>> for i in range(8): |
| 484 | turtle.undo() |
| 485 | |
| 486 | |
| 487 | .. method:: speed(speed=None): |
| 488 | speed -- an integer in the range 0..10 or a speedstring (see below) |
| 489 | |
| 490 | Set the turtle's speed to an integer value in the range 0 .. 10. |
| 491 | If no argument is given: return current speed. |
| 492 | |
| 493 | If input is a number greater than 10 or smaller than 0.5, |
| 494 | speed is set to 0. |
| 495 | Speedstrings are mapped to speedvalues as follows: |
| 496 | |
| 497 | * 'fastest' : 0 |
| 498 | * 'fast' : 10 |
| 499 | * 'normal' : 6 |
| 500 | * 'slow' : 3 |
| 501 | * 'slowest' : 1 |
| 502 | |
| 503 | speeds from 1 to 10 enforce increasingly faster animation of |
| 504 | line drawing and turtle turning. |
| 505 | |
| 506 | Attention: |
| 507 | speed = 0 : *no* animation takes place. forward/back makes turtle jump |
| 508 | and likewise left/right make the turtle turn instantly. |
| 509 | |
| 510 | Example (for a Turtle instance named turtle):: |
| 511 | >>> turtle.speed(3) |
| 512 | |
| 513 | |
| 514 | TELL TURTLE'S STATE |
| 515 | ------------------- |
| 516 | |
| 517 | |
| 518 | .. method:: position() |
| 519 | .. method:: pos() |
| 520 | Return the turtle's current location (x,y) (as a Vec2D-vector) |
| 521 | |
| 522 | Example (for a Turtle instance named turtle):: |
| 523 | >>> turtle.pos() |
| 524 | (0.00, 240.00) |
| 525 | |
| 526 | |
| 527 | .. method:: towards(x, y=None) |
| 528 | x -- a number or a pair/vector of numbers or a turtle instance |
| 529 | y -- a number None None |
| 530 | |
| 531 | call: distance(x, y) # two coordinates |
| 532 | --or: distance((x, y)) # a pair (tuple) of coordinates |
| 533 | --or: distance(vec) # e.g. as returned by pos() |
| 534 | --or: distance(mypen) # where mypen is another turtle |
| 535 | |
| 536 | Return the angle, between the line from turtle-position to position |
| 537 | specified by x, y and the turtle's start orientation. (Depends on |
| 538 | modes - "standard"/"world" or "logo") |
| 539 | |
| 540 | Example (for a Turtle instance named turtle):: |
| 541 | >>> turtle.pos() |
| 542 | (10.00, 10.00) |
| 543 | >>> turtle.towards(0,0) |
| 544 | 225.0 |
| 545 | |
| 546 | |
| 547 | .. method:: xcor() |
| 548 | Return the turtle's x coordinate |
| 549 | |
| 550 | Example (for a Turtle instance named turtle):: |
| 551 | >>> reset() |
| 552 | >>> turtle.left(60) |
| 553 | >>> turtle.forward(100) |
| 554 | >>> print turtle.xcor() |
| 555 | 50.0 |
| 556 | |
| 557 | |
| 558 | .. method:: ycor() |
| 559 | Return the turtle's y coordinate |
| 560 | |
| 561 | Example (for a Turtle instance named turtle):: |
| 562 | >>> reset() |
| 563 | >>> turtle.left(60) |
| 564 | >>> turtle.forward(100) |
| 565 | >>> print turtle.ycor() |
| 566 | 86.6025403784 |
| 567 | |
| 568 | |
| 569 | .. method:: heading() |
| 570 | Return the turtle's current heading (value depends on mode). |
| 571 | |
| 572 | Example (for a Turtle instance named turtle):: |
| 573 | >>> turtle.left(67) |
| 574 | >>> turtle.heading() |
| 575 | 67.0 |
| 576 | |
| 577 | |
| 578 | .. method:: distance(x, y=None) |
| 579 | x -- a number or a pair/vector of numbers or a turtle instance |
| 580 | y -- a number None None |
| 581 | |
| 582 | call: distance(x, y) # two coordinates |
| 583 | --or: distance((x, y)) # a pair (tuple) of coordinates |
| 584 | --or: distance(vec) # e.g. as returned by pos() |
| 585 | --or: distance(mypen) # where mypen is another turtle |
| 586 | |
| 587 | Return the distance from the turtle to (x,y) in turtle step units. |
| 588 | |
| 589 | Example (for a Turtle instance named turtle):: |
| 590 | >>> turtle.pos() |
| 591 | (0.00, 0.00) |
| 592 | >>> turtle.distance(30,40) |
| 593 | 50.0 |
| 594 | >>> joe = Turtle() |
| 595 | >>> joe.forward(77) |
| 596 | >>> turtle.distance(joe) |
| 597 | 77.0 |
| 598 | |
| 599 | |
| 600 | SETTINGS FOR MEASUREMENT |
| 601 | |
| 602 | |
| 603 | .. method:: degrees(fullcircle=360.0) |
| 604 | fullcircle - a number |
| 605 | |
| 606 | Set angle measurement units, i. e. set number |
| 607 | of 'degrees' for a full circle. Dafault value is |
| 608 | 360 degrees. |
| 609 | |
| 610 | Example (for a Turtle instance named turtle):: |
| 611 | >>> turtle.left(90) |
| 612 | >>> turtle.heading() |
| 613 | 90 |
| 614 | >>> turtle.degrees(400.0) # angle measurement in gon |
| 615 | >>> turtle.heading() |
| 616 | 100 |
| 617 | |
| 618 | |
| 619 | .. method:: radians() |
| 620 | Set the angle measurement units to radians. |
| 621 | |
| 622 | Example (for a Turtle instance named turtle):: |
| 623 | >>> turtle.heading() |
| 624 | 90 |
| 625 | >>> turtle.radians() |
| 626 | >>> turtle.heading() |
| 627 | 1.5707963267948966 |
| 628 | |
| 629 | |
| 630 | (II) PEN CONTROL: |
| 631 | ----------------- |
| 632 | |
| 633 | DRAWING STATE |
| 634 | |
| 635 | |
| 636 | .. method:: pendown() |
| 637 | .. method:: pd() |
| 638 | .. method:: down() |
| 639 | Pull the pen down -- drawing when moving. |
| 640 | |
| 641 | Example (for a Turtle instance named turtle):: |
| 642 | >>> turtle.pendown() |
| 643 | |
| 644 | |
| 645 | .. method:: penup() |
| 646 | .. method:: pu() |
| 647 | .. method:: up() |
| 648 | Pull the pen up -- no drawing when moving. |
| 649 | |
| 650 | Example (for a Turtle instance named turtle):: |
| 651 | >>> turtle.penup() |
| 652 | |
| 653 | |
| 654 | .. method:: pensize(width=None) |
| 655 | .. method:: width(width=None) |
| 656 | width -- positive number |
| 657 | |
| 658 | Set the line thickness to width or return it. If resizemode is set |
| 659 | to "auto" and turtleshape is a polygon, that polygon is drawn with |
| 660 | the same line thickness. If no argument is given, the current pensize |
| 661 | is returned. |
| 662 | |
| 663 | Example (for a Turtle instance named turtle):: |
| 664 | >>> turtle.pensize() |
| 665 | 1 |
| 666 | turtle.pensize(10) # from here on lines of width 10 are drawn |
| 667 | |
| 668 | |
| 669 | .. method:: pen(pen=None, **pendict) |
| 670 | pen -- a dictionary with some or all of the below listed keys. |
| 671 | **pendict -- one or more keyword-arguments with the below |
| 672 | listed keys as keywords. |
| 673 | |
| 674 | Return or set the pen's attributes in a 'pen-dictionary' |
| 675 | with the following key/value pairs: |
| 676 | |
| 677 | * "shown" : True/False |
| 678 | * "pendown" : True/False |
| 679 | * "pencolor" : color-string or color-tuple |
| 680 | * "fillcolor" : color-string or color-tuple |
| 681 | * "pensize" : positive number |
| 682 | * "speed" : number in range 0..10 |
| 683 | * "resizemode" : "auto" or "user" or "noresize" |
| 684 | * "stretchfactor": (positive number, positive number) |
| 685 | * "outline" : positive number |
| 686 | * "tilt" : number |
| 687 | |
| 688 | This dicionary can be used as argument for a subsequent |
| 689 | pen()-call to restore the former pen-state. Moreover one |
| 690 | or more of these attributes can be provided as keyword-arguments. |
| 691 | This can be used to set several pen attributes in one statement. |
| 692 | |
| 693 | Examples (for a Turtle instance named turtle):: |
| 694 | >>> turtle.pen(fillcolor="black", pencolor="red", pensize=10) |
| 695 | >>> turtle.pen() |
| 696 | {'pensize': 10, 'shown': True, 'resizemode': 'auto', 'outline': 1, |
| 697 | 'pencolor': 'red', 'pendown': True, 'fillcolor': 'black', |
| 698 | 'stretchfactor': (1,1), 'speed': 3} |
| 699 | >>> penstate=turtle.pen() |
| 700 | >>> turtle.color("yellow","") |
| 701 | >>> turtle.penup() |
| 702 | >>> turtle.pen() |
| 703 | {'pensize': 10, 'shown': True, 'resizemode': 'auto', 'outline': 1, |
| 704 | 'pencolor': 'yellow', 'pendown': False, 'fillcolor': '', |
| 705 | 'stretchfactor': (1,1), 'speed': 3} |
| 706 | >>> p.pen(penstate, fillcolor="green") |
| 707 | >>> p.pen() |
| 708 | {'pensize': 10, 'shown': True, 'resizemode': 'auto', 'outline': 1, |
| 709 | 'pencolor': 'red', 'pendown': True, 'fillcolor': 'green', |
| 710 | 'stretchfactor': (1,1), 'speed': 3} |
| 711 | |
| 712 | |
| 713 | .. method:: isdown(self): |
| 714 | Return True if pen is down, False if it's up. |
| 715 | |
| 716 | Example (for a Turtle instance named turtle):: |
| 717 | >>> turtle.penup() |
| 718 | >>> turtle.isdown() |
| 719 | False |
| 720 | >>> turtle.pendown() |
| 721 | >>> turtle.isdown() |
| 722 | True |
| 723 | |
| 724 | |
| 725 | COLOR CONTROL |
| 726 | |
| 727 | |
| 728 | .. method:: color(*args) |
| 729 | Return or set pencolor and fillcolor. |
| 730 | |
| 731 | Several input formats are allowed. They use 0, 1, 2, or 3 arguments |
| 732 | as follows: |
| 733 | |
| 734 | - color() |
| 735 | Return the current pencolor and the current fillcolor |
| 736 | as a pair of color specification strings as are returned |
| 737 | by pencolor and fillcolor. |
| 738 | - color(colorstring), color((r,g,b)), color(r,g,b) |
| 739 | inputs as in pencolor, set both, fillcolor and pencolor, |
| 740 | to the given value. |
| 741 | - color(colorstring1, colorstring2), |
| 742 | - color((r1,g1,b1), (r2,g2,b2)) |
| 743 | equivalent to pencolor(colorstring1) and fillcolor(colorstring2) |
| 744 | and analogously, if the other input format is used. |
| 745 | |
| 746 | If turtleshape is a polygon, outline and interior of that polygon |
| 747 | is drawn with the newly set colors. |
| 748 | For more info see: pencolor, fillcolor |
| 749 | |
| 750 | Example (for a Turtle instance named turtle):: |
| 751 | >>> turtle.color('red', 'green') |
| 752 | >>> turtle.color() |
| 753 | ('red', 'green') |
| 754 | >>> colormode(255) |
| 755 | >>> color((40, 80, 120), (160, 200, 240)) |
| 756 | >>> color() |
| 757 | ('#285078', '#a0c8f0') |
| 758 | |
| 759 | |
| 760 | .. method:: pencolor(*args) |
| 761 | Return or set the pencolor. |
| 762 | |
| 763 | Four input formats are allowed: |
| 764 | |
| 765 | - pencolor() |
| 766 | Return the current pencolor as color specification string, |
| 767 | possibly in hex-number format (see example). |
| 768 | May be used as input to another color/pencolor/fillcolor call. |
| 769 | - pencolor(colorstring) |
| 770 | s is a Tk color specification string, such as "red" or "yellow" |
| 771 | - pencolor((r, g, b)) |
| 772 | *a tuple* of r, g, and b, which represent, an RGB color, |
| 773 | and each of r, g, and b are in the range 0..colormode, |
| 774 | where colormode is either 1.0 or 255 |
| 775 | - pencolor(r, g, b) |
| 776 | r, g, and b represent an RGB color, and each of r, g, and b |
| 777 | are in the range 0..colormode |
| 778 | |
| 779 | If turtleshape is a polygon, the outline of that polygon is drawn |
| 780 | with the newly set pencolor. |
| 781 | |
| 782 | Example (for a Turtle instance named turtle):: |
| 783 | >>> turtle.pencolor('brown') |
| 784 | >>> tup = (0.2, 0.8, 0.55) |
| 785 | >>> turtle.pencolor(tup) |
| 786 | >>> turtle.pencolor() |
| 787 | '#33cc8c' |
| 788 | |
| 789 | |
| 790 | .. method:: fillcolor(*args) |
| 791 | """ Return or set the fillcolor. |
| 792 | |
| 793 | Four input formats are allowed: |
| 794 | |
| 795 | - fillcolor() |
| 796 | Return the current fillcolor as color specification string, |
| 797 | possibly in hex-number format (see example). |
| 798 | May be used as input to another color/pencolor/fillcolor call. |
| 799 | - fillcolor(colorstring) |
| 800 | s is a Tk color specification string, such as "red" or "yellow" |
| 801 | - fillcolor((r, g, b)) |
| 802 | *a tuple* of r, g, and b, which represent, an RGB color, |
| 803 | and each of r, g, and b are in the range 0..colormode, |
| 804 | where colormode is either 1.0 or 255 |
| 805 | - fillcolor(r, g, b) |
| 806 | r, g, and b represent an RGB color, and each of r, g, and b |
| 807 | are in the range 0..colormode |
| 808 | |
| 809 | If turtleshape is a polygon, the interior of that polygon is drawn |
| 810 | with the newly set fillcolor. |
| 811 | |
| 812 | Example (for a Turtle instance named turtle):: |
| 813 | >>> turtle.fillcolor('violet') |
| 814 | >>> col = turtle.pencolor() |
| 815 | >>> turtle.fillcolor(col) |
| 816 | >>> turtle.fillcolor(0, .5, 0) |
| 817 | |
| 818 | |
| 819 | See also: Screen method colormode() |
| 820 | |
| 821 | |
| 822 | FILLING |
| 823 | |
| 824 | |
| 825 | .. method:: fill(flag) |
| 826 | flag -- True/False (or 1/0 respectively) |
| 827 | |
| 828 | Call fill(True) before drawing the shape you want to fill, |
| 829 | and fill(False) when done. When used without argument: return |
| 830 | fillstate (True if filling, False else). |
| 831 | |
| 832 | Example (for a Turtle instance named turtle):: |
| 833 | >>> turtle.fill(True) |
| 834 | >>> for _ in range(3): |
| 835 | ... turtle.forward(100) |
| 836 | ... turtle.left(120) |
| 837 | ... |
| 838 | >>> turtle.fill(False) |
| 839 | |
| 840 | |
| 841 | .. method:: begin_fill() |
| 842 | Called just before drawing a shape to be filled. |
| 843 | |
| 844 | Example (for a Turtle instance named turtle):: |
| 845 | >>> turtle.color("black", "red") |
| 846 | >>> turtle.begin_fill() |
| 847 | >>> turtle.circle(60) |
| 848 | >>> turtle.end_fill() |
| 849 | |
| 850 | |
| 851 | .. method:: end_fill() |
| 852 | Fill the shape drawn after the call begin_fill(). |
| 853 | |
| 854 | Example: See begin_fill() |
| 855 | |
| 856 | |
| 857 | MORE DRAWING CONTROL |
| 858 | |
| 859 | |
| 860 | .. method:: reset() |
| 861 | Delete the turtle's drawings from the screen, re-center the turtle |
| 862 | and set variables to the default values. |
| 863 | |
| 864 | Example (for a Turtle instance named turtle):: |
| 865 | >>> turtle.position() |
| 866 | (0.00,-22.00) |
| 867 | >>> turtle.heading() |
| 868 | 100.0 |
| 869 | >>> turtle.reset() |
| 870 | >>> turtle.position() |
| 871 | (0.00,0.00) |
| 872 | >>> turtle.heading() |
| 873 | 0.0 |
| 874 | |
| 875 | |
| 876 | .. method:: clear() |
| 877 | Delete the turtle's drawings from the screen. Do not move turtle. |
| 878 | State and position of the turtle as well as drawings of other |
| 879 | turtles are not affected. |
| 880 | |
| 881 | Examples (for a Turtle instance named turtle): |
| 882 | >>> turtle.clear() |
| 883 | |
| 884 | |
| 885 | .. method:: write(arg, move=False, align='left', font=('Arial', 8, 'normal')) |
| 886 | arg -- info, which is to be written to the TurtleScreen |
| 887 | move (optional) -- True/False |
| 888 | align (optional) -- one of the strings "left", "center" or right" |
| 889 | font (optional) -- a triple (fontname, fontsize, fonttype) |
| 890 | |
| 891 | Write text - the string representation of arg - at the current |
| 892 | turtle position according to align ("left", "center" or right") |
| 893 | and with the given font. |
| 894 | If move is True, the pen is moved to the bottom-right corner |
| 895 | of the text. By default, move is False. |
| 896 | |
| 897 | Example (for a Turtle instance named turtle):: |
| 898 | >>> turtle.write('Home = ', True, align="center") |
| 899 | >>> turtle.write((0,0), True) |
| 900 | |
| 901 | |
| 902 | TURTLE STATE: |
| 903 | ------------- |
| 904 | |
| 905 | VISIBILITY |
| 906 | |
| 907 | |
| 908 | .. method:: showturtle() |
| 909 | .. method:: st() |
| 910 | Makes the turtle visible. |
| 911 | |
| 912 | Example (for a Turtle instance named turtle):: |
| 913 | >>> turtle.hideturtle() |
| 914 | >>> turtle.showturtle() |
| 915 | |
| 916 | |
| 917 | .. method:: hideturtle() |
| 918 | .. method:: ht() |
| 919 | Makes the turtle invisible. |
| 920 | It's a good idea to do this while you're in the middle of |
| 921 | doing some complex drawing, because hiding the turtle speeds |
| 922 | up the drawing observably. |
| 923 | |
| 924 | Example (for a Turtle instance named turtle):: |
| 925 | >>> turtle.hideturtle() |
| 926 | |
| 927 | |
| 928 | .. method:: isvisible(self): |
| 929 | Return True if the Turtle is shown, False if it's hidden. |
| 930 | |
| 931 | Example (for a Turtle instance named turtle):: |
| 932 | >>> turtle.hideturtle() |
| 933 | >>> print turtle.isvisible(): |
| 934 | False |
| 935 | |
| 936 | |
| 937 | APPEARANCE |
| 938 | |
| 939 | |
| 940 | .. method:: shape(name=None) |
| 941 | name -- a string, which is a valid shapename |
| 942 | |
| 943 | Set turtle shape to shape with given name or, if name is not given, |
| 944 | return name of current shape. |
| 945 | Shape with name must exist in the TurtleScreen's shape dictionary. |
| 946 | Initially there are the following polygon shapes: |
| 947 | 'arrow', 'turtle', 'circle', 'square', 'triangle', 'classic'. |
| 948 | To learn about how to deal with shapes see Screen-method register_shape. |
| 949 | |
| 950 | Example (for a Turtle instance named turtle):: |
| 951 | >>> turtle.shape() |
| 952 | 'arrow' |
| 953 | >>> turtle.shape("turtle") |
| 954 | >>> turtle.shape() |
| 955 | 'turtle' |
| 956 | |
| 957 | |
| 958 | .. method:: resizemode(rmode=None) |
| 959 | rmode -- one of the strings "auto", "user", "noresize" |
| 960 | |
| 961 | Set resizemode to one of the values: "auto", "user", "noresize". |
| 962 | If rmode is not given, return current resizemode. |
| 963 | Different resizemodes have the following effects: |
| 964 | |
| 965 | - "auto" adapts the appearance of the turtle |
| 966 | corresponding to the value of pensize. |
| 967 | - "user" adapts the appearance of the turtle according to the |
| 968 | values of stretchfactor and outlinewidth (outline), |
| 969 | which are set by shapesize() |
| 970 | - "noresize" no adaption of the turtle's appearance takes place. |
| 971 | |
| 972 | resizemode("user") is called by a shapesize when used with arguments. |
| 973 | |
| 974 | Examples (for a Turtle instance named turtle):: |
| 975 | >>> turtle.resizemode("noresize") |
| 976 | >>> turtle.resizemode() |
| 977 | 'noresize' |
| 978 | |
| 979 | |
| 980 | .. method:: shapesize(stretch_wid=None, stretch_len=None, outline=None): |
| 981 | stretch_wid -- positive number |
| 982 | stretch_len -- positive number |
| 983 | outline -- positive number |
| 984 | |
| 985 | Return or set the pen's attributes x/y-stretchfactors and/or outline. |
| 986 | Set resizemode to "user". |
| 987 | If and only if resizemode is set to "user", the turtle will be |
| 988 | displayed stretched according to its stretchfactors: |
| 989 | stretch_wid is stretchfactor perpendicular to it's orientation, |
| 990 | stretch_len is stretchfactor in direction of it's orientation, |
| 991 | outline determines the width of the shapes's outline. |
| 992 | |
| 993 | Examples (for a Turtle instance named turtle):: |
| 994 | >>> turtle.resizemode("user") |
| 995 | >>> turtle.shapesize(5, 5, 12) |
| 996 | >>> turtle.shapesize(outline=8) |
| 997 | |
| 998 | |
| 999 | .. method:: tilt(angle) |
| 1000 | angle - a number |
| 1001 | |
| 1002 | Rotate the turtleshape by angle from its current tilt-angle, |
| 1003 | but do NOT change the turtle's heading (direction of movement). |
| 1004 | |
| 1005 | Examples (for a Turtle instance named turtle):: |
| 1006 | >>> turtle.shape("circle") |
| 1007 | >>> turtle.shapesize(5,2) |
| 1008 | >>> turtle.tilt(30) |
| 1009 | >>> turtle.fd(50) |
| 1010 | >>> turtle.tilt(30) |
| 1011 | >>> turtle.fd(50) |
| 1012 | |
| 1013 | |
| 1014 | .. method:: settiltangle(angle) |
| 1015 | angle -- number |
| 1016 | |
| 1017 | Rotate the turtleshape to point in the direction specified by angle, |
| 1018 | regardless of its current tilt-angle. DO NOT change the turtle's |
| 1019 | heading (direction of movement). |
| 1020 | |
| 1021 | Examples (for a Turtle instance named turtle):: |
| 1022 | >>> turtle.shape("circle") |
| 1023 | >>> turtle.shapesize(5,2) |
| 1024 | >>> turtle.settiltangle(45) |
| 1025 | >>> stamp() |
| 1026 | >>> turtle.fd(50) |
| 1027 | >>> turtle.settiltangle(-45) |
| 1028 | >>> stamp() |
| 1029 | >>> turtle.fd(50) |
| 1030 | |
| 1031 | |
| 1032 | .. method:: tiltangle() |
| 1033 | Return the current tilt-angle, i. e. the angle between the |
| 1034 | orientation of the turtleshape and the heading of the turtle |
| 1035 | (it's direction of movement). |
| 1036 | |
| 1037 | Examples (for a Turtle instance named turtle):: |
| 1038 | >>> turtle.shape("circle") |
| 1039 | >>> turtle.shapesize(5,2) |
| 1040 | >>> turtle.tilt(45) |
| 1041 | >>> turtle.tiltangle() |
| 1042 | 45 |
| 1043 | |
| 1044 | |
| 1045 | IV. USING EVENTS |
| 1046 | ---------------- |
| 1047 | |
| 1048 | |
| 1049 | .. method:: onclick(fun, btn=1, add=None) |
| 1050 | fun -- a function with two arguments, to which will be assigned |
| 1051 | the coordinates of the clicked point on the canvas. |
| 1052 | num -- number of the mouse-button defaults to 1 (left mouse button). |
| 1053 | add -- True or False. If True, new binding will be added, otherwise |
| 1054 | it will replace a former binding. |
| 1055 | |
| 1056 | Bind fun to mouse-click event on this turtle on canvas. |
| 1057 | If fun is None, existing bindings are removed. |
| 1058 | Example for the anonymous turtle, i. e. the procedural way:: |
| 1059 | |
| 1060 | >>> def turn(x, y): |
| 1061 | left(360) |
| 1062 | |
| 1063 | >>> onclick(turn) # Now clicking into the turtle will turn it. |
| 1064 | >>> onclick(None) # event-binding will be removed |
| 1065 | |
| 1066 | |
| 1067 | .. method:: onrelease(fun, btn=1, add=None): |
| 1068 | """ |
| 1069 | Arguments: |
| 1070 | fun -- a function with two arguments, to which will be assigned |
| 1071 | the coordinates of the clicked point on the canvas. |
| 1072 | num -- number of the mouse-button defaults to 1 (left mouse button). |
| 1073 | add -- True or False. If True, new binding will be added, otherwise |
| 1074 | it will replace a former binding. |
| 1075 | |
| 1076 | Bind fun to mouse-button-release event on this turtle on canvas. |
| 1077 | If fun is None, existing bindings are removed. |
| 1078 | |
| 1079 | Example (for a MyTurtle instance named turtle): |
| 1080 | >>> class MyTurtle(Turtle): |
| 1081 | ... def glow(self,x,y): |
| 1082 | ... self.fillcolor("red") |
| 1083 | ... def unglow(self,x,y): |
| 1084 | ... self.fillcolor("") |
| 1085 | ... |
| 1086 | >>> turtle = MyTurtle() |
| 1087 | >>> turtle.onclick(turtle.glow) |
| 1088 | >>> turtle.onrelease(turtle.unglow) |
| 1089 | ### clicking on turtle turns fillcolor red, |
| 1090 | ### unclicking turns it to transparent. |
| 1091 | |
| 1092 | |
| 1093 | .. method:: ondrag(fun, btn=1, add=None): |
| 1094 | fun -- a function with two arguments, to which will be assigned |
| 1095 | the coordinates of the clicked point on the canvas. |
| 1096 | num -- number of the mouse-button defaults to 1 (left mouse button). |
| 1097 | add -- True or False. If True, new binding will be added, otherwise |
| 1098 | it will replace a former binding. |
| 1099 | |
| 1100 | Bind fun to mouse-move event on this turtle on canvas. |
| 1101 | If fun is None, existing bindings are removed. |
| 1102 | |
| 1103 | Remark: Every sequence of mouse-move-events on a turtle is preceded |
| 1104 | by a mouse-click event on that turtle. |
| 1105 | If fun is None, existing bindings are removed. |
| 1106 | |
| 1107 | Example (for a Turtle instance named turtle): |
| 1108 | >>> turtle.ondrag(turtle.goto) |
| 1109 | ### Subsequently clicking and dragging a Turtle will move it across |
| 1110 | ### the screen thereby producing handdrawings (if pen is down). |
| 1111 | |
| 1112 | |
| 1113 | V. SPECIAL TURTLE METHODS |
| 1114 | -------------------------- |
| 1115 | |
| 1116 | |
| 1117 | .. method:: begin_poly(): |
| 1118 | Start recording the vertices of a polygon. Current turtle position |
| 1119 | is first vertex of polygon. |
| 1120 | |
| 1121 | Example (for a Turtle instance named turtle): |
| 1122 | >>> turtle.begin_poly() |
| 1123 | |
| 1124 | |
| 1125 | .. method:: end_poly(): |
| 1126 | Stop recording the vertices of a polygon. Current turtle position is |
| 1127 | last vertex of polygon. This will be connected with the first vertex. |
| 1128 | |
| 1129 | Example (for a Turtle instance named turtle): |
| 1130 | >>> turtle.end_poly() |
| 1131 | |
| 1132 | |
| 1133 | .. method:: get_poly(): |
| 1134 | Return the lastly recorded polygon. |
| 1135 | |
| 1136 | Example (for a Turtle instance named turtle): |
| 1137 | >>> p = turtle.get_poly() |
| 1138 | >>> turtle.register_shape("myFavouriteShape", p) |
| 1139 | |
| 1140 | |
| 1141 | .. method:: clone(): |
| 1142 | Create and return a clone of the turtle with same position, heading |
| 1143 | and turtle properties. |
| 1144 | |
| 1145 | Example (for a Turtle instance named mick): |
| 1146 | mick = Turtle() |
| 1147 | joe = mick.clone() |
| 1148 | |
| 1149 | |
| 1150 | .. method:: getturtle(): |
| 1151 | Return the Turtleobject itself. |
| 1152 | Only reasonable use: as a function to return the 'anonymous turtle': |
| 1153 | |
| 1154 | Example: |
| 1155 | >>> pet = getturtle() |
| 1156 | >>> pet.fd(50) |
| 1157 | >>> pet |
| 1158 | <turtle.Turtle object at 0x01417350> |
| 1159 | >>> turtles() |
| 1160 | [<turtle.Turtle object at 0x01417350>] |
| 1161 | |
| 1162 | |
| 1163 | .. method:: getscreen(): |
| 1164 | Return the TurtleScreen object, the turtle is drawing on. |
| 1165 | So TurtleScreen-methods can be called for that object. |
| 1166 | |
| 1167 | Example (for a Turtle instance named turtle): |
| 1168 | >>> ts = turtle.getscreen() |
| 1169 | >>> ts |
| 1170 | <turtle.Screen object at 0x01417710> |
| 1171 | >>> ts.bgcolor("pink") |
| 1172 | |
| 1173 | |
| 1174 | .. method:: def setundobuffer(size): |
| 1175 | size -- an integer or None |
| 1176 | |
| 1177 | Set or disable undobuffer. |
| 1178 | If size is an integer an empty undobuffer of given size is installed. |
| 1179 | Size gives the maximum number of turtle-actions that can be undone |
| 1180 | by the undo() method/function. |
| 1181 | If size is None, no undobuffer is present. |
| 1182 | |
| 1183 | Example (for a Turtle instance named turtle): |
| 1184 | >>> turtle.setundobuffer(42) |
| 1185 | |
| 1186 | |
| 1187 | .. method:: undobufferentries(): |
| 1188 | """Return count of entries in the undobuffer. |
| 1189 | |
| 1190 | Example (for a Turtle instance named turtle): |
| 1191 | >>> while undobufferentries(): |
| 1192 | ... undo() |
| 1193 | |
| 1194 | |
| 1195 | .. method:: tracer(flag=None, delay=None) |
| 1196 | A replica of the corresponding TurtleScreen-method |
| 1197 | *Deprecated since Python 2.6* (as RawTurtle method) |
| 1198 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1199 | |
Martin v. Löwis | 8718459 | 2008-06-04 06:29:55 +0000 | [diff] [blame^] | 1200 | .. method:: window_width() |
| 1201 | .. method:: window_height() |
| 1202 | Both are replicas of the corresponding TurtleScreen-methods |
| 1203 | *Deprecated since Python 2.6* (as RawTurtle methods) |
| 1204 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1205 | |
Martin v. Löwis | 8718459 | 2008-06-04 06:29:55 +0000 | [diff] [blame^] | 1206 | EXCURSUS ABOUT THE USE OF COMPOUND SHAPES |
| 1207 | ----------------------------------------- |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1208 | |
Martin v. Löwis | 8718459 | 2008-06-04 06:29:55 +0000 | [diff] [blame^] | 1209 | To use compound turtle shapes, which consist of several polygons |
| 1210 | of different color, you must use the helper class Shape |
| 1211 | explicitely as described below: |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1212 | |
Martin v. Löwis | 8718459 | 2008-06-04 06:29:55 +0000 | [diff] [blame^] | 1213 | 1. Create an empty Shape object of type compound |
| 1214 | 2. Add as many components to this object as desired, |
| 1215 | using the addcomponent() method: |
| 1216 | |
| 1217 | .. method:: addcomponent(self, poly, fill, outline=None) |
| 1218 | poly -- a polygon |
| 1219 | fill -- a color, the poly will be filled with |
| 1220 | outline -- a color for the poly's outline (if given) |
| 1221 | |
| 1222 | So it goes like this:: |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1223 | |
Martin v. Löwis | 8718459 | 2008-06-04 06:29:55 +0000 | [diff] [blame^] | 1224 | >>> s = Shape("compound") |
| 1225 | >>> poly1 = ((0,0),(10,-5),(0,10),(-10,-5)) |
| 1226 | >>> s.addcomponent(poly1, "red", "blue") |
| 1227 | >>> poly2 = ((0,0),(10,-5),(-10,-5)) |
| 1228 | >>> s.addcomponent(poly2, "blue", "red") |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1229 | |
Martin v. Löwis | 8718459 | 2008-06-04 06:29:55 +0000 | [diff] [blame^] | 1230 | Now add Shape s to the Screen's shapelist ... |
| 1231 | .. and use it:: |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1232 | |
Martin v. Löwis | 8718459 | 2008-06-04 06:29:55 +0000 | [diff] [blame^] | 1233 | >>> register_shape("myshape", s) |
| 1234 | >>> shape("myshape") |
| 1235 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1236 | |
Martin v. Löwis | 8718459 | 2008-06-04 06:29:55 +0000 | [diff] [blame^] | 1237 | NOTE 1: addcomponent() is a method of class Shape (not of |
| 1238 | Turtle nor Screen) and thus there is NO FUNCTION of the same name. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1239 | |
Martin v. Löwis | 8718459 | 2008-06-04 06:29:55 +0000 | [diff] [blame^] | 1240 | NOTE 2: class Shape is used internally by the register_shape method |
| 1241 | in different ways. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1242 | |
Martin v. Löwis | 8718459 | 2008-06-04 06:29:55 +0000 | [diff] [blame^] | 1243 | The application programmer has to deal with the Shape class |
| 1244 | ONLY when using compound shapes like shown above! |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1245 | |
Martin v. Löwis | 8718459 | 2008-06-04 06:29:55 +0000 | [diff] [blame^] | 1246 | NOTE 3: A short description of the class Shape is in section 4. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1247 | |
Martin v. Löwis | 8718459 | 2008-06-04 06:29:55 +0000 | [diff] [blame^] | 1248 | |
| 1249 | |
| 1250 | 3. METHODS OF TurtleScreen/Screen AND CORRESPONDING FUNCTIONS |
| 1251 | ============================================================= |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1252 | |
| 1253 | |
Martin v. Löwis | 8718459 | 2008-06-04 06:29:55 +0000 | [diff] [blame^] | 1254 | WINDOW CONTROL |
| 1255 | -------------- |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1256 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1257 | |
Martin v. Löwis | 8718459 | 2008-06-04 06:29:55 +0000 | [diff] [blame^] | 1258 | .. method:: bgcolor(*args) |
| 1259 | args -- a color string or three numbers in the range 0..colormode |
| 1260 | or a 3-tuple of such numbers. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1261 | |
Martin v. Löwis | 8718459 | 2008-06-04 06:29:55 +0000 | [diff] [blame^] | 1262 | Set or return backgroundcolor of the TurtleScreen. |
| 1263 | |
| 1264 | Example (for a TurtleScreen instance named screen): |
| 1265 | >>> screen.bgcolor("orange") |
| 1266 | >>> screen.bgcolor() |
| 1267 | 'orange' |
| 1268 | >>> screen.bgcolor(0.5,0,0.5) |
| 1269 | >>> screen.bgcolor() |
| 1270 | '#800080' |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1271 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1272 | |
Martin v. Löwis | 8718459 | 2008-06-04 06:29:55 +0000 | [diff] [blame^] | 1273 | .. method:: bgpic(picname=None) |
| 1274 | picname -- a string, name of a gif-file or "nopic". |
| 1275 | |
| 1276 | Set background image or return name of current backgroundimage. |
| 1277 | If picname is a filename, set the corresponing image as background. |
| 1278 | If picname is "nopic", delete backgroundimage, if present. |
| 1279 | If picname is None, return the filename of the current backgroundimage. |
| 1280 | |
| 1281 | Example (for a TurtleScreen instance named screen): |
| 1282 | >>> screen.bgpic() |
| 1283 | 'nopic' |
| 1284 | >>> screen.bgpic("landscape.gif") |
| 1285 | >>> screen.bgpic() |
| 1286 | 'landscape.gif' |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1287 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1288 | |
Martin v. Löwis | 8718459 | 2008-06-04 06:29:55 +0000 | [diff] [blame^] | 1289 | .. method:: clear() |
| 1290 | .. method:: clearscreen() |
| 1291 | Delete all drawings and all turtles from the TurtleScreen. |
| 1292 | Reset empty TurtleScreen to it's initial state: white background, |
| 1293 | no backgroundimage, no eventbindings and tracing on. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1294 | |
Martin v. Löwis | 8718459 | 2008-06-04 06:29:55 +0000 | [diff] [blame^] | 1295 | Example (for a TurtleScreen instance named screen): |
| 1296 | screen.clear() |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1297 | |
Martin v. Löwis | 8718459 | 2008-06-04 06:29:55 +0000 | [diff] [blame^] | 1298 | *Note*: this method is only available as the function named |
| 1299 | clearscreen(). (The function clear() is another one derived from |
| 1300 | the Turtle-method clear()!). |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1301 | |
| 1302 | |
Martin v. Löwis | 8718459 | 2008-06-04 06:29:55 +0000 | [diff] [blame^] | 1303 | .. method:: reset() |
| 1304 | .. method:: resetscreen() |
| 1305 | Reset all Turtles on the Screen to their initial state. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1306 | |
Martin v. Löwis | 8718459 | 2008-06-04 06:29:55 +0000 | [diff] [blame^] | 1307 | Example (for a TurtleScreen instance named screen): |
| 1308 | >>> screen.reset() |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1309 | |
Martin v. Löwis | 8718459 | 2008-06-04 06:29:55 +0000 | [diff] [blame^] | 1310 | *Note*: this method is pnly available as the function named |
| 1311 | resetscreen(). (The function reset() is another one derived from |
| 1312 | the Turtle-method reset()!). |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1313 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1314 | |
Martin v. Löwis | 8718459 | 2008-06-04 06:29:55 +0000 | [diff] [blame^] | 1315 | .. method:: screensize(canvwidth=None, canvheight=None, bg=None): |
| 1316 | canvwidth -- positive integer, new width of canvas in pixels |
| 1317 | canvheight -- positive integer, new height of canvas in pixels |
| 1318 | bg -- colorstring or color-tupel, new backgroundcolor |
| 1319 | |
| 1320 | If no arguments are given, return current (canvaswidth, canvasheight) |
| 1321 | Resize the canvas, the turtles are drawing on. |
| 1322 | Do not alter the drawing window. To observe hidden parts of |
| 1323 | the canvas use the scrollbars. (So one can make visible those |
| 1324 | parts of a drawing, which were outside the canvas before!) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1325 | |
Martin v. Löwis | 8718459 | 2008-06-04 06:29:55 +0000 | [diff] [blame^] | 1326 | Example (for a Turtle instance named turtle): |
| 1327 | >>> turtle.screensize(2000,1500) |
| 1328 | ### e. g. to search for an erroneously escaped turtle ;-) |
| 1329 | |
| 1330 | |
| 1331 | .. method:: setworldcoordinates(llx, lly, urx, ury): |
| 1332 | llx -- a number, x-coordinate of lower left corner of canvas |
| 1333 | lly -- a number, y-coordinate of lower left corner of canvas |
| 1334 | urx -- a number, x-coordinate of upper right corner of canvas |
| 1335 | ury -- a number, y-coordinate of upper right corner of canvas |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1336 | |
Martin v. Löwis | 8718459 | 2008-06-04 06:29:55 +0000 | [diff] [blame^] | 1337 | Set up user coodinate-system and switch to mode 'world' if necessary. |
| 1338 | This performs a screen.reset. If mode 'world' is already active, |
| 1339 | all drawings are redrawn according to the new coordinates. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1340 | |
Martin v. Löwis | 8718459 | 2008-06-04 06:29:55 +0000 | [diff] [blame^] | 1341 | But *ATTENTION*: in user-defined coordinatesystems angles may appear |
| 1342 | distorted. (see Screen.mode()) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1343 | |
Martin v. Löwis | 8718459 | 2008-06-04 06:29:55 +0000 | [diff] [blame^] | 1344 | Example (for a TurtleScreen instance named screen): |
| 1345 | >>> screen.reset() |
| 1346 | >>> screen.setworldcoordinates(-50,-7.5,50,7.5) |
| 1347 | >>> for _ in range(72): |
| 1348 | ... left(10) |
| 1349 | ... |
| 1350 | >>> for _ in range(8): |
| 1351 | ... left(45); fd(2) # a regular octogon |
| 1352 | |
| 1353 | |
| 1354 | ANIMATION CONTROL |
| 1355 | ----------------- |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1356 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1357 | |
Martin v. Löwis | 8718459 | 2008-06-04 06:29:55 +0000 | [diff] [blame^] | 1358 | .. method:: delay(delay=None): |
| 1359 | delay -- positive integer |
| 1360 | |
| 1361 | Set or return the drawing delay in milliseconds. (This is sort of |
| 1362 | time interval between two consecutived canvas updates.) The longer |
| 1363 | the drawing delay, the slower the animation. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1364 | |
Martin v. Löwis | 8718459 | 2008-06-04 06:29:55 +0000 | [diff] [blame^] | 1365 | Optional argument: |
| 1366 | Example (for a TurtleScreen instance named screen):: |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1367 | |
Martin v. Löwis | 8718459 | 2008-06-04 06:29:55 +0000 | [diff] [blame^] | 1368 | >>> screen.delay(15) |
| 1369 | >>> screen.delay() |
| 1370 | 15 |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1371 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1372 | |
Martin v. Löwis | 8718459 | 2008-06-04 06:29:55 +0000 | [diff] [blame^] | 1373 | .. method:: tracer(n=None, delay=None): |
| 1374 | n -- nonnegative integer |
| 1375 | delay -- nonnegative integer |
| 1376 | |
| 1377 | Turn turtle animation on/off and set delay for update drawings. |
| 1378 | If n is given, only each n-th regular screen update is really performed. |
| 1379 | (Can be used to accelerate the drawing of complex graphics.) |
| 1380 | Second argument sets delay value (see delay()) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1381 | |
Martin v. Löwis | 8718459 | 2008-06-04 06:29:55 +0000 | [diff] [blame^] | 1382 | Example (for a TurtleScreen instance named screen): |
| 1383 | >>> screen.tracer(8, 25) |
| 1384 | >>> dist = 2 |
| 1385 | >>> for i in range(200): |
| 1386 | ... fd(dist) |
| 1387 | ... rt(90) |
| 1388 | ... dist += 2 |
| 1389 | |
| 1390 | |
| 1391 | .. method:: update(): |
| 1392 | Perform a TurtleScreen update. To be used, when tracer is turned |
| 1393 | off. |
| 1394 | |
| 1395 | See also RawTurtle/Turtle - method speed() |
| 1396 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1397 | |
Martin v. Löwis | 8718459 | 2008-06-04 06:29:55 +0000 | [diff] [blame^] | 1398 | USING SCREEN EVENTS |
| 1399 | ------------------- |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1400 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1401 | |
Martin v. Löwis | 8718459 | 2008-06-04 06:29:55 +0000 | [diff] [blame^] | 1402 | .. method:: listen(xdummy=None, ydummy=None): |
| 1403 | """Set focus on TurtleScreen (in order to collect key-events) |
| 1404 | Dummy arguments are provided in order to be able to pass listen |
| 1405 | to the onclick method. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1406 | |
Martin v. Löwis | 8718459 | 2008-06-04 06:29:55 +0000 | [diff] [blame^] | 1407 | Example (for a TurtleScreen instance named screen): |
| 1408 | >>> screen.listen() |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1409 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1410 | |
Martin v. Löwis | 8718459 | 2008-06-04 06:29:55 +0000 | [diff] [blame^] | 1411 | .. method:: onkey(fun, key): |
| 1412 | fun -- a function with no arguments or None |
| 1413 | key -- a string: key (e.g. "a") or key-symbol (e.g. "space") |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1414 | |
Martin v. Löwis | 8718459 | 2008-06-04 06:29:55 +0000 | [diff] [blame^] | 1415 | Bind fun to key-release event of key. If fun is None, event-bindings |
| 1416 | are removed. |
| 1417 | Remark: in order to be able to register key-events, TurtleScreen |
| 1418 | must have focus. (See method listen.) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1419 | |
Martin v. Löwis | 8718459 | 2008-06-04 06:29:55 +0000 | [diff] [blame^] | 1420 | Example (for a TurtleScreen instance named screen |
| 1421 | and a Turtle instance named turtle):: |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1422 | |
Martin v. Löwis | 8718459 | 2008-06-04 06:29:55 +0000 | [diff] [blame^] | 1423 | >>> def f(): |
| 1424 | ... fd(50) |
| 1425 | ... lt(60) |
| 1426 | ... |
| 1427 | >>> screen.onkey(f, "Up") |
| 1428 | >>> screen.listen() |
| 1429 | |
| 1430 | |
| 1431 | .. method:: onclick(fun, btn=1, add=None): |
| 1432 | .. method:: onscreenclick(fun, btn=1, add=None): |
| 1433 | fun -- a function with two arguments, to which will be assigned |
| 1434 | the coordinates of the clicked point on the canvas - or None. |
| 1435 | num -- number of the mouse-button defaults to 1 (left mouse button). |
| 1436 | add -- True or False. If True, new binding will be added, otherwise |
| 1437 | it will replace a former binding. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1438 | |
Martin v. Löwis | 8718459 | 2008-06-04 06:29:55 +0000 | [diff] [blame^] | 1439 | Example (for a TurtleScreen instance named screen and a Turtle instance |
| 1440 | named turtle):: |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1441 | |
Martin v. Löwis | 8718459 | 2008-06-04 06:29:55 +0000 | [diff] [blame^] | 1442 | >>> screen.onclick(turtle.goto) |
| 1443 | ### Subsequently clicking into the TurtleScreen will |
| 1444 | ### make the turtle move to the clicked point. |
| 1445 | >>> screen.onclick(None) |
| 1446 | |
| 1447 | ### event-binding will be removed |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1448 | |
Martin v. Löwis | 8718459 | 2008-06-04 06:29:55 +0000 | [diff] [blame^] | 1449 | *Note*: this method is only available as the function named |
| 1450 | onscreenclick(). (The function onclick() is a different one derived |
| 1451 | from the Turtle-method onclick()!). |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1452 | |
| 1453 | |
Martin v. Löwis | 8718459 | 2008-06-04 06:29:55 +0000 | [diff] [blame^] | 1454 | .. method:: ontimer(fun, t=0): |
| 1455 | fun -- a function with no arguments. |
| 1456 | t -- a number >= 0 |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1457 | |
Martin v. Löwis | 8718459 | 2008-06-04 06:29:55 +0000 | [diff] [blame^] | 1458 | Install a timer, which calls fun after t milliseconds. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1459 | |
Martin v. Löwis | 8718459 | 2008-06-04 06:29:55 +0000 | [diff] [blame^] | 1460 | Example (for a TurtleScreen instance named screen): |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1461 | |
Martin v. Löwis | 8718459 | 2008-06-04 06:29:55 +0000 | [diff] [blame^] | 1462 | >>> running = True |
| 1463 | >>> def f(): |
| 1464 | if running: |
| 1465 | fd(50) |
| 1466 | lt(60) |
| 1467 | screen.ontimer(f, 250) |
| 1468 | >>> f() ### makes the turtle marching around |
| 1469 | >>> running = False |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1470 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1471 | |
Martin v. Löwis | 8718459 | 2008-06-04 06:29:55 +0000 | [diff] [blame^] | 1472 | SETTINGS AND SPECIAL METHODS |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1473 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1474 | |
Martin v. Löwis | 8718459 | 2008-06-04 06:29:55 +0000 | [diff] [blame^] | 1475 | .. method:: mode(mode=None): |
| 1476 | mode -- on of the strings 'standard', 'logo' or 'world' |
| 1477 | |
| 1478 | Set turtle-mode ('standard', 'logo' or 'world') and perform reset. |
| 1479 | If mode is not given, current mode is returned. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1480 | |
Martin v. Löwis | 8718459 | 2008-06-04 06:29:55 +0000 | [diff] [blame^] | 1481 | Mode 'standard' is compatible with old turtle.py. |
| 1482 | Mode 'logo' is compatible with most Logo-Turtle-Graphics. |
| 1483 | Mode 'world' uses userdefined 'worldcoordinates'. *Attention*: in |
| 1484 | this mode angles appear distorted if x/y unit-ratio doesn't equal 1. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1485 | |
Martin v. Löwis | 8718459 | 2008-06-04 06:29:55 +0000 | [diff] [blame^] | 1486 | ============ ========================= =================== |
| 1487 | Mode Initial turtle heading positive angles |
| 1488 | ============ ========================= =================== |
| 1489 | 'standard' to the right (east) counterclockwise |
| 1490 | 'logo' upward (north) clockwise |
| 1491 | ============ ========================= =================== |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1492 | |
Martin v. Löwis | 8718459 | 2008-06-04 06:29:55 +0000 | [diff] [blame^] | 1493 | Examples:: |
| 1494 | >>> mode('logo') # resets turtle heading to north |
| 1495 | >>> mode() |
| 1496 | 'logo' |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1497 | |
| 1498 | |
Martin v. Löwis | 8718459 | 2008-06-04 06:29:55 +0000 | [diff] [blame^] | 1499 | .. method:: colormode(cmode=None): |
| 1500 | cmode -- one of the values 1.0 or 255 |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1501 | |
Martin v. Löwis | 8718459 | 2008-06-04 06:29:55 +0000 | [diff] [blame^] | 1502 | """Return the colormode or set it to 1.0 or 255. |
| 1503 | Subsequently r, g, b values of colortriples have to be in |
| 1504 | range 0..cmode. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1505 | |
Martin v. Löwis | 8718459 | 2008-06-04 06:29:55 +0000 | [diff] [blame^] | 1506 | Example (for a TurtleScreen instance named screen): |
| 1507 | >>> screen.colormode() |
| 1508 | 1.0 |
| 1509 | >>> screen.colormode(255) |
| 1510 | >>> turtle.pencolor(240,160,80) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1511 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1512 | |
Martin v. Löwis | 8718459 | 2008-06-04 06:29:55 +0000 | [diff] [blame^] | 1513 | .. method:: getcanvas(): |
| 1514 | Return the Canvas of this TurtleScreen. Useful for insiders, who |
| 1515 | know what to do with a Tkinter-Canvas ;-) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1516 | |
Martin v. Löwis | 8718459 | 2008-06-04 06:29:55 +0000 | [diff] [blame^] | 1517 | Example (for a Screen instance named screen): |
| 1518 | >>> cv = screen.getcanvas() |
| 1519 | >>> cv |
| 1520 | <turtle.ScrolledCanvas instance at 0x010742D8> |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1521 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1522 | |
Martin v. Löwis | 8718459 | 2008-06-04 06:29:55 +0000 | [diff] [blame^] | 1523 | .. method:: getshapes(): |
| 1524 | """Return a list of names of all currently available turtle shapes. |
| 1525 | |
| 1526 | Example (for a TurtleScreen instance named screen): |
| 1527 | >>> screen.getshapes() |
| 1528 | ['arrow', 'blank', 'circle', ... , 'turtle'] |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1529 | |
| 1530 | |
Martin v. Löwis | 8718459 | 2008-06-04 06:29:55 +0000 | [diff] [blame^] | 1531 | .. method:: register_shape(name, shape=None) |
| 1532 | .. method:: addshape(name, shape=None) |
| 1533 | Arguments: |
| 1534 | (1) name is the name of a gif-file and shape is None. |
| 1535 | Installs the corresponding image shape. |
| 1536 | !! Image-shapes DO NOT rotate when turning the turtle, |
| 1537 | !! so they do not display the heading of the turtle! |
| 1538 | (2) name is an arbitrary string and shape is a tuple |
| 1539 | of pairs of coordinates. Installs the corresponding |
| 1540 | polygon shape |
| 1541 | (3) name is an arbitrary string and shape is a |
| 1542 | (compound) Shape object. Installs the corresponding |
| 1543 | compound shape. (See class Shape.) |
| 1544 | |
| 1545 | Adds a turtle shape to TurtleScreen's shapelist. Only thusly |
| 1546 | registered shapes can be used by issueing the command shape(shapename). |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1547 | |
Martin v. Löwis | 8718459 | 2008-06-04 06:29:55 +0000 | [diff] [blame^] | 1548 | call: register_shape("turtle.gif") |
| 1549 | --or: register_shape("tri", ((0,0), (10,10), (-10,10))) |
| 1550 | |
| 1551 | Example (for a TurtleScreen instance named screen): |
| 1552 | >>> screen.register_shape("triangle", ((5,-3),(0,5),(-5,-3))) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1553 | |
| 1554 | |
Martin v. Löwis | 8718459 | 2008-06-04 06:29:55 +0000 | [diff] [blame^] | 1555 | .. method:: turtles(): |
| 1556 | Return the list of turtles on the screen. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1557 | |
Martin v. Löwis | 8718459 | 2008-06-04 06:29:55 +0000 | [diff] [blame^] | 1558 | Example (for a TurtleScreen instance named screen): |
| 1559 | >>> for turtle in screen.turtles() |
| 1560 | ... turtle.color("red") |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1561 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1562 | |
Martin v. Löwis | 8718459 | 2008-06-04 06:29:55 +0000 | [diff] [blame^] | 1563 | .. method:: window_height(): |
| 1564 | Return the height of the turtle window. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1565 | |
Martin v. Löwis | 8718459 | 2008-06-04 06:29:55 +0000 | [diff] [blame^] | 1566 | Example (for a TurtleScreen instance named screen): |
| 1567 | >>> screen.window_height() |
| 1568 | 480 |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1569 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1570 | |
Martin v. Löwis | 8718459 | 2008-06-04 06:29:55 +0000 | [diff] [blame^] | 1571 | .. method:: window_width(): |
| 1572 | Return the width of the turtle window. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1573 | |
Martin v. Löwis | 8718459 | 2008-06-04 06:29:55 +0000 | [diff] [blame^] | 1574 | Example (for a TurtleScreen instance named screen): |
| 1575 | >>> screen.window_width() |
| 1576 | 640 |
| 1577 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1578 | |
Martin v. Löwis | 8718459 | 2008-06-04 06:29:55 +0000 | [diff] [blame^] | 1579 | METHODS SPECIFIC TO Screen, not inherited from TurtleScreen |
| 1580 | ----------------------------------------------------------- |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1581 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1582 | |
Martin v. Löwis | 8718459 | 2008-06-04 06:29:55 +0000 | [diff] [blame^] | 1583 | .. method:: bye(): |
| 1584 | """Shut the turtlegraphics window. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1585 | |
Martin v. Löwis | 8718459 | 2008-06-04 06:29:55 +0000 | [diff] [blame^] | 1586 | This is a method of the Screen-class and not available for |
| 1587 | TurtleScreen instances. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1588 | |
Martin v. Löwis | 8718459 | 2008-06-04 06:29:55 +0000 | [diff] [blame^] | 1589 | Example (for a TurtleScreen instance named screen): |
| 1590 | >>> screen.bye() |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1591 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1592 | |
Martin v. Löwis | 8718459 | 2008-06-04 06:29:55 +0000 | [diff] [blame^] | 1593 | .. method:: exitonclick(): |
| 1594 | Bind bye() method to mouseclick on TurtleScreen. |
| 1595 | If "using_IDLE" - value in configuration dictionary is False |
| 1596 | (default value), enter mainloop. |
| 1597 | Remark: If IDLE with -n switch (no subprocess) is used, this value |
| 1598 | should be set to True in turtle.cfg. In this case IDLE's own mainloop |
| 1599 | is active also for the client script. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1600 | |
Martin v. Löwis | 8718459 | 2008-06-04 06:29:55 +0000 | [diff] [blame^] | 1601 | This is a method of the Screen-class and not available for |
| 1602 | TurtleScreen instances. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1603 | |
Martin v. Löwis | 8718459 | 2008-06-04 06:29:55 +0000 | [diff] [blame^] | 1604 | Example (for a Screen instance named screen): |
| 1605 | >>> screen.exitonclick() |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1606 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1607 | |
Martin v. Löwis | 8718459 | 2008-06-04 06:29:55 +0000 | [diff] [blame^] | 1608 | .. method:: setup(width=_CFG["width"], height=_CFG["height"], |
| 1609 | startx=_CFG["leftright"], starty=_CFG["topbottom"]): |
| 1610 | Set the size and position of the main window. |
| 1611 | Default values of arguments are stored in the configuration dicionary |
| 1612 | and can be changed via a turtle.cfg file. |
| 1613 | |
| 1614 | width -- as integer a size in pixels, as float a fraction of the screen. |
| 1615 | Default is 50% of screen. |
| 1616 | height -- as integer the height in pixels, as float a fraction of the |
| 1617 | screen. Default is 75% of screen. |
| 1618 | startx -- if positive, starting position in pixels from the left |
| 1619 | edge of the screen, if negative from the right edge |
| 1620 | Default, startx=None is to center window horizontally. |
| 1621 | starty -- if positive, starting position in pixels from the top |
| 1622 | edge of the screen, if negative from the bottom edge |
| 1623 | Default, starty=None is to center window vertically. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1624 | |
Martin v. Löwis | 8718459 | 2008-06-04 06:29:55 +0000 | [diff] [blame^] | 1625 | Examples (for a Screen instance named screen):: |
| 1626 | >>> screen.setup (width=200, height=200, startx=0, starty=0) |
| 1627 | # sets window to 200x200 pixels, in upper left of screen |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1628 | |
Martin v. Löwis | 8718459 | 2008-06-04 06:29:55 +0000 | [diff] [blame^] | 1629 | >>> screen.setup(width=.75, height=0.5, startx=None, starty=None) |
| 1630 | # sets window to 75% of screen by 50% of screen and centers |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1631 | |
Martin v. Löwis | 8718459 | 2008-06-04 06:29:55 +0000 | [diff] [blame^] | 1632 | |
| 1633 | .. method:: title(titlestring): |
| 1634 | titlestring -- a string, to appear in the titlebar of the |
| 1635 | turtle graphics window. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1636 | |
Martin v. Löwis | 8718459 | 2008-06-04 06:29:55 +0000 | [diff] [blame^] | 1637 | Set title of turtle-window to titlestring |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1638 | |
Martin v. Löwis | 8718459 | 2008-06-04 06:29:55 +0000 | [diff] [blame^] | 1639 | This is a method of the Screen-class and not available for |
| 1640 | TurtleScreen instances. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1641 | |
Martin v. Löwis | 8718459 | 2008-06-04 06:29:55 +0000 | [diff] [blame^] | 1642 | Example (for a Screen instance named screen): |
| 1643 | >>> screen.title("Welcome to the turtle-zoo!") |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1644 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1645 | |
| 1646 | |
Martin v. Löwis | 8718459 | 2008-06-04 06:29:55 +0000 | [diff] [blame^] | 1647 | 4. THE PUBLIC CLASSES of the module turtle.py |
| 1648 | ============================================= |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1649 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1650 | |
Martin v. Löwis | 8718459 | 2008-06-04 06:29:55 +0000 | [diff] [blame^] | 1651 | class RawTurtle(canvas): |
| 1652 | canvas -- a Tkinter-Canvas, a ScrolledCanvas or a TurtleScreen |
| 1653 | |
| 1654 | Alias: RawPen |
| 1655 | |
| 1656 | Define a turtle. |
| 1657 | A description of the methods follows below. All methods are also |
| 1658 | available as functions (to control some anonymous turtle) thus |
| 1659 | providing a procedural interface to turtlegraphics |
| 1660 | |
| 1661 | class Turtle() |
| 1662 | Subclass of RawTurtle, has the same interface with the additional |
| 1663 | property, that Turtle instances draw on a default Screen object, |
| 1664 | which is created automatically, when needed for the first time. |
| 1665 | |
| 1666 | class TurtleScreen(cv) |
| 1667 | cv -- a Tkinter-Canvas |
| 1668 | Provides screen oriented methods like setbg etc. |
| 1669 | A description of the methods follows below. |
| 1670 | |
| 1671 | class Screen() |
| 1672 | Subclass of TurtleScreen, with four methods added. |
| 1673 | All methods are also available as functions to conrtol a unique |
| 1674 | Screen instance thus belonging to the procedural interface |
| 1675 | to turtlegraphics. This Screen instance is automatically created |
| 1676 | when needed for the first time. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1677 | |
Martin v. Löwis | 8718459 | 2008-06-04 06:29:55 +0000 | [diff] [blame^] | 1678 | class ScrolledCavas(master) |
| 1679 | master -- some Tkinter widget to contain the ScrolledCanvas, i.e. |
| 1680 | a Tkinter-canvas with scrollbars added. |
| 1681 | Used by class Screen, which thus provides automatically a |
| 1682 | ScrolledCanvas as playground for the turtles. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1683 | |
Martin v. Löwis | 8718459 | 2008-06-04 06:29:55 +0000 | [diff] [blame^] | 1684 | class Shape(type\_, data) |
| 1685 | type --- one of the strings "polygon", "image", "compound" |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1686 | |
Martin v. Löwis | 8718459 | 2008-06-04 06:29:55 +0000 | [diff] [blame^] | 1687 | Data structure modeling shapes. |
| 1688 | The pair type\_, data must be as follows: |
| 1689 | |
| 1690 | type\_ data |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1691 | |
Martin v. Löwis | 8718459 | 2008-06-04 06:29:55 +0000 | [diff] [blame^] | 1692 | "polygon" a polygon-tuple, i. e. |
| 1693 | a tuple of pairs of coordinates |
| 1694 | |
| 1695 | "image" an image (in this form only used internally!) |
| 1696 | |
| 1697 | "compound" None |
| 1698 | A compund shape has to be constructed using |
| 1699 | the addcomponent method |
| 1700 | |
| 1701 | addcomponent(self, poly, fill, outline=None) |
| 1702 | poly -- polygon, i. e. a tuple of pairs of numbers. |
| 1703 | fill -- the fillcolor of the component, |
| 1704 | outline -- the outline color of the component. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1705 | |
Martin v. Löwis | 8718459 | 2008-06-04 06:29:55 +0000 | [diff] [blame^] | 1706 | Example: |
| 1707 | >>> poly = ((0,0),(10,-5),(0,10),(-10,-5)) |
| 1708 | >>> s = Shape("compound") |
| 1709 | >>> s.addcomponent(poly, "red", "blue") |
| 1710 | ### .. add more components and then use register_shape() |
| 1711 | |
| 1712 | See EXCURSUS ABOUT THE USE OF COMPOUND SHAPES |
| 1713 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1714 | |
Martin v. Löwis | 8718459 | 2008-06-04 06:29:55 +0000 | [diff] [blame^] | 1715 | class Vec2D(x, y): |
| 1716 | A two-dimensional vector class, used as a helper class |
| 1717 | for implementing turtle graphics. |
| 1718 | May be useful for turtle graphics programs also. |
| 1719 | Derived from tuple, so a vector is a tuple! |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1720 | |
Martin v. Löwis | 8718459 | 2008-06-04 06:29:55 +0000 | [diff] [blame^] | 1721 | Provides (for a, b vectors, k number): |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1722 | |
Martin v. Löwis | 8718459 | 2008-06-04 06:29:55 +0000 | [diff] [blame^] | 1723 | * a+b vector addition |
| 1724 | * a-b vector subtraction |
| 1725 | * a*b inner product |
| 1726 | * k*a and a*k multiplication with scalar |
| 1727 | * \|a\| absolute value of a |
| 1728 | * a.rotate(angle) rotation |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1729 | |
| 1730 | |
Martin v. Löwis | 8718459 | 2008-06-04 06:29:55 +0000 | [diff] [blame^] | 1731 | |
| 1732 | V. HELP AND CONFIGURATION |
| 1733 | ========================= |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1734 | |
Martin v. Löwis | 8718459 | 2008-06-04 06:29:55 +0000 | [diff] [blame^] | 1735 | This section contains subsections on: |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1736 | |
Martin v. Löwis | 8718459 | 2008-06-04 06:29:55 +0000 | [diff] [blame^] | 1737 | - how to use help |
| 1738 | - how to prepare and use translations of the online-help |
| 1739 | into other languages |
| 1740 | - how to configure the appearance of the graphics window and |
| 1741 | the turtles at startup |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1742 | |
| 1743 | |
Martin v. Löwis | 8718459 | 2008-06-04 06:29:55 +0000 | [diff] [blame^] | 1744 | HOW TO USE HELP: |
| 1745 | ---------------- |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1746 | |
Martin v. Löwis | 8718459 | 2008-06-04 06:29:55 +0000 | [diff] [blame^] | 1747 | The public methods of the Screen and Turtle classes are documented |
| 1748 | extensively via docstrings. So these can be used as online-help |
| 1749 | via the Python help facilities: |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1750 | |
Martin v. Löwis | 8718459 | 2008-06-04 06:29:55 +0000 | [diff] [blame^] | 1751 | - When using IDLE, tooltips show the signatures and first lines of |
| 1752 | the docstrings of typed in function-/method calls. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1753 | |
Martin v. Löwis | 8718459 | 2008-06-04 06:29:55 +0000 | [diff] [blame^] | 1754 | - calling help on methods or functions display the docstrings. |
| 1755 | Examples:: |
| 1756 | |
| 1757 | >>> help(Screen.bgcolor) |
| 1758 | Help on method bgcolor in module turtle: |
| 1759 | |
| 1760 | bgcolor(self, *args) unbound turtle.Screen method |
| 1761 | Set or return backgroundcolor of the TurtleScreen. |
| 1762 | |
| 1763 | Arguments (if given): a color string or three numbers |
| 1764 | in the range 0..colormode or a 3-tuple of such numbers. |
| 1765 | |
| 1766 | Example (for a TurtleScreen instance named screen):: |
| 1767 | |
| 1768 | >>> screen.bgcolor("orange") |
| 1769 | >>> screen.bgcolor() |
| 1770 | 'orange' |
| 1771 | >>> screen.bgcolor(0.5,0,0.5) |
| 1772 | >>> screen.bgcolor() |
| 1773 | '#800080' |
| 1774 | |
| 1775 | >>> help(Turtle.penup) |
| 1776 | Help on method penup in module turtle: |
| 1777 | |
| 1778 | penup(self) unbound turtle.Turtle method |
| 1779 | Pull the pen up -- no drawing when moving. |
| 1780 | |
| 1781 | Aliases: penup | pu | up |
| 1782 | |
| 1783 | No argument |
| 1784 | |
| 1785 | Example (for a Turtle instance named turtle): |
| 1786 | >>> turtle.penup() |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1787 | |
Martin v. Löwis | 8718459 | 2008-06-04 06:29:55 +0000 | [diff] [blame^] | 1788 | The docstrings of the functions which are derived from methods have |
| 1789 | a modified form:: |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1790 | |
Martin v. Löwis | 8718459 | 2008-06-04 06:29:55 +0000 | [diff] [blame^] | 1791 | >>> help(bgcolor) |
| 1792 | Help on function bgcolor in module turtle: |
| 1793 | |
| 1794 | bgcolor(*args) |
| 1795 | Set or return backgroundcolor of the TurtleScreen. |
| 1796 | |
| 1797 | Arguments (if given): a color string or three numbers |
| 1798 | in the range 0..colormode or a 3-tuple of such numbers. |
| 1799 | |
| 1800 | Example:: |
| 1801 | |
| 1802 | >>> bgcolor("orange") |
| 1803 | >>> bgcolor() |
| 1804 | 'orange' |
| 1805 | >>> bgcolor(0.5,0,0.5) |
| 1806 | >>> bgcolor() |
| 1807 | '#800080' |
| 1808 | |
| 1809 | >>> help(penup) |
| 1810 | Help on function penup in module turtle: |
| 1811 | |
| 1812 | penup() |
| 1813 | Pull the pen up -- no drawing when moving. |
| 1814 | |
| 1815 | Aliases: penup | pu | up |
| 1816 | |
| 1817 | No argument |
| 1818 | |
| 1819 | Example: |
| 1820 | >>> penup() |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1821 | |
Martin v. Löwis | 8718459 | 2008-06-04 06:29:55 +0000 | [diff] [blame^] | 1822 | These modified docstrings are created automatically together with the |
| 1823 | function definitions that are derived from the methods at import time. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1824 | |
| 1825 | |
Martin v. Löwis | 8718459 | 2008-06-04 06:29:55 +0000 | [diff] [blame^] | 1826 | TRANSLATION OF DOCSTRINGS INTO DIFFERENT LANGUAGES |
| 1827 | -------------------------------------------------- |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1828 | |
Martin v. Löwis | 8718459 | 2008-06-04 06:29:55 +0000 | [diff] [blame^] | 1829 | There is a utility to create a dictionary the keys of which are the |
| 1830 | method names and the values of which are the docstrings of the public |
| 1831 | methods of the classes Screen and Turtle. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1832 | |
Martin v. Löwis | 8718459 | 2008-06-04 06:29:55 +0000 | [diff] [blame^] | 1833 | write_docstringdict(filename="turtle_docstringdict"): |
| 1834 | filename -- a string, used as filename |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1835 | |
Martin v. Löwis | 8718459 | 2008-06-04 06:29:55 +0000 | [diff] [blame^] | 1836 | Create and write docstring-dictionary to a Python script |
| 1837 | with the given filename. |
| 1838 | This function has to be called explicitely, (it is not used by the |
| 1839 | turtle-graphics classes). The docstring dictionary will be written |
| 1840 | to the Python script <filname>.py It is intended to serve as a |
| 1841 | template for translation of the docstrings into different languages. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1842 | |
Martin v. Löwis | 8718459 | 2008-06-04 06:29:55 +0000 | [diff] [blame^] | 1843 | If you (or your students) want to use turtle.py with online help in |
| 1844 | your native language. You have to translate the docstrings and save |
| 1845 | the resulting file as e.g. turtle_docstringdict_german.py |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1846 | |
Martin v. Löwis | 8718459 | 2008-06-04 06:29:55 +0000 | [diff] [blame^] | 1847 | If you have an appropriate entry in your turtle.cfg file this dictionary |
| 1848 | will be read in at import time and will replace the original English |
| 1849 | docstrings. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1850 | |
Martin v. Löwis | 8718459 | 2008-06-04 06:29:55 +0000 | [diff] [blame^] | 1851 | At the time of this writing there exist docstring_dicts in German |
| 1852 | and in Italian. (Requests please to glingl@aon.at) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1853 | |
Martin v. Löwis | 8718459 | 2008-06-04 06:29:55 +0000 | [diff] [blame^] | 1854 | |
| 1855 | |
| 1856 | HOW TO CONFIGURE SCREEN AND TURTLES |
| 1857 | ----------------------------------- |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1858 | |
Martin v. Löwis | 8718459 | 2008-06-04 06:29:55 +0000 | [diff] [blame^] | 1859 | The built-in default configuration mimics the appearance and |
| 1860 | behaviour of the old turtle module in order to retain best possible |
| 1861 | compatibility with it. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1862 | |
Martin v. Löwis | 8718459 | 2008-06-04 06:29:55 +0000 | [diff] [blame^] | 1863 | If you want to use a different configuration which reflects |
| 1864 | better the features of this module or which fits better to |
| 1865 | your needs, e. g. for use in a classroom, you can prepare |
| 1866 | a configuration file turtle.cfg which will be read at import |
| 1867 | time and modify the configuration according to it's settings. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1868 | |
Martin v. Löwis | 8718459 | 2008-06-04 06:29:55 +0000 | [diff] [blame^] | 1869 | The built in configuration would correspond to the following |
| 1870 | turtle.cfg: |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1871 | |
Martin v. Löwis | 8718459 | 2008-06-04 06:29:55 +0000 | [diff] [blame^] | 1872 | width = 0.5 |
| 1873 | height = 0.75 |
| 1874 | leftright = None |
| 1875 | topbottom = None |
| 1876 | canvwidth = 400 |
| 1877 | canvheight = 300 |
| 1878 | mode = standard |
| 1879 | colormode = 1.0 |
| 1880 | delay = 10 |
| 1881 | undobuffersize = 1000 |
| 1882 | shape = classic |
| 1883 | pencolor = black |
| 1884 | fillcolor = black |
| 1885 | resizemode = noresize |
| 1886 | visible = True |
| 1887 | language = english |
| 1888 | exampleturtle = turtle |
| 1889 | examplescreen = screen |
| 1890 | title = Python Turtle Graphics |
| 1891 | using_IDLE = False |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1892 | |
Martin v. Löwis | 8718459 | 2008-06-04 06:29:55 +0000 | [diff] [blame^] | 1893 | Short explanation of selected entries: |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1894 | |
Martin v. Löwis | 8718459 | 2008-06-04 06:29:55 +0000 | [diff] [blame^] | 1895 | - The first four lines correspond to the arguments of the |
| 1896 | Screen.setup method |
| 1897 | - Line 5 and 6 correspond to the arguments of the Method |
| 1898 | Screen.screensize |
| 1899 | - shape can be any of the built-in shapes, e.g: arrow, turtle, |
| 1900 | etc. For more info try help(shape) |
| 1901 | - if you want to use no fillcolor (i. e. turtle transparent), |
| 1902 | you have to write: |
| 1903 | fillcolor = "" |
| 1904 | (All not empty strings must not have quotes in the cfg-file!) |
| 1905 | - if you want to reflect the turtle its state, you have to use |
| 1906 | resizemode = auto |
| 1907 | - if you set, e. g.: language = italian |
| 1908 | the docstringdict turtle_docstringdict_italian.py will be |
| 1909 | loaded at import time (if present on the import path, e.g. in |
| 1910 | the same directory as turtle.py |
| 1911 | - the entries exampleturtle and examplescreen define the names |
| 1912 | of these objects as they occur in the docstrings. The |
| 1913 | transformation of method-docstrings to function-docstrings |
| 1914 | will delete these names from the docstrings. (See examples in |
| 1915 | section on HELP) |
| 1916 | - using_IDLE Set this to True if you regularly work with IDLE |
| 1917 | and it's -n - switch. ("No subprocess") This will prevent |
| 1918 | exitonclick to enter the mainloop. |
| 1919 | |
| 1920 | There can be a turtle.cfg file in the directory where turtle.py |
| 1921 | is stored and an additional one in the currentworkingdirectory. |
| 1922 | The latter will override the settings of the first one. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1923 | |
Martin v. Löwis | 8718459 | 2008-06-04 06:29:55 +0000 | [diff] [blame^] | 1924 | The turtledemo directory contains a turtle.cfg file. If you |
| 1925 | study it as an example and see its effects when running the |
| 1926 | demos (preferably not from within the demo-viewer). |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1927 | |
Martin v. Löwis | 8718459 | 2008-06-04 06:29:55 +0000 | [diff] [blame^] | 1928 | |
| 1929 | VI. Demo scripts |
| 1930 | ================ |
| 1931 | |
| 1932 | There is a set of demo scripts in the turtledemo directory |
| 1933 | located here ... |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1934 | |
Martin v. Löwis | 8718459 | 2008-06-04 06:29:55 +0000 | [diff] [blame^] | 1935 | ##### please complete info about path ######################## |
| 1936 | |
| 1937 | It contains: |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1938 | |
Martin v. Löwis | 8718459 | 2008-06-04 06:29:55 +0000 | [diff] [blame^] | 1939 | - a set of 15 demo scripts demonstrating differet features |
| 1940 | of the new module turtle.py |
| 1941 | - a Demo-Viewer turtleDemo.py which can be used to view |
| 1942 | the sourcecode of the scripts and run them at the same time |
| 1943 | 14 of the examples can be accessed via the Examples Menu. |
| 1944 | All of them can also be run standalone. |
| 1945 | - The example turtledemo_two_canvases.py demonstrates the |
| 1946 | simultaneous use of two canvases with the turtle module. |
| 1947 | Therefor it only can be run standalone. |
| 1948 | - There is a turtle.cfg file in this directory, which also |
| 1949 | serves as an example for how to write and use such files. |
| 1950 | |
| 1951 | The demoscripts are: |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1952 | |
Martin v. Löwis | 8718459 | 2008-06-04 06:29:55 +0000 | [diff] [blame^] | 1953 | +----------------+------------------------------+-----------------------+ |
| 1954 | |Name | description | features | |
| 1955 | +----------------+------------------------------+-----------------------+ |
| 1956 | |bytedesign | complex classical | tracer, delay | |
| 1957 | | | turtlegraphics pattern | update | |
| 1958 | +----------------+------------------------------+-----------------------+ |
| 1959 | |chaos | graphs verhust dynamics, | worldcoordinates | |
| 1960 | | | proofs that you must not | | |
| 1961 | | | trust computers computations| | |
| 1962 | +----------------+------------------------------+-----------------------+ |
| 1963 | |clock | analog clock showing time | turtles as clock's | |
| 1964 | | | of your computer | hands, ontimer | |
| 1965 | +----------------+------------------------------+-----------------------+ |
| 1966 | |colormixer | experiment with r, g, b | ondrag | |
| 1967 | +----------------+------------------------------+-----------------------+ |
| 1968 | |fractalcurves | Hilbert & Koch | recursion | |
| 1969 | +----------------+------------------------------+-----------------------+ |
| 1970 | |lindenmayer | ethnomathematics | L-System | |
| 1971 | | | (indian kolams) | | |
| 1972 | +----------------+------------------------------+-----------------------+ |
| 1973 | |minimal_hanoi | Towers of Hanoi | Rectangular Turtles | |
| 1974 | | | | as Hanoi-Discs | |
| 1975 | | | | (shape, shapesize) | |
| 1976 | +----------------+------------------------------+-----------------------+ |
| 1977 | |paint | super minimalistic | onclick | |
| 1978 | | | drawing program | | |
| 1979 | +----------------+------------------------------+-----------------------+ |
| 1980 | |peace | elementary | turtle: appearance | |
| 1981 | | | | and animation | |
| 1982 | +----------------+------------------------------+-----------------------+ |
| 1983 | |penrose | aperiodic tiling with | stamp | |
| 1984 | | | kites and darts | | |
| 1985 | +----------------+------------------------------+-----------------------+ |
| 1986 | |planet_and_moon | simulation of | compound shape | |
| 1987 | | | gravitational system | Vec2D | |
| 1988 | +----------------+------------------------------+-----------------------+ |
| 1989 | |tree | a (graphical) breadth | clone | |
| 1990 | | | first tree (using generators)| | |
| 1991 | +----------------+------------------------------+-----------------------+ |
| 1992 | |wikipedia | a pattern from the wikipedia | clone, undo | |
| 1993 | | | article on turtle-graphics | | |
| 1994 | +----------------+------------------------------+-----------------------+ |
| 1995 | |yingyang | another elementary example | circle | |
| 1996 | +----------------+------------------------------+-----------------------+ |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1997 | |
Martin v. Löwis | 8718459 | 2008-06-04 06:29:55 +0000 | [diff] [blame^] | 1998 | turtledemo_two-canvases: two distinct Tkinter-Canvases |
| 1999 | are populated with turtles. Uses class RawTurtle. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 2000 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 2001 | |
Martin v. Löwis | 8718459 | 2008-06-04 06:29:55 +0000 | [diff] [blame^] | 2002 | Have fun! |