Why is there a second Turtle?
Asked Answered
F

3

1

I am learning turtle graphics in python and for some reason there is a second turtle on the screen and I haven't even created a second turtle. How can I get rid of the second turtle?

import turtle
s = turtle.getscreen()
t = turtle.Turtle()
for i in range(4):
    t.fd(100)
    t.rt(90)
turtle.exitonclick()
Feeding answered 8/6, 2020 at 13:41 Comment(1)
Related: Third Clone Of The Turtle. turtle.getscreen is a global function that creates the singleton functional turtle if it doesn't already exist.Coloration
S
1

The second turtle at the starting location appears because of the line s = turtle.getscreen().

This line is not needed (you do not use s), and if you remove it this turtle disappears but the rest of the code seems to work as before.

Subadar answered 8/6, 2020 at 14:1 Comment(1)
That fixed the problem. Thanks! I was following a guide on realpython.com and I was told that to open the turtle screen I need to write s = turtle.getscreen()Feeding
I
1

The turtle library exposes two interfaces, a functional one (for beginners) and an object-oriented one. You got that extra turtle because you mixed the two interfaces (and @mkrieger1's solution doesn't fix that completely).

I always recommend an import like:

from turtle import Screen, Turtle

screen = Screen()
turtle = Turtle()

for _ in range(4):
    turtle.forward(100)
    turtle.right(90)

screen.exitonclick()

This gives you access to the object-oriented interface and blocks the functional one. Mixing the two leads to all sorts of bugs and artifacts.

Incipit answered 10/6, 2020 at 5:59 Comment(0)
H
1

To combine the answer from mkrieger1 and cdlane, you could replace

s = turtle.getscreen()

with

s = turtle.Screen()

You've still got a variable holding the screen (in case you should ever need it), and it doesn't generate that extra turtle in the center.

Homocyclic answered 16/12, 2021 at 19:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.