此代码应利用鼠标移动事件在当前鼠标位置绘制一个点:import turtledef motion(event): x, y = event.x, event.y turtle.goto(x-300, 300-y) turtle.dot(5, "red")turtle.pu()turtle.setup(600, 600)turtle.hideturtle()canvas = turtle.getcanvas()canvas.bind("<Motion>", motion)如果鼠标移动得很慢,代码会按预期工作几秒钟或更长时间。然后它抛出:>>> ====================== RESTART: C:/code/turtle_move.py ======================>>> Exception in Tkinter callbackTraceback (most recent call last): File "C:\Users\...\AppData\Local\Programs\Python\Python36-32\lib\tkinter\__init__.py", line 1698, in __call__ args = self.subst(*args) File "C:\Users\...\AppData\Local\Programs\Python\Python36-32\lib\tkinter\__init__.py", line 1428, in _substitute e.type = EventType(T)RecursionError: maximum recursion depth exceeded=============================== RESTART: Shell ===============================>>>任何帮助深表感谢。
1 回答
慕容3067478
TA贡献1773条经验 获得超3个赞
问题是当您的事件处理程序仍在处理前一个事件时,一个新事件进来了,因此事件处理程序从事件处理程序内部被调用,这看起来像一个递归!修复方法是在事件处理程序中禁用事件绑定:
from turtle import Screen, Turtle
def motion(event):
canvas.unbind("<Motion>")
turtle.goto(event.x - 300, 300 - event.y)
turtle.dot(5, "red")
canvas.bind("<Motion>", motion)
screen = Screen()
screen.setup(600, 600)
turtle = Turtle(visible=False)
turtle.speed('fastest')
turtle.penup()
canvas = screen.getcanvas()
canvas.bind("<Motion>", motion)
screen.mainloop()
添加回答
举报
0/150
提交
取消