Python 海龟Tk主回路

Python 海龟Tk主回路,python,tkinter,python-turtle,Python,Tkinter,Python Turtle,在尝试将海龟移植到不同的后端(而不是Tkinter)时,我遇到了以下问题 from turtle import * onscreenclick(lambda x,y:print(x,y)) while True: #a=heading() # option 1. clicks are not reported setheading(0) # option 2: clicks are reported 请注意,未调用mainloop()。 虽然我知道在turtle编程中使用wh

在尝试将海龟移植到不同的后端(而不是Tkinter)时,我遇到了以下问题

from turtle import *
onscreenclick(lambda x,y:print(x,y))
while True:
    #a=heading()  # option 1. clicks are not reported
    setheading(0) # option 2: clicks are reported
请注意,未调用mainloop()。
虽然我知道在turtle编程中使用while True循环是一个坏习惯,但我不明白为什么这个程序使用选项2。允许事件在事件循环之外调度和传播的魔法是什么?

在选项1中,您询问的是海龟,在选项2中,您要求海龟做一些事情。我的猜测是,在第一种情况下,没有切换到事件处理程序来检查点击。在第二种情况下,将切换到事件处理程序,作为执行某些操作的一部分

这与标准turtle(使用Tkinter)上的行为相同,其中第一个选项从不打印。让我们通过添加对
update()
的调用,使这两种情况都能发挥作用

这在标准turtle下对这两种情况都有效,因为现在我们在每次迭代中都要交给事件处理者。这就是为什么
而True
不仅仅是一个坏习惯,而是一个坏主意的例子。在standard turtle中,我将使用计时器事件编写以下内容:

from turtle import *

onscreenclick(print)

def run():
    a = heading()  # option 1. clicks are reported
    # setheading(0)  # option 2: clicks are reported

    ontimer(run)

run()

mainloop()

这是否意味着有一个并行事件处理程序在主循环之外运行?我在turtle.py中找不到它(但我的tk知识非常有限)。
from turtle import *

onscreenclick(print)

def run():
    a = heading()  # option 1. clicks are reported
    # setheading(0)  # option 2: clicks are reported

    ontimer(run)

run()

mainloop()