问题
I'm running the latest PyCharm Pro version and trying to run the below code from a scratch file but it doesn't seem to work
import turtle
wn = turtle.Screen()
alex = turtle.Turtle()
alex.forward(150)
alex.left(90)
alex.forward(75)
By not working I mean, no window is popping out however I do see in the output saying
Process finished with exit code 0
Any idea
- If it can be done via PyCharm
- What am I missing in terms of configuration
Cheers
回答1:
I ran into the same problem. Turns out the solution is in the "turtle" module.
You want to end with
turtle.done()
or
turtle.exitonclick()
Enjoy!
回答2:
I managed to find out a way of doing it by using the below code.
The only downside is that it does close the canvas once it finished.
def main():
wn = turtle.Screen() # creates a graphics window
alex = turtle.Turtle() # create a turtle named alex
alex.forward(150) # tell alex to move forward by 150 units
alex.left(90) # turn by 90 degrees
alex.forward(75) # complete the second side of a rectangle
if __name__ == "__main__":
main()
If anyone has a different idea on how to not close the canvas that will be awesome.
Thanks,
Dani
回答3:
As Allan Anderson says, easiest way I found (as I don't use main that often):
turtle.exitonclick()
As the last line of code forces the Graphics Window to stay open until it is clicked.
回答4:
def main():
import turtle
wn = turtle.Screen() # creates a graphics window
alex = turtle.Turtle() # create a turtle named alex
alex.forward(150) # tell alex to move forward by 150 units
alex.left(90) # turn by 90 degrees
alex.forward(75) # complete the second side of a rectangle
if __name__ == "__main__":
main()
input("Press RETURN to close. ")
The last line will hold display till RETURN key is not pressed.
回答5:
By "no window is popping out" means that the program is executing and then directly closing. To fix it you have to loop the program as follows:
import turtle
wn = turtle.Screen()
alex = turtle.Turtle()
alex.forward(150)
alex.left(90)
alex.forward(75)
wn.mainloop()
回答6:
Use the below code. You are missing the function which keep the screen alive until it is closed by the user. exitonclick() Method helps to keep screen alive.
import turtle
wn = turtle.Screen()
alex = turtle.Turtle()
alex.forward(150)
alex.left(90)
alex.forward(75)
wn.exitonclick()
来源:https://stackoverflow.com/questions/26737366/drawing-with-turtlepython-using-pycharm