import turtle
tess = turtle.Turtle()
wn = turtle.Screen()
def hello3 (x,y):
tess.goto(x, y)
return tess.xcor()
我希望我的函数 hello3 在我单击屏幕上的某个位置后返回我的乌龟的 x 坐标,但它似乎一直返回 None。
turtle.onscreenclick(hello3, btn=1)
import turtle
tess = turtle.Turtle()
wn = turtle.Screen()
def hello3 (x,y):
tess.goto(x, y)
return tess.xcor()
我希望我的函数 hello3 在我单击屏幕上的某个位置后返回我的乌龟的 x 坐标,但它似乎一直返回 None。
turtle.onscreenclick(hello3, btn=1)
当您调用turtle.onscreenclick(hello3, btn=1) 时,它会将'hello3' 函数绑定到屏幕点击事件。但是,它不会立即调用 'hello3' 函数。当有屏幕点击事件时,程序会调用'hello3'函数。因此,turtle.onscreenclick() 返回 None,因为它将函数连接到事件但不调用该函数。试试下面的代码:
import turtle
tess = turtle.Turtle()
wn = turtle.Screen()
def hello3(x,y):
tess.goto(x, y)
# When you click the screen, it will print out its x-coordinate.
print tess.xcor()
# You cannot receive this return value, if you put this function
# as a call-back function (or completion handler).
return tess.xcor()
returnValue = turtle.onscreenclick(hello3, btn=1)
print type(returnValue)
turtle.done()