cuong.pq
cuong.pq

Reputation: 157

What does turtle.tracer() do?

I am currently learning about the turtle module in Python. So I read the function of tracer() in this link, but I haven't understood what does n-th regular screen update actually means. If for example, I set screen.tracer(0) or screen.tracer(2, 100) what do they actually do?

Upvotes: 2

Views: 22804

Answers (1)

cdlane
cdlane

Reputation: 41895

The turtle was originally a small programmable physical robot that carried a pen and could trace its path as it moved.

From https://compform.net/turtles

Generally, the computer can draw graphics instantaneously. Displaying every drawing update on the screen, and slowing down these updates, so we can see them, is tracing in turtle graphics.

The tracer() function turns automatic screen updates on or off -- on by default -- and also sets the update() delay. In Python 2, the first argument to tracer() is boolean, True to have automatic screen updates on, False to turn them off. You can still use tracer() this way in Python 3, and it's the way I most commonly use it.

In Python 3, the first argument, n, is a number. If n is 0 (zero), automatic screen updates are off. If n is 1 (one), the default, automatic screen updates will happen. This matches the Python 2 model. When automatic updates are off, you need to explicitly call update() when you want the screen to reflect the current state of the drawing.

But in Python 3, if n is greater than 1 (one), then only every nth automatic screen update will occur. If n is 2, only every other screen update will actually happen. However, there's a glitch:

As I discuss in my tracer() rules of thumb, some Python turtle graphics operations force an update() regardless of tracer() settings. Due to this, and other turtle underpinnings, calculating the proper nth to set n, is error prone. So my recommendation is to stick with the Python 2 model, and ignore this feature.

Finally, the second, delay, argument to tracer(), is the time delay added after an update() to allow users to see the change, before something else gets updated. The default value for this is 10 milliseconds, which is fairly short. This is similar to turtle.speed(), but affects everything, not just an individual turtle.

Upvotes: 7

Related Questions