Python output window disappears immediately in eclipse after run - python

I use eclipse IDE and pydev plugins for python development. In eclipse when I run the following code snippet the program output window appears and disappears immediately. How to solve this when using ecliple (not using by terminal or command prompt)?
import turtle
wn = turtle.Screen()
alex = turtle.Turtle()
alex.forward(50)
alex.left(90)
alex.forward(30)
wn.mainloop()

That is because you get an error, it has nothing to do with eclipse:
AttributeError: '_Screen' object has no attribute 'mainloop'
If you want a continuous loop use a while loop or a for loop:
import turtle
while True:
wn = turtle.Screen()
alex = turtle.Turtle()
alex.forward(50)
alex.left(90)
alex.forward(30)
If you want to just see the keep the screen up use exitonclick()
wn.exitonclick()
the docs

Related

Python turtle terminator

I just wrote a program based on turtle module and after last line of code I used turtle.done() but when again I want to use my turtle I will get Terminator error and I should close and again open jupyter or even commond line to be able to run my code again please tell me what's wrong? Is it a bug inside turtle?
my code is something like this:
my_screen = turtle.Screen()
my_turtle= turtle.Turtle()
wm.setup(200, 300)
....
Here is my code(quite long!)
....
turtle.done()
The error occurs when a turtle program is interrupted during its main loop. If the way you close your turtle program is by clicking the X and there's a while loop that never ends, then the turtle.done() statement will never be met.
You can detect a turtle.Terminator error inside your while loop, so that instead of crashing the program, let the program continue to parse outside the while loop.
Something like this:
my_screen = turtle.Screen()
my_turtle= turtle.Turtle()
wm.setup(200, 300)
while True:
try:
# Your code for the while loop
except turtle.Terminator:
break
turtle.done
Or use ontimer().
The turtle module uses a class variable _RUNNING which remains true between executions when running in jupyter instead of running it as a self contained script. I have requested for the module to be updated with a pull request.
Meanwhile, work around/working example
1)
import importlib
import turtle
importlib.reload(turtle)
my_screen = turtle.Screen()
my_turtle= turtle.Turtle()
wm.setup(200, 300)
....
Your code
....
turtle.done()
import turtle
my_screen = turtle.Screen()
turtle.TurtleScreen._RUNNING=True
my_turtle= turtle.Turtle()
wm.setup(200, 300)
....
Your code
....
turtle.done()

Python Turtle Crashes When Opening Second Window

I have a small program where I would like to pop up a Python turtle window, display something, close on user click, then pop up a next window. An example program for this is
import turtle
window = turtle.Screen()
t1 = turtle.Turtle()
t1.forward(100)
window.exitonclick()
window2 = turtle.Screen()
t2 = turtle.Turtle()
t2.forward(100)
window2.exitonclick()
The program crashes when trying to open the second window. This is with Python3.6 on a Mac OS X machine. I have seen some reference to crashes when re-running a program twice, and seen advice about adding a bye() call after the first window close, but I haven't managed to work around this yet.
Any suggestions?
It seems like you should probably using a different function then exitonclick to exit out of the screen or clear it. But I was able to get your code working by running turtle.bye() and doing a try/except around it in case of errors.
It's definitely not an elegant solution but is a workaround to get the second window to appear properly.
import turtle
window = turtle.Screen()
t1 = turtle.Turtle()
t1.forward(100)
window.exitonclick()
try:
turtle.bye()
except Exception:
pass
window2 = turtle.Screen()
t2 = turtle.Turtle()
t2.forward(100)
window2.exitonclick()

python turtle graphics window won't open

I have little piece of code from a tutorial which should work fine but I don't get the turtle graphics window to show (I'm on Windows 10 using python 2.7.10). The code looks like this
import turtle
def draw_square():
window = turtle.Screen()
window.bgcolor("red")
brad = turtle.Turtle()
brad.forward(100)
window.exitonclick()
However, when I execute it nothing happens, I don't even get an error message. Instead, the shell just says
================================ RESTART ================================
and displays the little Windows circle (indicating it is working on something) but the turtle graphics window does not pop up.
I have tried repairing my python installation and additionally installing the x86 version, but I get the same outcome on the other installation, too.
Does anyone please know how to fix this?
Thank you,
Tomislav
Functions don't do anything unless you call them. Try:
import turtle
def draw_square():
window = turtle.Screen()
window.bgcolor("red")
brad = turtle.Turtle()
brad.forward(100)
window.exitonclick()
draw_square()

mainloop function from turtle class is not working, Python

This is my code :
import turtle
wn = turtle.Screen()
alex = turtle.Turtle()
alex.forward(50)
alex.left(90)
alex.forward(30)
wn.mainloop()
And my error is: "_Screen object has no attribute mainloop"
I tried to do just "mainloop" but then I got NameError while doing alex.mainloop gave same error i.e. turtle object has no attribute mainloop.
Googling told me that I might have my file name as turtle.py creating the conflict but that's not the case.
You just need to call the mainloop() function on the turtle module. So your last line should be:
turtle.mainloop()

Object oriented programming turtle (python)

I'm trying to get two turtles on a screen. So far I have this:
import turtle
t1 = turtle.Turtle()
t2 = turtle.Turtle()
t1.color("red")
t2.color("blue")
t1.forward(20)
t2.right(90)
t2.forward(100)
But nothing is happening :(
Im using the Turtle IDE downloaded from:http://pythonturtle.org/
All i want to do is illustrate some object orientation but if that code wont work, i cant
Anyone got any suggestions (ive tried using turtles API but its hard to follow)
Thanks
You didn't take an object from the class Screen
You should do that to make sure that the window will not close immediately, plus you should make the program loop.
import turtle
wn = turtle.Screen() # taking window object from Screen class
t1 = turtle.Turtle()
t2 = turtle.Turtle()
t1.color("red")
t2.color("blue")
t1.forward(20)
t2.right(90)
t2.forward(100)
while True: # Making the program loop to make the program continue running.
wn.update()
The main loop also you can make it this way
wn.mainloop()
I recommend you follow the first solution.

Categories