[Tutor] Questions about the new turtle module in Python 2.6b2

Gregor Lingl gregor.lingl at aon.at
Fri Aug 15 22:28:06 CEST 2008


Hi Dick,
as I promised some days ago, here is an event driven version of
a rectangle generator, which is based on my first example:

from turtle import *
from random import random, randint
from time import sleep

MAXLEN = 30
MAXWID = 25

def randomcolor():
    return random(), random(), random()

def pause(x,y):
    global running
    running = not running
    if running:
        title("RUNNING... - CLICK TO HALT")
    else:
        title("HALTED... - CLICK TO CONTINUE")
       
def squares(x,y):
    clear()
    title("RUNNING... - CLICK TO HALT")
    onscreenclick(pause)
    for cycle in range(randint(3, 5)):
        bgcolor(randomcolor())
        for rect in range(randint(5,10)):
            shapesize(3 + random()*MAXLEN, 3 + random()*MAXWID,
                       randint(3, 10))
            color(randomcolor(), randomcolor())
            stamp()
            update()
            sleep(1)
            update()
            while not running:   # now pausing
                sleep(0.5)
                update()
        sleep(1)
        clearstamps()
    bgcolor("white")
    pencolor("black")
    write("Click to exit!", align="center", font=("Arial", 24, "bold"))
    title("")
    exitonclick()
   
   
reset()
title("Python turtle graphics: random rectangle generator")
hideturtle()
resizemode("user")
shape("square")
running = True
onscreenclick(squares)
listen()
write("Click me!", align="center", font=("Arial", 24, "bold"))
mainloop()

######## end of program

A short explanation:
In event driven programs you have to ecapsulate all actions in
functions (or methods when using oop-techniques) which have to
be bound to some events. In this example this happens via the
onscreenclick() function.

The functions that perform the desired actions are not called
in the  main program. Instead the main program calls the
mainloop() which is a loop waiting for events. If such an
event - like a mouse click - occurs the function bound to
this event will be called. This function may rebind the event
to another action - as is done in squares - line three.
So in this example the first click starts the rectangle
generator, subsequent clicks will swith the running-state.
(The last click wil terminate the program.)

Making a program pause needs a special trick:
the pause() function + boolean variable running.
update() is used  to make the program check the
event-queue.  (This would not be necessary, I think,
if you draw rectangles slowly using goto(), because in
this case the animation itself automatically uses update()
frequently.)

Hope this helps - if not feel free to aks again

Gregor



Gregor Lingl schrieb:
> Dick Moores schrieb:
>> Gregor,
>>
>> <http://docs.python.org/dev/library/turtle.html#turtle.setup>
>> 1. I want the window to open with the right edge 0 pixels from the 
>> right edge of my screen.
>> However, setup(width=.75, height=.915, startx=-0, starty=0) doesn't 
>> work. I have to do the nearest thing,
>> setup(width=.75, height=.915, startx=-1, starty=0). A small thing, 
>> but what's wrong with perfection.
>>
> Hi Dick,
> a good observation, but I think this cannot be changed, because  of
>
> >>> -0 == 0
> True
>
> in Python
>> 2. There's a lot I don't understand in
>> turtle.setup(width=_CFG[, "width"], height=_CFG[, "height"], 
>> startx=_CFG[, "leftright"], starty=_CFG[, "topbottom"])
>>
>> What is '_CFG'? And how should anyone know what it is?
>> What is "leftright"? And how to use it?
>> What is "topbottom"? And how to use it?
>>
> _CFG is an internal dictionary, which contains configuration data. You 
> can change these
> by editing adding or editing the turtle.cfg file (an example is in the 
> Demo directory).
> How to do this you can find here:
>
> http://docs.python.org/dev/library/turtle.html#how-to-configure-screen-and-turtles 
>
>> 3. http://docs.python.org/dev/library/turtle.html#turtle.stamp
>>
>> "turtle.stamp()
>> Stamp a copy of the turtle shape onto the canvas at the current 
>> turtle position. Return a stamp_id for that stamp, which can be used 
>> to delete it by calling clearstamp(stamp_id)."
>>
>> But in your version 1 
>> (<http://article.gmane.org/gmane.comp.python.tutor/49805>), the 
>> turtle is hidden by hideturtle()! What are you stamping? It seems the 
>> shape, or rectangle. If so, why does the doc say 'turtle'?
>>
> there will be stamped the shape of the turtle, that appeared  if it 
> were not hidden.
>
> General remark: you can investigate Python and especially its turtle 
> module interactively
> using IDLE. But note: in the case of graphics (Tkinter and especially 
> turtle.py) you have to
> use the -n switch of IDLE. So the link calling idle must look 
> something like this:
>
> /... ../... ../python  /... ../.../.../idle.pyw -n
>
> the dotted parts representig the path according to your system 
> configuration
>
> If you have done this onceyou can issue function calls on after 
> another and
> observe what happens. E. g.:
>
> >>> from turtle import *
> >>> reset()
> >>> shape("square")
> >>> color("blue", "red")
> >>> resizemode("user")   #  at first no visual effect, but ...
> >>> turtlesize(2,3,5)
> >>> fd(100)
> >>> left(45)
> >>> pensize(8)
> >>> fd(100)
> >>> left(90)
> >>> pencolor("green")
> >>> fd(100)
> >>> turtlesize(1,5)
> >>> left(1080)
> >>> stamp()
> 8
> >>> left(45)
> >>> ht()
> >>> fd(100)
> >>> stamp()
> 9
> >>> left(45)
> >>> fd(100)
> >>> stamp()
> 10
> >>>
>
> ... and so on.
> If you want to know how some function works and the docs
> don't give you a proper clue, simply try out.
>> 4. For my random_rectangles.py program I've started to try out the 
>> new turtle. (See the current state of random_rectanglesV16_web.py at 
>> <http://py77.python.pastebin.com/d3e842821>.)  The only downside I've 
>> found is that the new turtle is much faster that the old. I want to 
>> set the speed so that the outlines of rectangles are drawn slowly 
>> enough that the user (me at present) can both appreciate the colors 
>> and have time to check the names of the colors being printed in the 
>> console window. Using the old turtle, I found that a default delay of 
>> 1 ((delay(1)) was just right; with the new turtle, setting a delay of 
>> even 50 affects only the first cycle of rectangles. So I don't use 
>> delay at all. Setting the slowest speed of 1 (speed(1)) is still too 
>> fast. How can I slow down the drawing of the rectangles?
>>
> I suppose you have some call of reset() or clear() which resets the 
> delay. So try a
> delay call at the beginning of every cycle.
>> 5. I've been unable to figure out how to use onclick() 
>> (<http://docs.python.org/dev/library/turtle.html#turtle.onclick>). 
>> I'd like to find a way to pause my script by clicking on the screen 
>> -- so I could snag an image of what's showing, and then click again 
>> to restart the drawing of the rectangles. And in addition, being able 
>> to stop the program with a double click on the screen would be very 
>> handy. Could you explain how to do these, if in fact they are possible?
>>
> This idea for an event driven program affords a bit of different 
> thinking and a different
> program structure. I'll come up with a proposition and an explanation 
> of some
> prerequisites  in  another posting. (I have not got the time  to do 
> that  now).
>
> Regards,
> Gregor
>> That's all for now.
>>
>> Thanks,
>>
>> Dick
>>
>> _______________________________________________
>> Tutor maillist  -  Tutor at python.org
>> http://mail.python.org/mailman/listinfo/tutor
>>
>>
>
>



More information about the Tutor mailing list