【发布时间】:2017-03-29 21:39:30
【问题描述】:
我正在尝试编写一个 python 乌龟程序,其行为类似于使用游戏循环的常规事件驱动程序。该程序尝试混合鼠标、键盘和计时器事件,如下所示。
我的问题是 python 似乎无法将 onkey() 事件与 ontimer() 循环混合。运行时,程序将为海龟设置动画,并且 onclick() 事件将起作用。在第一次单击鼠标之前,甚至没有注册按键。然后,当按下该键退出时,我在 shell 中得到一大串错误。 bye() 方法似乎以粗暴的方式终止程序,而不是优雅地关闭。
我认为我的命令顺序正确。
任何建议将不胜感激!
import turtle
playGround = turtle.Screen()
playGround.screensize(800, 600, 'light blue')
bob = turtle.Turtle()
bob.color('red')
bob.pencolor('red')
bob.ht()
def teleport(x,y):
bob.goto(x,y)
def quitThis():
playGround.bye()
def moveAround():
bob.fd(10)
bob.rt(15)
playGround.ontimer(moveAround,30)
playGround.onclick(teleport,btn=1)
playGround.onkey(quitThis,'q')
moveAround()
playGround.listen()
playGround.mainloop()
【问题讨论】: