获得“超过最大递归深度"用 Python 龟鼠标移动 [英] Getting "maximum recursion depth exceeded" with Python turtle mouse move

查看:77
本文介绍了获得“超过最大递归深度"用 Python 龟鼠标移动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码应利用鼠标移动事件在当前鼠标位置绘制一个点:

导入海龟定义运动(事件):x, y = 事件.x, 事件.y龟.goto(x-300, 300-y)乌龟点(5,红色")龟.pu()龟.setup(600, 600)海龟.hideturtle()画布 = 海龟.getcanvas()canvas.bind("<Motion>", 运动)

如果鼠标移动得很慢,代码会按预期工作几秒钟或更长时间.然后它抛出:

<预><代码>>>>====================== 重新启动:C:/code/turtle_move.py======================>>>Tkinter 回调中的异常回溯(最近一次调用最后一次):文件C:\Users\...\AppData\Local\Programs\Python\Python36-32\lib\tkinter\__init__.py",第 1698 行,在 __call__ 中args = self.subst(*args)文件C:\Users\...\AppData\Local\Programs\Python\Python36-32\lib\tkinter\__init__.py",第 1428 行,在 _substitutee.type = EventType(T)RecursionError:超过最大递归深度================================ 重新启动:Shell================================>>>

非常感谢任何帮助.

解决方案

问题是当你的事件处理程序仍在处理前一个事件时,一个新事件进来了,所以事件处理程序从事件处理程序内部被调用,看起来就像一个递归!修复方法是在事件处理程序中禁用事件绑定:

from turtle import Screen, Turtle定义运动(事件):canvas.unbind("")龟.goto(event.x - 300, 300 - event.y)乌龟点(5,红色")canvas.bind("<Motion>", 运动)屏幕 = 屏幕()screen.setup(600, 600)乌龟=乌龟(可见=假)龟速('最快')乌龟.penup()画布 = screen.getcanvas()canvas.bind("<Motion>", 运动)screen.mainloop()

This code should utilize mouse motion events to draw a dot at the current mouse position:

import turtle

def 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)

The code works as expected for a few seconds or longer if the mouse is moved very slowly. Then it throws:

>>> 
====================== RESTART: C:/code/turtle_move.py 
======================
>>> Exception in Tkinter callback
Traceback (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 
===============================
>>>

Any help is much appreciated.

解决方案

The problem is that a new event comes in while your event hander is still processing the previous event, so the event handler gets called from inside the event handler which looks like a recursion! The fix is to disable the event binding while inside the event handler:

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()

这篇关于获得“超过最大递归深度"用 Python 龟鼠标移动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆