Turtle tracer arguments example trouble

拟墨画扇 提交于 2021-02-10 14:25:44

问题


What is the exact usage of Turtle.tracer? In python docs it is written Turn turtle animation on/off and set delay for update drawings. I use it for disabling animation but the arguments are not clear for example in this code if I use turtle.trace turtle doesn't draw rest of table how to set correct arguments.

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

回答1:


Use turtle.delay(0):

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

Or use turtle.update if you use turtle.tracer:

...
turtle.tracer(8,25)
for i in range(2):
    ...
turtle.update()
tracer.mainloop()



回答2:


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


来源:https://stackoverflow.com/questions/19619858/turtle-tracer-arguments-example-trouble

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!