DanyC
DanyC

Reputation: 109

Drawing with turtle(python) using PyCharm

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

Upvotes: 7

Views: 47298

Answers (6)

DanyC
DanyC

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

Udai Verma
Udai Verma

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

vivek manchikatla
vivek manchikatla

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

Anony
Anony

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

phil3000
phil3000

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

Allan Anderson
Allan Anderson

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

Related Questions