0

I am learning Python (2.7) and currently turtles is on the list.

Regards the documentation there are exitonclick() and onclick() etc. functions. However, I have some problems utilizing them.

For instance: This click event is working, but only after the loop finishes:

[...]
for i in range(4):
    trtl.forward(100)
    trtl.left(90)
scrn.exitonclick()
[...]

But what I'd like to do are things like this (but are not working):

[...]
while not scrn.screenonclick():
    trtl.forward(100)
    trtl.left(91)
[...]

or maybe like this:

[...]
while True:
    trtl.forward(100)
    trtl.left(91)
    scrn.screenonclick(break)
[...]

I think you get the general idea about what concepts I try to experiment with.

Any tips in using these onclick methods or any alternative ways in accomplishing an onclick interrupt?

Thanks!

1 Answer 1

1

Have you tried moving the click handler to the beginning? Here binding to the click will be made before the drawing starts:

def say_bye(x, y):
    bye()

scrn.onclick(say_bye)

for i in range(4):
    trtl.forward(100)
    trtl.left(90)
Sign up to request clarification or add additional context in comments.

6 Comments

hm, unfortunately it is not an attribute: "AttributeError: '_Screen' object has no attribute 'screenonclick'"
Unfortunately, the second idea does not work neither. The program just blocks on exitonclick() until the screen is clicked - which obviously exits the screen before it goes into the loop. ^^
One more try please :)
Thx for your persistence. :-) Yes this works! At least in a pragmatic way. The downside seems to be that the Screen instance has to be a global variable, so bye() can be called. :-/
Wonderful! Could you accept the answer if you are happy with it? Here is how: meta.stackexchange.com/questions/23138/…
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.