-1

Is there a way to make an instance of turtle.Turtle() and not open the window?

I've tried using turtle.Screen.bye immediately but that just opens it then closes it.

Isiah
  • 195
  • 2
  • 15
  • Where is the turtle supposed to live if not on the canvas? It is a graphical object. What would it even mean for an instance of it to exist in a non-graphical context? Please clarify your question. – John Coleman Jul 06 '21 at 22:50
  • Possible duplicate: [python - Hide Turtle Window?](https://stackoverflow.com/q/7236879/4996248) – John Coleman Jul 06 '21 at 22:54

1 Answers1

0

Is there a way to make an instance of turtle.Turtle() and not open the window?

You can emulate this using the tkinter underpinnings of the turtle library. You can either work standalone the turtle level and reach down to tkinter, or you can work embedded in tkinter:

from tkinter import Canvas, Tk
from turtle import RawTurtle

(window := Tk()).withdraw()

canvas = Canvas(window, width=500, height=500)
canvas.pack()

(turtle := RawTurtle(canvas)).hideturtle()
turtle.speed('fast')

for _ in range(9):
    turtle.circle(100)
    turtle.left(40)

window.deiconify()
window.mainloop()

The program creates and immediately withdraws the tkinter window. It then takes its time drawing a turtle graphic. When it's done (about 10 seconds on my system), it opens the window for the user to see. Or, never open the window if, for example, you want to extract a polygon and print it for the user.

We can draw this graphic faster, I chose to draw it slow enough to demonstrate the principle. There are likely other ways to do this from Tk, e.g. setting the alpha of the window to make invisible and then setting it back to opaque.

cdlane
  • 40,441
  • 5
  • 32
  • 81