龟屏点击不适用于 while 循环
Turtle screen click doesn't work with a while loop
当我运行这个程序时,它开始打印0。当我点击乌龟屏幕时,它进入无响应模式。
我已经尝试将 while True
放入函数中。我还尝试在循环之后放置 onscreenclick
。
from turtle import*
v=0
def g(x,y):
global v
v=v+5
onscreenclick(g)
while True:
print(v)
我预计它会在第一次点击后开始打印 5,在第二次点击后开始打印 10,但是 turtle 在继续打印时进入无响应模式。
这整个程序都是错误的乌龟智慧。但是,我将保留该讲座并简单地说不要在像 turtle 这样基于事件的世界中使用 while True:
。而是使用 ontimer
事件:
from turtle import *
v = 0
def g(x, y):
global v
v += 5
onscreenclick(g)
def repeat():
print(v)
ontimer(repeat, 100)
repeat()
mainloop()
当我运行这个程序时,它开始打印0。当我点击乌龟屏幕时,它进入无响应模式。
我已经尝试将 while True
放入函数中。我还尝试在循环之后放置 onscreenclick
。
from turtle import*
v=0
def g(x,y):
global v
v=v+5
onscreenclick(g)
while True:
print(v)
我预计它会在第一次点击后开始打印 5,在第二次点击后开始打印 10,但是 turtle 在继续打印时进入无响应模式。
这整个程序都是错误的乌龟智慧。但是,我将保留该讲座并简单地说不要在像 turtle 这样基于事件的世界中使用 while True:
。而是使用 ontimer
事件:
from turtle import *
v = 0
def g(x, y):
global v
v += 5
onscreenclick(g)
def repeat():
print(v)
ontimer(repeat, 100)
repeat()
mainloop()