【发布时间】:2020-10-22 15:56:11
【问题描述】:
我试图将海龟移动到我的鼠标点击而不是打印 x 和 y。
import turtle
tim = turtle.Turtle()
tim.shape('turtle')
screen = tim.getscreen()
def getPos():
# this should move turtle to mouse click and print coords
screen.onscreenclick(tim.goto)
print(tim.xcor(), tim.ycor())
def main():
# once I run, it prints coords but once I move turtle it does not
getPos()
screen.mainloop()
turtle.bye()
main()
一旦我运行我的程序,它就会打印 x 和 y。但是,一旦我用鼠标单击移动海龟,它就不会打印坐标。
【问题讨论】:
-
您只调用一次
getPos。为什么打印会不止一次? -
您是否期望其他东西会调用
getPos()? -
这就是我感到困惑的原因。我的印象是 getPos() 因为 screen.mainloop() 而被多次调用。即使我只调用了一次,为什么 screen.oncscreenclick(tim.goto) 仍然有效?
标签: python turtle-graphics python-turtle