otc09
otc09

Reputation: 89

Turtle Graphic Window not working from VS Code

I am using Visual Studio Code as my IDE and I am a bit of a beginner with Python so I decided to try out the turtle library built into Python to learn some of the syntax. However, when I tried running just a simple script to see if it would work, the window flashed open for less than a second then closed. I have tried using different extensions and re-downloading the python extension for VS Code. This is my code I'm trying to run:

import turtle
geoff = turtle.Turtle()

geoff.forward(100)

Please help as I really can't figure out why the window won't stay open. Thanks!

Upvotes: 5

Views: 35638

Answers (4)

Pressing_Keys_24_7
Pressing_Keys_24_7

Reputation: 1863

The easiest solution is to add the following line in your V.S. Code:-

turtle.done()

This would prevent the window (Python Turtle Graphics) from closing after running the code:)

Upvotes: 6

Wilder Quiceno
Wilder Quiceno

Reputation: 1

You can create a canvas into turtle like a blank space to draw on. Use this code just to import the module an hold on the graphic window open -Pen It will work with Visual Studio Code, Spyder or Python IDLE

import turtle
window = turtle.Screen()
geoff = turtle.Turtle()
t = turtle.Pen()
window.exitonclick()

Upvotes: 0

Riddhi Narkar
Riddhi Narkar

Reputation: 51

You can use exitonclick() to avoid the window from shutting down.

import turtle
window = turtle.Screen()

geoff = turtle.Turtle()
geoff.forward(100)

window.exitonclick()

This way, the graphics window will shut only after you click.

Upvotes: 5

Brett Cannon
Brett Cannon

Reputation: 16030

The screen flashed on and then closed because when the application is done, Python exits and thus so does the screen. This has nothing to do with VS Code or the Python extension and simply how applications work.

Probably the easiest way to keep the window open is add the following line at the very end:

input("Press any key to exit ...")

That way Python won't exit until you press a key in the terminal.

Upvotes: 9

Related Questions