简体   繁体   中英

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!

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM