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!
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With