我的代码中缺少海龟绘图环境变量

4

我刚开始使用海龟绘图,找到了一段关于谢尔宾斯基地毯的代码。代码本应能够画出地毯,但我不知道如何添加显示变量。我只是在网上找到这段代码来帮助我。顺便说一句,这不是作业,只是一个有趣的小项目,让我看到地毯的效果。

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()

我收到一个非常奇怪的错误。
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

这不是一个重复问题,因为我询问的是如何修复这段代码,而不是错误的原理。


1
你尝试在什么样的环境中运行这段代码?这个错误通常表示你正在尝试在某些连接(远程登录、串行控制台等)上运行GUI程序,但这些连接并没有关联任何图形显示。 - jasonharper
问题不在代码上,它可以正常工作。问题出在你的环境上。$DISPLAY shell 变量是 tkinter 与 X-windows 链接以显示图形的方式。某些 tkinter 方面的配置不正确。 - cdlane
1个回答

0
问题在于你不能直接导入turtle并立即使用像turtle.forward这样的命令。相反,最好将变量赋值为turtle.Turtle(),然后使用该变量来执行命令。就像这样:
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()

此外,根据您在哪个代码上运行代码,代码将运行一定次数。有关更多信息,您可以查看此question。简而言之,因为您有一个无限循环,在函数中不断调用函数,所以存在最大回调,即函数在函数中可以被调用的最大次数。try和except只是在达到最大回调后简单地打印消息。

你声称,“你不能导入turtle并立即使用像turtle.forward这样的命令”,但这不是真的——你测试过了吗?启动Python,执行import turtle,然后执行turtle.forward(100),它应该可以工作。Turtle具有面向对象的方法和函数的双重性质。当然,面向对象的方法更好,但这并不意味着“不能”。 - cdlane

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接