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()
ggorlen
  • 44,755
  • 7
  • 76
  • 106
  • Related: [Third Clone Of The Turtle](https://stackoverflow.com/questions/75559538/third-clone-of-the-turtle/75560168#75559538). `turtle.getscreen` is a global function that creates the singleton functional turtle if it doesn't already exist. – ggorlen Feb 24 '23 at 19:13

3 Answers3

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.

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
  • 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() – Nicholas Chill Jun 08 '20 at 14:05
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.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
cdlane
  • 40,441
  • 5
  • 32
  • 81
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.

Alex
  • 29
  • 4