简体   繁体   中英

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!

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.

The easiest solution is to add the following line in your VS Code:-

turtle.done()

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

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()

do you know some extension in VSC for turtle module? i ve done a project for some contest in Idle 3.8 32 bit and im trying to make it work in VSC cuz i find VSC better in terms of orginasing projects. anyway, please send a solution ASAP

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM