Reputation: 109
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
Cheers
Upvotes: 7
Views: 47298
Reputation: 109
I fixed the problem like so:
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()
The only downside is that it does close the canvas once it finished.
Upvotes: 2
Reputation: 1
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.
Upvotes: 0
Reputation: 1
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()
Upvotes: -1
Reputation: 63
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()
Upvotes: 2
Reputation: 17
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.
Upvotes: 0
Reputation: 583
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!
Upvotes: 15