Reputation: 35
import turtle
import random
wn = turtle.Screen() #sets the screen
wn.setup(1000,900)
wn.screensize(2000,2000)
ad = turtle.Turtle() #names the turtle
ad.shape("circle") #changes turtles or "ad's" shape
ad.speed("fastest")
r = int(60) #CHANGES THE SIZE OF THE WRITING
x = int(-950)
y = int(200)
ad.penup()
ad.goto(x,y)
def enter():
ad.penup()
y -= 100
ad.goto(x,y)
wn.onkey(lambda: enter(), "Return")
wn.listen()
Trying to do an enter button in turtle but this wont work. It says that there is an error with the local variable.
Upvotes: 0
Views: 64
Reputation: 41905
Although your immediate problem is the lack of a global y
statement in your enter()
function, there's a lot of noise in your code that we should eliminate to make it a better MVCE:
import random # not used so leave out of SO question example
r = int(60) # ditto
x = int(-950) # int() not needed for ints
y = int(200) # ditto
wn.onkey(lambda: enter(), "Return") # lambda not needed
Although we could fix this with the addition of a global y
statement, I prefer to simply interrogate the turtle itself and avoid the global:
from turtle import Turtle, Screen
def enter():
ad.sety(ad.ycor() - 100)
X, Y = -950, 200
wn = Screen()
wn.setup(1000, 1000) # visible field
wn.screensize(2000, 2000) # total field
ad = Turtle("circle")
ad.speed("fastest")
ad.penup()
ad.goto(X, Y)
wn.onkey(enter, "Return")
wn.listen()
wn.mainloop()
Note, you positioned the turtle off the visible portion of the screen so you need to scroll to the left side of the window to see the turtle cursor. Once you do, you can move it down by hitting, "Return".
Upvotes: 1