1

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
unor
  • 92,415
  • 26
  • 211
  • 360
hamidfzm
  • 4,595
  • 8
  • 48
  • 80

2 Answers2

2

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()
falsetru
  • 357,413
  • 63
  • 732
  • 636
  • 1
    tracer.update() ... Really? There's no 'tracer' defined. Have you run this code??? Also, in https://docs.python.org/2/library/turtle.html#turtle.update, it says: turtle.update(): Perform a TurtleScreen update. To be used when tracer is turned off. Godssake! (Yet, I don't downvote your reply because I don't do that. Comments are much more useful than downvoting, which it is often incorrect!) – Apostolos Jan 31 '18 at 11:21
  • @Apostolos, `tracer` comes from OP's code. I ran code without any attribute error. https://docs.python.org/2/library/turtle.html#turtle.tracer (Did you type `tracer(..)` instead of `turtle.tracer(..)` ? – falsetru Jan 31 '18 at 12:57
  • @Apostolos, tracer is turned off after n (8 in the above code) drawings according to documentation. No drawings are done after that; manual `update` call is required to draw rest of drawings. – falsetru Jan 31 '18 at 12:59
  • @Apostolos, With `update`: https://i.imgur.com/8EMSkg3.png, without `update`: https://i.imgur.com/hkYlI1U.png – falsetru Jan 31 '18 at 13:01
  • 1
    I don't know whar you are talking about. tracer.update() raises "NameError: name 'tracer' is not defined". And I didn't type anyting. I copy-pasted the code, as I always do, so that I avoid typing errors accidentally. – Apostolos Feb 01 '18 at 15:18
  • @Apostolos, I'm sorry. I mis-read your comment. Thank you for pointing the error. Updated the code in the answer to fix the issue: `tracer.update()` -> `turtle.update()` – falsetru Feb 01 '18 at 15:41
  • OK, no problem. – Apostolos Feb 05 '18 at 09:04
1

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()
cdlane
  • 40,441
  • 5
  • 32
  • 81