Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Turtle Graphic Window not working from VS Code

Tags:

python

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!

like image 377
otc09 Avatar asked Dec 13 '22 10:12

otc09


2 Answers

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.

like image 90
Brett Cannon Avatar answered Dec 27 '22 00:12

Brett Cannon


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.

like image 40
Riddhi Narkar Avatar answered Dec 27 '22 01:12

Riddhi Narkar