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()
Related
import turtle
t = turtle.Turtle()
t.forward(100)
t.done()
this code is returning an error like this
NameError: name 'turtle' is not defined
I have tried to use turtle but it doesn't seem to be working the way it is shown in the code.
I have tried referring to various sources but could not find the fix
The problem lies on this line:
t.done()
You see, you defined t as a Turtle object, and Turtle objects have no attribute done.
The done() attribute belongs to the turtle module itself, so instead of t.done(), it's turtle.done():
import turtle
t = turtle.Turtle()
t.forward(100)
turtle.done()
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()
Why does the screen with turtle graphic close down by itself?
import turtle
circle = turtle.Turtle()
circle.color("pink")
circle.pensize(5)
circle.speed(5)
circle.circle(10)
circle.done()
Instead of circle.done(), put turtle.done()
The turtle window closed by itself because your program ran into an error:
AttributeError: 'Turtle' object has no attribute 'done'
Use turtle.done() instead:
import turtle
circle = turtle.Turtle()
circle.color("pink")
circle.pensize(5)
circle.speed(5)
circle.circle(10)
turtle.done()
See the documentation here.
I am trying to write a program to draw a flower, but no matter what I do it keeps throwing an "invalid syntax" error for the turtle name. I have taken out all of my other code, tried naming the turtle something different, yet nothing works. Any ideas?
import turtle
def draw_flower():
window = turtle.Screen()
window.bgcolor(#42dff4)
sam = turtle.Turtle()
sam.forward(50)
window.exitonclick()
draw_flower()
Besides quoting the color string, as noted in the comments, your lines of code are in the wrong order. For example, generally nothing should follow window.exitonclick():
window.exitonclick()
draw_flower()
Make it (or window.mainloop()) the last statement of your program as that's when your code ends and the Tk event handler loop begins. I.e. reverse the order of these two statements. The second problem is that the variable window is in the wrong scope:
def draw_flower():
window = turtle.Screen()
...
window.exitonclick()
Since it's defined locally in draw_flower(), it's not available to use globally. Here's a rework of your code addressing both issues:
import turtle
def draw_flower():
sam = turtle.Turtle()
sam.forward(50)
window = turtle.Screen()
window.bgcolor("#42dff4")
draw_flower()
window.exitonclick()
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