Vankog
Vankog

Reputation: 687

Python: turtle - (exit)onclick while in infinite loop?

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!

Upvotes: 0

Views: 5410

Answers (1)

arocks
arocks

Reputation: 2882

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)

Upvotes: 1

Related Questions