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()
Related
I have used the onkey function for the right and left:
sc.onkey(user_left,"left")
sc.onkey(user_right,"right")
I have also set the screen after setting the turtle and importing turtle:
sc=Screen()
But when I use the same format for up and down:
sc.onkey(user_up,"up")
sc.onkey(user_down,"down")
It does nothing. I also have my functions:
def user_right:
t3.forward(5)
def user_left:
t3.backward(5)
t3 is my user turtle, and it is sideways, the shape is turtle, and its head is facing right. t3 is automatically set to make its head facing the right side when the code runs. By the way, I import from turtle import*
I see several issues here. First, this can't work as you claim:
def user_right:
t3.forward(5)
def user_left:
t3.backward(5)
It would need to be:
def user_right():
t3.forward(5)
def user_left():
t3.backward(5)
Next, this won't work in turtle in standard Python:
sc.onkey(user_left,"left")
sc.onkey(user_right,"right")
These keys would have to be "Left" and "Right". Are you using a non-standard turtle implementation? (E.g. the one on repl.it) You show your two event handlers that work, but fail to show the two that don't which would be of more interest when trying to debug your code.
Finally, your missing a call to the screen's listen() method, so your keystrokes are ignored. Here's how I might implement the functionality your code is hinting at:
from turtle import Screen, Turtle
def user_right():
turtle.forward(5)
def user_left():
turtle.backward(5)
def user_up():
turtle.sety(turtle.ycor() + 5)
def user_down():
turtle.sety(turtle.ycor() - 5)
turtle = Turtle()
turtle.shape('turtle')
screen = Screen()
screen.onkey(user_left, 'Left')
screen.onkey(user_right, 'Right')
screen.onkey(user_up, 'Up')
screen.onkey(user_down, 'Down')
screen.listen()
screen.mainloop()
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()
I have some code as follows:
# My code here
turtle.bye()
After that, is there any way I can reopen the turtle window.
I know you can do turtle.clearscreen() but that does not close the turtle window.
I will accept any answer which allows me to close the turtle graphics window and then reopen it without opening and running another python program to do this.
Thank you in advance
I've seen situations where the approach of #LukeTimmons worked but not always reliably and not in every situation. Give this solution a try:
import time
import turtle
turtle.dot(200, 'green')
time.sleep(2)
turtle.bye()
# These two lines (indirectly) resurrect turtle environment after turtle.bye()
turtle.Turtle._screen = None # force recreation of singleton Screen object
turtle.TurtleScreen._RUNNING = True # only set upon TurtleScreen() definition
turtle.dot(200, 'red')
turtle.mainloop()
It resets two flags that keep turtle from starting up again. It may be safer to create your own turtle after restart rather than use the default turtle which may point back to the departed environment.
There may be other ways but this is the only way I know.
from turtle import *
def turtle1():
#Your code here
turtle1()
turtle.bye()
turtle1()
This should re-run your code without re-typing it.
I am using the turtle module in python. the problem is that whenever I have the turtle move i will draw even if the pen is up. for example if I run this program:
import turtle
turtle.penup
turtle.goto(0,50)
the turtle will still draw a line when it moves to (0,50)
why is this and how can it be prevented?
It looks like you're not actually calling turtle.penup. Try this:
import turtle
turtle.penup()
turtle.goto(0,50)
You have a typo, you aren't calling the penup method:
import turtle
turtle.penup() #This needs to be a method call
turtle.goto(0,50)
import turtle
turtle.up()
turtle.goto(0,50)
turtle.down()
if you don't put the pen down it will keep on drawing in invisible condition.
This question is super old and definitely has already been answered, but I'll leave this explanation here for future people
"penup" is a method in Python, aka function in other languages. This means that when you want to use it you have it include some parenthesis just so that your code knows what is supposed to be happening
import turtle
turtle.penup()
turtle.goto(0,50)
When you don't include the parenthesis, the code thinks you are talking about a variable, and looks for one called "penup", but there is no variable of that name, so Python throws its hands up and crashes
you called penup without (). with
turtle.penup()
this will work.
Others here said that, but implicitly. trying to ensure it is clear where the typo is.
You should probably try,
turtle.penup()
no it should be something like this:
turtle.up() # This a method call
turtle.goto(0,50) # Part of the method call