0

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 bit the program stops and calls a RecursionError.

from turtle import *
import tkinter as tk

box=tk.Tk()

canv=ScrolledCanvas(box)
canv.pack()

screen=TurtleScreen(canv)

p=RawTurtle(screen)

p.speed(0)

def draw(event):

    p.goto(event.x-256,185-event.y) #The numbers are here because otherwise the turtle draws off center. Might be different depending on computer.

canv.bind('<B1-Motion>', draw)


box.mainloop()

This should in theory draw just fine but instead it is calling a RecursionError. I'm lost as to what I can do to prevent this other than maybe wrap the function in a try loop. Any help would be really appreciated.

The output in the Python Shell:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Program Files (x86)\Python36-32\lib\tkinter\__init__.py", line 1699, in __call__
    return self.func(*args)
  File "C:\Users\Charlie\Desktop\demo.py", line 17, in draw
    p.speed(0)
  File "C:\Program Files (x86)\Python36-32\lib\turtle.py", line 2174, in speed
    self.pen(speed=speed)
  File "C:\Program Files (x86)\Python36-32\lib\turtle.py", line 2459, in pen
    self._update()
  File "C:\Program Files (x86)\Python36-32\lib\turtle.py", line 2660, in _update
    self._update_data()
  File "C:\Program Files (x86)\Python36-32\lib\turtle.py", line 2651, in _update_data
    self._pencolor, self._pensize)
  File "C:\Program Files (x86)\Python36-32\lib\turtle.py", line 545, in _drawline
    self.cv.coords(lineitem, *cl)
  File "<string>", line 1, in coords
RecursionError: maximum recursion depth exceeded while calling a Python object

I've spent a good deal of time looking around for an answer and as far as I can tell there isn't one anywhere easy to find. This is my first question on this site so please forgive me if something I did was wrong.

2
  • I don't know why but it has some problem with speed(0). Maybe use it only once, outside draw() Commented Dec 7, 2017 at 1:32
  • Moved it and it still calls an error - Also forgot to change bind when I wrote it on here. The problem only occurs when dragging mouse. Commented Dec 7, 2017 at 1:55

1 Answer 1

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()
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the help but I realized looking back that the bind was supposed to be '<B1-Motion>' but instead I wrote '<Button-1>'. if you run the edited code you can see the error I was referring to. Again thank you.
@CharlesD., I've updated my example accordingly. You'll need to click on the turtle itself, not just somewhere on the screen, to drag it around.
thank you very much! This should work to serve my purposes just fine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.