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()
CodePudding user response:
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.