6

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

DanyC
  • 99
  • 1
  • 1
  • 10

6 Answers6

15

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!

Allan Anderson
  • 574
  • 4
  • 15
2

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.

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
DanyC
  • 99
  • 1
  • 1
  • 10
2

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()
Anony
  • 63
  • 6
0

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.

phil3000
  • 19
  • 3
0
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.

luator
  • 4,769
  • 3
  • 30
  • 51
-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()