如何在Python中隐藏乌龟图标/指针

Bry*_*ant 5 python turtle-graphics

使用Python Turtle时,如何在Turtle代码中隐藏龟图形中的龟图标/指针,以便在测试时不会显示?

Pet*_*ood 11

文档有一个关于可见性的部分:

turtle.hideturtle()
turtle.ht()
让乌龟看不见.当你正在做一些复杂的绘图时,这是一个好主意,因为隐藏龟可以显着加快绘图速度.

>>> turtle.hideturtle()
Run Code Online (Sandbox Code Playgroud)

此外,你可以取消隐藏乌龟:

turtle.showturtle()
turtle.st()
让乌龟可见.

>>> turtle.showturtle()
Run Code Online (Sandbox Code Playgroud)

您还可以查询其可见性:

turtle.isvisible()
True如果显示海龟,False如果它被隐藏,则 返回.

>>> turtle.hideturtle()
>>> turtle.isvisible()
False
>>> turtle.showturtle()
>>> turtle.isvisible()
True
Run Code Online (Sandbox Code Playgroud)

  • 你也可以看到"天生"看不见的乌龟:`turtle = turtle.Turtle(visible = False)`这允许我将乌龟移动到我试图解决的问题的逻辑起点,并使它可见,而不是显示乌龟从原点移动到首选的起始位置.如果我使用乌龟来写文本或其他实用程序,我会使用它. (2认同)

Ann*_*Zen 7

另一个答案未能解决的一种更实用的方法是在定义对象时设置visible关键字参数:FalseTurtle

import turtle

my_turtle = turtle.Turtle(visible=False)
Run Code Online (Sandbox Code Playgroud)

当然,这是当您希望Turtle从程序一开始就不可见的时候。

当您定义一个Turtle对象而不设置visible为时False,总会有一个闪电般的短暂时刻,海龟仍然可见:


import turtle

my_turtle = turtle.Turtle()
# The Turtle may be visible before the program reaches the line under, depending on the speed of your computer 
my_turtle.hideturtle()
Run Code Online (Sandbox Code Playgroud)

visible关键字参数设置为 后False,您始终可以在代码中需要再次可见和隐藏的地方调用my_turtle.showturtle()和。my_turtle.hideturtle()Turtle


turtle以下是您可以自定义的所有默认设置(此处感兴趣的设置是用 注释的设置# RawTurtle

_CFG = {"width" : 0.5,               # Screen
        "height" : 0.75,
        "canvwidth" : 400,
        "canvheight": 300,
        "leftright": None,
        "topbottom": None,
        "mode": "standard",          # TurtleScreen
        "colormode": 1.0,
        "delay": 10,
        "undobuffersize": 1000,      # RawTurtle
        "shape": "classic",
        "pencolor" : "black",
        "fillcolor" : "black",
        "resizemode" : "noresize",
        "visible" : True,
        "language": "english",        # docstrings
        "exampleturtle": "turtle",
        "examplescreen": "screen",
        "title": "Python Turtle Graphics",
        "using_IDLE": False
       }
Run Code Online (Sandbox Code Playgroud)

更新:我刚刚注意到 cdlane 对另一个答案的评论指出了这种方法,但评论是暂时的。