Hide Turtle Window?

Question:

I am generating diagrams in Turtle, and as part of my program, I identify certain coordinates from my diagrams. I would like to be able to hide the complete turtle window, since I only care about the coordinates, is that possible?

Edit:

QUESTION 2:

This isn’t really an answer, but a few other questions.

I got my program working to some extent, if you run it in IDLE and type “l” it will give you the list with the coordinates.

import Tkinter
import turtle

from turtle import rt, lt, fd   # Right, Left, Forward

size = 10

root = Tkinter.Tk()
root.withdraw()

c = Tkinter.Canvas(master = root)
t = turtle.RawTurtle(c)

t.speed("Fastest")

# List entire coordinates
l = []

def findAndStoreCoords():
    x = t.xcor()
    y = t.ycor()

    x = round(x, 0)     # Round x to the nearest integer
    y = round(y, 0)     # Round y to the nearest integer

    # Integrate coordinates into sub-list
    l.append([x, y])

def hilbert(level, angle):
    if level == 0:
        return

    t.rt(angle)
    hilbert(level - 1, -angle)
    t.fd(size)
    findAndStoreCoords()
    t.lt(angle)
    hilbert(level - 1, angle)
    t.fd(size)
    findAndStoreCoords()
    hilbert(level - 1, angle)
    t.lt(angle)
    t.fd(size)
    findAndStoreCoords()
    hilbert(level - 1, -angle)
    t.rt(angle)

The problem is that Turtle is so SLOW! Is there any package that is just like Turtle but can do commands much faster?

Asked By: ElectroNerd

||

Answers:

Yes, this is possible. The simplest way is to instantiate a root Tkinter window, withdraw it, and then use it as the master window for a RawTurtle’s Canvas instance.

Example:

import Tkinter
import turtle

root=Tkinter.Tk()
root.withdraw()
c=Tkinter.Canvas(master=root)
t=turtle.RawTurtle(c)
t.fd(5)
print t.xcor() # outputs 5.0

Unfortunately, this still initiates the graphics system, but no window will appear.

Answered By: Ted Kaplan

I reimplemented the turtle class as suggested by thirtyseven. It is consistent with the api. (i.e. when you turn right in this class, it is the same as turning right in turtle.

This does not implement all the methods in the api, only common ones. (And the ones you used).

However, it’s short and fairly straightforward to extend. Also, it keeps track of all of the points it has been to. It does this by adding an entry to pointsVisited every time you call forward, backward, or setpos (or any of the aliases for those functions).

import math

class UndrawnTurtle():
    def __init__(self):
        self.x, self.y, self.angle = 0.0, 0.0, 0.0
        self.pointsVisited = []
        self._visit()

    def position(self):
        return self.x, self.y

    def xcor(self):
        return self.x

    def ycor(self):
        return self.y

    def forward(self, distance):
        angle_radians = math.radians(self.angle)

        self.x += math.cos(angle_radians) * distance
        self.y += math.sin(angle_radians) * distance

        self._visit()

    def backward(self, distance):
        self.forward(-distance)

    def right(self, angle):
        self.angle -= angle

    def left(self, angle):
        self.angle += angle

    def setpos(self, x, y = None):
        """Can be passed either a tuple or two numbers."""
        if y == None:
            self.x = x[0]
            self.y = y[1]
        else:
            self.x = x
            self.y = y
        self._visit()

    def _visit(self):
        """Add point to the list of points gone to by the turtle."""
        self.pointsVisited.append(self.position())

    # Now for some aliases. Everything that's implemented in this class
    # should be aliased the same way as the actual api.
    fd = forward
    bk = backward
    back = backward
    rt = right
    lt = left
    setposition = setpos
    goto = setpos
    pos = position

ut = UndrawnTurtle()
Answered By: Nick ODell

The problem is that Turtle is so SLOW! Is there any package that is
just like Turtle but can do commands much faster?

Yes, turtle can. If we add a TurtleScreen to the tkinter implementation, and use it’s tracer() functionality, we can speed things up more than turtle’s speed() method. And we can simplify the code greatly by tossing the customizations and simply use turtle’s own begin_poly(), end_poly() and get_poly() methods:

from tkinter import Tk, Canvas
from turtle import TurtleScreen, RawTurtle

SIZE = 10

def hilbert(level, angle):
    if level == 0:
        return

    turtle.right(angle)
    hilbert(level - 1, -angle)
    turtle.forward(SIZE)
    turtle.left(angle)
    hilbert(level - 1, angle)
    turtle.forward(SIZE)
    hilbert(level - 1, angle)
    turtle.left(angle)
    turtle.forward(SIZE)
    hilbert(level - 1, -angle)
    turtle.right(angle)

root = Tk()
root.withdraw()

canvas = Canvas(master=root)
screen = TurtleScreen(canvas)
screen.tracer(False)  # turn off turtle animation

turtle = RawTurtle(screen)
turtle.begin_poly()  # start tracking movements

hilbert(5, 90)

turtle.end_poly()  # end tracking movements

print(turtle.get_poly())

This prints all the points in a level 5 Hilbert curve in about 1/3 of a second on my system. Your posted code toke nearly 9 seconds to output a level 4.

Answered By: cdlane