Drawing with turtle(python) using PyCharm

Question:

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

  1. If it can be done via PyCharm
  2. What am I missing in terms of configuration

Cheers

Asked By: DanyC

||

Answers:

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.

Answered By: DanyC

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!

Answered By: Allan Anderson

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.

Answered By: phil3000

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()
Answered By: Anony

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()
Answered By: vivek manchikatla
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.

Answered By: Udai Verma
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.