0

I have a fully functioning game in turtle. I moved it to a tkinter window and it works similar to before, but screen.tracer(0) does not seem to do anything (meaning it does not stop screen updates as expected). Also, screen.addshape("shape") doesn't seem to work either if anyone has an idea about that. I'm wondering if both methods don't actually work when the screen is embedded in a tkinter canvas. The rest of the methods, such as screen.listen() work as expected.

Here is some example code where I tried to get this to work, but the tracer clearly stays on since the turtle continues to draw on the screen when "Up" is pressed:

from turtle import TurtleScreen, RawTurtle
from tkinter import *


def go():
    turtle.forward(20)


window = Tk()
canvas = Canvas()
canvas.pack()
screen = TurtleScreen(canvas)
turtle = RawTurtle(canvas)

screen.tracer(0)

screen.listen()
screen.onkey(fun=go, key="Up")

window.mainloop()
  • Does this answer your question? [What does turtle.tracer() do?](https://stackoverflow.com/questions/62613041/what-does-turtle-tracer-do) – Reblochon Masque Dec 11 '21 at 01:13
  • There is a duplicate to your question, and a great explanation of `turtle.tracer` its arguments, recommended uses, quircks and python 3 pitfalls [here](https://stackoverflow.com/questions/62613041/what-does-turtle-tracer-do). `penup` and `pendown` work as intended. – Reblochon Masque Dec 11 '21 at 01:15
  • @ReblochonMasque Unfortunately, no. That explains very well what `turtle.tracer(0)` does, setting updates to False, but it doesn't explain why tkinter would be affecting this. In the case above, `screen.tracer(0)` should be making it so the screen does not update until `screen.update()` is called. But it continues to update. – Joshua Bilbrey Dec 11 '21 at 01:21

1 Answers1

0

The problem is you initialized your RawTurtle() with a Canvas instead of a TurtleScreen. If we instead do turtle = RawTurtle(screen) everything works the same as standalone turtle:

from turtle import TurtleScreen, RawTurtle
from tkinter import *

def go():
    turtle.forward(20)

window = Tk()
canvas = Canvas()
canvas.pack()

screen = TurtleScreen(canvas)
turtle = RawTurtle(screen)

screen.tracer(0)

screen.onkey(fun=go, key="Up")
screen.listen()

screen.mainloop()

If you now add screen.update() as the last line of the go() event handler, the arrow will move and leave a trailing line.

cdlane
  • 40,441
  • 5
  • 32
  • 81