如何让我的函数返回我的乌龟的x坐标?

时间:2015-11-01 04:01:38

标签: python turtle-graphics python-3.5

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)

1 个答案:

答案 0 :(得分:0)

当你打电话给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()