Skip to content Skip to sidebar Skip to footer

Avoid RecursionError In Turtle Paint Code

I'm creating a simple program that uses turtle in a tkinter canvas to allow the user to draw with the mouse. The drawing function seems to work fine however after you draw for a b

Solution 1:

I was not able to reproduce your recursion error but I have seen it before. Usually in situations where there are new events coming in while the event handler is busy handling an event. (Which is why it looks like recursion.) The usual fix is to turn off the event handler inside the event handler. Give this a try to see if it works any better for you:

from turtle import RawTurtle, ScrolledCanvas, TurtleScreen
import tkinter as tk

box = tk.Tk()

canv = ScrolledCanvas(box)
canv.pack()

screen = TurtleScreen(canv)

p = RawTurtle(screen)
p.speed('fastest')

def draw(x, y):
    p.ondrag(None)
    p.setheading(p.towards(x, y))
    p.goto(x, y)
    p.ondrag(draw)

p.ondrag(draw)

box.mainloop()

Post a Comment for "Avoid RecursionError In Turtle Paint Code"