'Turtle No Display Environment Variable in my code

I just started using turtle and I found code online for Sierpinski's Carpet. It should be able to draw the Carpet without problem, but I don't know how to add a display variable. I just found this code online to help me. By the way, this is not homework. It is a fun little project to see the carpet in action. Here's the code

import turtle,math
def s(n, l):

    if n == 0: # stop conditions

        # draw filled rectangle

        turtle.color('black')
        turtle.begin_fill()
        for _ in range (4):
            turtle.forward(l)
            turtle.left(90)
        turtle.end_fill()

    else: # recursion

        # around center point create 8 smalles rectangles.
        # create two rectangles on every side 
        # so you have to repeat it four times

        for _ in range(4):
            # first rectangle
            s(n-1, l/3)    
            turtle.forward(l/3)

            # second rectangle
            s(n-1, l/3)    
            turtle.forward(l/3)

            # go to next corner
            turtle.forward(l/3)
            turtle.left(90)

        # update screen
        turtle.update()

# --- main ---    

# stop updating screen (to make it faster)
turtle.tracer(0) 

# start
s(4, 400)

# event loop
turtle.done()

I get a very weird error

Traceback (most recent call last):
  File "main.py", line 40, in <module>
    turtle.tracer(0)
  File "<string>", line 6, in tracer
  File "/usr/local/lib/python3.8/turtle.py", line 3662, in Screen
    Turtle._screen = _Screen()
  File "/usr/local/lib/python3.8/turtle.py", line 3678, in __init__
    _Screen._root = self._root = _Root()
  File "/usr/local/lib/python3.8/turtle.py", line 434, in __init__
    TK.Tk.__init__(self)
  File "/usr/local/lib/python3.8/tkinter/__init__.py", line 2261, in __init__
    self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
_tkinter.TclError: no display name and no $DISPLAY environment variable

This is not a duplicate because I’m asking how to fix THIS CODE, not how the error works.



Solution 1:[1]

The problem is you can't import turtle and immediately use commands like turtle.forward. Instead, it's best to assign a variable equal to turtle.Turtle() and then use the variable to execute commands. Like so:

import turtle
import math
t = turtle.Turtle()
def s(n, l):

    if n == 0: # stop conditions

        # draw filled rectangle

        t.color('black')
        t.begin_fill()
        for _ in range (4):
            t.forward(l)
            t.left(90)
        t.end_fill()

    else: # recursion

        # around center point create 8 smalles rectangles.
        # create two rectangles on every side 
        # so you have to repeat it four times

        for _ in range(4):
            # first rectangle
            s(n-1, l/3)    
            t.forward(l/3)

            # second rectangle
            s(n-1, l/3)    
            t.forward(l/3)

            # go to next corner
            t.forward(l/3)
            t.left(90)

        # update screen
        t.update()

# --- main ---    

# stop updating screen (to make it faster)
t.tracer(0) 

# start
try:
  s(4, 400)
except:
  print("This loop has reached it's max callback")
# event loop 
t.done()

Also, depending on what you run the code on the code will run a certain number of times. For more information you can view this question. In summary, because you have an infinite loop where you keep calling the function in the function, there is a maximum callback, which is the maximum number of times the function can be called in the function. The try and except are there to simply print the message after you have reached the maximum callback.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 WangGang