Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 200
DanyC Avatar asked Nov 04 '14 14:11

DanyC


People also ask

How do I run a Python turtle in PyCharm?

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.

Does PyCharm have turtle?

To try it yourself, follow the PyCharm installation instructions, then type some turtle graphics code, as in the example above. Finally, from the Run menu, choose Start Live Turtle. You should see a preview of your turtle graphics.


2 Answers

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!

like image 61
Allan Anderson Avatar answered Oct 17 '22 10:10

Allan Anderson


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

like image 3
DanyC Avatar answered Oct 17 '22 10:10

DanyC