Turtle.tracer的确切用法是什么?在python文档中,它被写成Turn turtle animation on/off and set delay for update drawings.
我用它来禁用动画,但参数不清楚,例如在这段代码中,如果我使用turtle.trace,turtle不会绘制其余的表如何设置正确的参数。
import turtle
turtle.width(5)
yd=xd=-64
turtle.tracer(8,25)#This is the problem
for i in range(2):
turtle.up()
turtle.goto(-197.5,yd)
turtle.down()
turtle.seth(0)
turtle.fd(394)
yd+=128
turtle.up()
turtle.goto(xd,197.5)
turtle.down()
turtle.seth(270)
turtle.fd(394)
xd+=128
答案 0 :(得分:2)
import turtle
turtle.width(5)
yd=xd=-64
turtle.delay(0) # <----
for i in range(2):
turtle.up()
turtle.goto(-197.5,yd)
turtle.down()
turtle.seth(0)
turtle.fd(394)
yd+=128
turtle.up()
turtle.goto(xd,197.5)
turtle.down()
turtle.seth(270)
turtle.fd(394)
xd+=128
turtle.mainloop()
如果您使用turtle.tracer
:
turtle.update
...
turtle.tracer(8,25)
for i in range(2):
...
turtle.update()
tracer.mainloop()
答案 1 :(得分:0)
What is the exact usage of Turtle.tracer? ... I use it for disabling animation but the arguments are not clear...
The function of the tracer()
method of the turtle screen singleton (also exposed as a top level function) is easy to understand, but it's arguments aren't. The default settings are:
turtle.tracer(1, 10)
Let's deal the optional second argument first. It is simply passed onto the delay()
method of the turtle screen singleton as a convenience. From there it is passed onto turtle's TK underpinnings. Since this answer is about tracer()
I'll not discuss delay()
here.
I see two ways to use tracer()
. The more complicated is to supply a numeric value n
that tells turtle to only perform every nth update instead of every update. This depends on you having a deep understanding of your drawing code to know, for example, you ony need to update every 257th time. And have a sense when updates actually occur. For most of us, that's simply not the case.
The simpler way to use tracer()
is with one boolean argument (posing as a number) and in conjunction with the update()
function:
tracer(False) # turn off visible drawing
for ...:
... # draw stuff
update() # update the visible drawing now
...
tracer(True) # return to normal drawing
A couple of things to note: 1) the clear()
method of the turtle screen singleton resets tracer()
to its default values; 2) some turtle commands, like end_fill()
, may cause updates to occur regardless of your tracer settings.
Your code reworked using the simpler model of tracer()
:
import turtle
yd = xd = -64
turtle.width(5)
turtle.tracer(False)
for i in range(2):
turtle.penup()
turtle.goto(-197.5, yd)
turtle.pendown()
turtle.setheading(0)
turtle.forward(394)
yd += 128
turtle.penup()
turtle.goto(xd, 197.5)
turtle.pendown()
turtle.setheading(270)
turtle.forward(394)
xd += 128
turtle.update() # only display completed lines
turtle.hideturtle()
turtle.tracer(True)
turtle.mainloop()