I'm working on a python project and I'm using VS Code.
Any python file I write and include turtle
from turtle import *
up()
goto(-200, k)
down()
I get errors like:
Undefined variable 'goto'
Undefined variable 'down'
Undefined variable 'forward'
Undefined variable 'left'
I get an error like this for every time I use a turtle element (I got like 100+ total)
The code does work so this isn't crucial, but it just bothers me a bit cause it's just there, you know.
Try,
from turtle import *
t = Turtle()
t.up()
# ... etc.
You need to create a instance.
Related
I wanted to make a blinking "Press SPACE to start!" screen and if SPACE was pressed, the program should shut down but I always get the turtle.terminator error. Can someone help me please?
from turtle import *
import pygame
while True:
x = 0
penup()
tracer(0)
if x == 0:
color("black")
goto(-80,-170)
write("Press SPACE to start!",font=50)
update()
pygame.time.delay(1*800)
x = 1
if x == 1:
color("white")
goto(-90,-180)
begin_fill()
goto(90,-180)
goto(90,-140)
goto(-90,-140)
goto(-90,-180)
end_fill()
update()
x = 0
pygame.time.delay(1*800)
def close():
bye()
onkey(close(),"Space")
done()
After integrating advice from the comments, it still raises an error when I use onkey(close,...) instead of onkey(close(),..).
It is "space", not "Space". Also, it is still correct to use onkey(close, ...).
The complete function would be onkey(close, "space")
There are a few issues here. A word of advice: work in small bursts and run your code often to validate all of your assumptions at each step of the way. This code looks as if it was all written in one fell swoop, and once it didn't work, there was too much complexity to isolate the bugs. Had you run the code to test it often as you went along, you'd see those errors immediately and the solutions would be more obvious.
Try to minimize the problem space by breaking your functionality into small pieces and validating each one.
First, let me provide my solution:
import turtle
def render_white_screen():
win.ontimer(render_press_space, delay_ms)
t.clear()
turtle.update()
def render_press_space():
win.ontimer(render_white_screen, delay_ms)
t.write(
"Press SPACE to start!",
move=False,
align="center",
font=("Arial", 20, "normal"),
)
turtle.update()
delay_ms = 800
turtle.tracer(0)
win = turtle.Screen()
win.onkey(turtle.bye, "space")
win.listen()
t = turtle.Turtle()
t.hideturtle()
t.color("black")
render_press_space()
turtle.mainloop()
Turtle is surprisingly not especially beginner-friendly and has a huge list of gotchas, so it's important to check the docs and do research on Stack Overflow as soon as you encounter an error.
Errors:
turtle.onkey(some_func(), "some key") is incorrect (assuming some_func isn't a higher-order function) because you're calling some_func rather than passing it as the handler. This should be turtle.onkey(some_func, "some key"). This happens to trigger the terminator error because terminator occurs when you run turtle code after exiting, which is what your handler does. See Python Turtle.Terminator even after using exitonclick() and onkeypress( ) function in turtle module problem.
turtle.onkey(space_handler, "Space") should be turtle.onkey(space_handler, "space") (lowercase). See Couldn't get rid of (_tkinter.TclError: bad event type or keysym "UP") problem.
You must call turtle.listen() to activate key handlers that you've registered. See Turtle.onkeypress not working (Python).
Suggestions/better practices:
Always use import turtle, never from turtle import *. The reason is that from turtle import * dumps 160+ functions into the global namespace, causing potential clashes with your own functions. Many of these functions have common names like update and reset that can easily cause confusion and bugs. Turtle has a module-level instance ostensibly to avoid confusion for beginners, but it only winds up introducing more problems in the long run than simply creating a turtle instance.
Don't import pygame just to sleep. Python has a native function sleep available in the time module. But even better is to use turtle's ontimer callback as shown above.
You can clear the screen with t.clear().
from turtle import *
s = turtle()
why I am not getting any output? Error is occurring, saying turtle is not defined? It's python 3.7
s = turtle() statement is meant to create object of class turtle. But in module turtle there is no class by this name. That's why you're getting error NameError: name 'turtle' is not defined.
Turtle module is meant for simple drawings. You don't need to create any object (of any class) to start. Just call any drawing function straight away (and read module docs), for example:
from turtle import *
circle(50)
done()
More complex example taken from module docs:
from turtle import *
color('red', 'yellow')
begin_fill()
while True:
forward(200)
left(170)
if abs(pos()) < 1:
break
end_fill()
done()
will visualize this picture:
I'm new to the Python turtle library and I have a problem that confuses me a lot.
I can work with turtle in real time, but when I write a program and I save it to a file, I can't run it.
The code I have written follows below:
from turtle import *
turtle.Pen(9999999)
penup()
for i in range(16):
write(i,align='center')
forward(25)
goto(0,-5)
x=0
right(90)
for i in range(16):
pendown()
forward(400)
penup()
x+=25
goto(x,-5)
but it didn't work at all.
It gave me this error:
Traceback (most recent call last):
File "C:\Users\Nobody\Desktop\main.py", line 3, in <module>
turtle.Pen(9999999)
NameError: name 'turtle' is not defined
I think it doesn't import turtle at all.
You named your own file turtle.py
So your main.py is importing your own turtle.py, not python's turtle module.
Delete turtle.py from your desktop (and the turtle.pyc that was automatically generated).
from turtle import *
this line imports everything to the default module namespace, so you don't have to add turtle. prefix to anything
Instead of turtle.Pen you want just Pen
from turtle import *
Means literally: import all from the file turtle.py.
Python interpreter firstly checks in the current directory for any match with turtle.py and if not found anything, it will search inside library folders.
In your case, you are importing all classes, all functions and all global variables (at least not private ones) from turtle.py so you need to use
Pen(9999999)
Instead of
turtle.Pen(9999999)
Here's a rework of your code that runs fine for me:
from turtle import *
speed('fastest')
penup()
for i in range(16):
write(i, align='center')
forward(25)
goto(0, -5)
right(90)
x = 0
for i in range(16):
pendown()
forward(400)
penup()
x += 25
goto(x, -5)
hideturtle()
done()
If it runs for you, great. If it doesn't, let us know the complete error you're getting as a comment follow on to this answer. Make sure you don't have a personal turtle.py file, as #nosklo notes.
My advice is when it comes to invoking library functions, it's better to lookup rather than make up.
I'm trying to use Turtle, but for some reason, it won't let me import it. I've tried from turtle import * (and that works) but if I try print(dir(turtle)) or to use any functions, I get an error saying turtle is not defined.
from turtle import Turtle doesn't work but print(dir(Turtle)) after using from turtle import * does work. However, prefixing commands with Turtle. ie Turtle.color("red") doesn't work.
Also, the demonstrations in the turtledemo folder work. I'd really appreciate any help
This imports turtle into the main namespace and is used as follows:
import turtle
turtle.something()
Thus, you now have an identifier in your main namespace, named turtle.
This imports all visible identifiers from turtle into the main namespace
and is used differently:
from turtle import *
something()
In this scenario, turtle is not in the main namespace. Its contents
are. Thus, dir(turtle) will fail, because that identifier isn't there.
Are you sure that from turtle import Turtle doesn't work? It worked
for me.
If you import all from turtle then you have to use the Turtle object directly, like this:
from turtle import *
print(dir(Turtle))
and not like this:
from turtle import *
print(dir(turtle))
I have two pictures "gif" and want to insert them as small pictures and add them to Python code. I want the picture to stay in an accurate location as well decrease it size. When i try to run this code the shell shows an error "screen isn't defined"
import turtle
import time
from tkinter import *
screen=turtle.Turtle()
screen=turtle.getscreen()
screen.register_shape("health.gif")
screen.penup()
screen.shape("health.gif")
screen.goto(x+50,y+150)
I don't know why you're getting that error... but you have a mixup with your variable names... you create a variable you call screen that is turtle.Turtle() but then you overwrite that variable by doing turtle.getscreen()
Doing:
import turtle
t=turtle.Turtle()
screen=t.getscreen()
screen.register_shape("health.gif")
t.penup()
t.shape("health.gif")
Draws a health.gif image (if that happens to be in your working directory)