事件驱动环境中的Python Turtle"While True"



我在Stack Overflow上的几篇文章中读到,"像turtle这样的事件驱动环境永远不应该有while True:,因为它可能会屏蔽事件(例如键盘(。">

这里有一个Python-Turtle程序,它看起来工作得很好,但它使用了while True:构造。

有人能解释一下为什么这种方法是错误的,会产生什么问题,以及实现同样结果的正确方法是什么吗?

import turtle
import time

def move_snake():
"""
This function updates the position of the snake's head according to its direction.
"""
if head.direction == "up":
head.sety(head.ycor() + 20)
def go_up():
"""
callback for up key.
"""
if head.direction != "down":
head.direction = "up"
# Set up screen
screen = turtle.Screen()
screen.tracer(0)  # Disable animation so we can update screen manually.
# Event handlers
screen.listen()
screen.onkey(go_up, "Up")
# Snake head
head = turtle.Turtle()
head.shape("square")
head.penup()
head.direction = "stopped"  # Cheeky use of instance property to avoid global variable.

while True:
move_snake()
screen.update()
time.sleep(0.2)
turtle.done()

我可以提供一个粗略的例子。按原样运行上面的代码。让蛇动起来。单击窗口的关闭按钮。计算您在控制台中收到的错误消息的行数。它很容易超过20打。

现在用以下代码进行同样的实验,消除while True::

from turtle import Screen, Turtle
class Head(Turtle):
def __init__(self):
super().__init__(shape="square")
self.penup()
self.direction = "stopped"
def move_snake():
if head.direction == "up":
head.sety(head.ycor() + 20)
screen.update()
screen.ontimer(move_snake, 200)
def go_up():
if head.direction != "down":
head.direction = "up"
# Snake head
head = Head()
# Set up screen
screen = Screen()
screen.tracer(0)  # Disable animation so we can update screen manually.
# Event handlers
screen.onkey(go_up, "Up")
screen.listen()
move_snake()
screen.mainloop()

您的错误消息计数应降至零。这是因为窗口关闭事件与乌龟运动发生在同一事件循环中。

还有其他的效果,你以后会去追求。这只是一个简单易见的例子。

最新更新