简体   繁体   中英

Turtle screen click doesn't work with a while loop

When I run the program, it begins printing 0. When I click on the turtle screen, it goes to not responding mode.

I've already tried putting the while True within the function. I also tried putting onscreenclick after the loop.

from turtle import*
v=0
def g(x,y):
    global v
    v=v+5
onscreenclick(g)
while True:
    print(v)

I expected it to begin printing 5 after the first click, 10 after the second and but turtle goes to not responding mode while printing continues.

This entire program is wrong-headed turtle-wise. But, I'll save that lecture and simply say don't use while True: in an event-based world like turtle. Instead use an ontimer event:

from turtle import *

v = 0

def g(x, y):
    global v
    v += 5

onscreenclick(g)

def repeat():
    print(v)
    ontimer(repeat, 100)

repeat()

mainloop()

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