3

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.

  • 1
    What is the exact environment you're trying to run this code in? This error typically indicates that you're trying to run a GUI program over some connection (remote login, serial console, etc.) that isn't associated with any sort of graphical display. – jasonharper Mar 13 '20 at 13:24
  • The problem is not with the code, it works fine. The problem is with your *environment*. The `$DISPLAY` shell variable is how tkinter links up with X-windows to display the graphics. Something's not configured correctly tkinter-wise. – cdlane Mar 14 '20 at 03:33

1 Answers1

0

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.

WangGang
  • 533
  • 3
  • 15
  • You claim, "you can't import turtle and immediately use commands like turtle.forward" but that's not true -- did you test it? Start Python, do `import turtle` followed by `turtle.forward(100)` and it *should* work. Turtle has a dual nature of having object-oriented methods as well as functions. Of course, the OO approach is better, but that's not the same as "can't". – cdlane Mar 14 '20 at 03:31