Detecting a click on the turtle screen when the turtle isn't doing anything?

woooee at gmail.com woooee at gmail.com
Tue Feb 5 19:08:01 CET 2013


> waiting = False
> 
> 
> 
> def clicked(x, y):
> 
>     global waiting
> 
>     print('clicked at %f %f' % (x,y))
> 
>     waiting = False
> 
>     return
> 
> 
> 
> def wait_for_click(s):
> 
>     global waiting
> 
>     waiting = True
> 
>     s.listen()
> 
>     while waiting:
> 
>         time.sleep(1)
> 
>     return
> 
> 
> 
> 
> 
> ...
> 
> t = turtle.Pen()
> 
> s = turtle.Screen()
> 
> ...
> 
> traverse.plot(s, t, "black", scale, adjx, adjy)
> 
> wait_for_click(s)
> 
> bowditch.plot(s, t, "red", scale, adjx, adjy)
> 
> wait_for_click(s)
> 
> transit.plot(s, t, "blue", scale, adjx, adjy)
> 
> wait_for_click(s)

Note that the code you posted does not call onclick().  Globals are confusing IMHO.  Code becomes cleaner and easier to write and read when you become familiar with classes.

import turtle

class TurtleTest():
    def __init__(self):
        self.ctr=0
  
        t = turtle.Turtle() 
        s = turtle.Screen() 
        s.onclick(self.clicked)
        turtle.mainloop()

    def clicked(self, x, y):
        print self.ctr
        if 0 == self.ctr:
            self.first() 
        elif 1 == self.ctr:
            self.second()      
        elif 2 == self.ctr:
            self.third()

        self.ctr += 1

    def first(self):
        print "first called"

    def second(self):
        print "second called"

    def third(self):
        print "third called"

TT = TurtleTest()



More information about the Python-list mailing list