使用Python Turtle时,如何在Turtle代码中隐藏龟图形中的龟图标/指针,以便在测试时不会显示?
答案 0 :(得分:9)
documentation在Visibility上有一个部分:
turtle.hideturtle()
turtle.ht()
让乌龟看不见。当你正在做一些复杂的绘图时,这是一个好主意,因为隐藏龟可以显着加快绘图速度。
>>> turtle.hideturtle()
另外,你可以取消隐藏乌龟:
turtle.showturtle()
turtle.st()
让乌龟可见。
>>> turtle.showturtle()
您还可以查询其可见性:
turtle.isvisible()
如果显示Turtle,则返回True
,如果隐藏,则返回False
。
>>> turtle.hideturtle()
>>> turtle.isvisible()
False
>>> turtle.showturtle()
>>> turtle.isvisible()
True
答案 1 :(得分:0)
另一个无法解决的实用方法是,在定义visible
对象时,将False
关键字参数设置为Turtle
:
import turtle
my_turtle = turtle.Turtle(visible=False)
当然,这是当您希望从程序的开始就看不见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()
在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
}
更新:我只是注意到cdlane在另一个答案上的评论指出了这种方法,但是评论是暂时的。