4

I decided to use the built-in turtle for the display of my whole program, but if there's a better option you can leave it here too.

So, when I was using the turtle and bound a function to the left click drag, it ends up working fine, but only for slow mouse velocities and, thus, for short amounts of time before crashing my kernel and giving me a fatal "stack overflow" error.

Code:

from turtle import *
screen = Screen()
t1 = Turtle()
t1.shape("circle")
t1.pu()
bi = 1
ni = 1
screen.tracer(None, 0)
t1.speed(0)
screen.screensize(1000,1000)
def grow(ke):
    t1.goto(ke.x - 475,-ke.y + 400)
    global bi, ni
    t1.shapesize(bi,ni)
    bi += .004
    ni += .004
s2 = getcanvas()
s2.bind("<B1-Motion>", grow)
s2.bind("<Button-1>", grow)
aargon
  • 126
  • 3
  • 9
  • Just an fyi, your kernel did not crash, your program did. If the kernel crashed I would not expect your program to be the cause – cujo May 25 '18 at 15:58

1 Answers1

1

There are several issues with your code:

  • You didn't disable events inside the event hander, which is what lead to your fatal "stack overflow" error.

  • You bypassed turtle's own event mechanism and used that of the tkinter underpinning. Sometimes that's necessary, but it's not the place to start.

  • You don't need to turn off tracer() as you're not drawing anything.

Below is my rework of your code that I believe achieves your basic goals. You can drag the turtle around the screen cleanly and it will grow as you do. You can click anywhere on the screen and the turtle will come to you and grow:

from turtle import Turtle, Screen

def grow(x, y):
    global bi, ni

    turtle.ondrag(None)  # disable events when inside handler
    screen.onclick(None)

    turtle.goto(x, y)
    turtle.shapesize(bi, ni)

    bi += 0.04
    ni += 0.04

    turtle.ondrag(grow)
    screen.onclick(grow)

screen = Screen()
screen.screensize(1000, 1000)

turtle = Turtle('circle')
turtle.speed('fastest')
turtle.penup()

bi = ni = 1

turtle.ondrag(grow)
screen.onclick(grow)

screen.mainloop()
cdlane
  • 40,441
  • 5
  • 32
  • 81