Python 3.0 使用 turtle.onclick

问题描述

所以这是我的问题,我必须为我的 CS 课制作一张照片,在海龟中进行估计真的很令人沮丧.我打算使用 .onclick() 来显示我的位置.

So here is my problem, I have to make a picture for my CS class and it is really frustrating estimating in turtle. I planed to use .onclick() to show me to position.

import turtle as t
def getPos(x,y):
    print("(", x, "," ,y,")")
    return

def main():
    t.onclick(getPos)
    t.mainloop()
main()

海龟文档似乎说 onclick 将在一个接受两个变量的函数中传递坐标.

The turtle documentation seems to say that the onclick will pass the coordinates in a function that takes in two variables.

http://docs.python.org/3.1/library/turtle.html#turtle.onclick

注意:当我单击箭头时它会起作用,但这不是我想要的.我想点击屏幕上的其他位置来找出我应该将箭头发送到哪个坐标!

NOTE: It works when I click on the arrow head, but thats not what I want. I want to click some other position on the screen to find out what coordinates I should send the arrow head to!

任何帮助将不胜感激.


解决方案

好吧,我想出了一个解决办法.它不是一个完美的解决方案,但效果很好.因为只有当你点击箭头时 onclick 才会响应,所以我让箭头包含了整个屏幕.然后我把它藏了起来.你需要做的是将鼠标悬停在你想去的位置上,按a",当它变黑时点击屏幕.然后外壳将显示您需要的坐标.确保你总是回到 (1000,0).

Alright, I figured out a work around. Its not a perfect solution but it works pretty well. Because onclick will only respond if you click on the arrow head, I made the arrow head encompass the entire screen. Then I hid it. What you need to do is hover over the position you want to go to, press "a" and when it goes black click the screen. The shell will then display the coordinates you need. Make sure you always go back to (1000,0).

import turtle as t

def showTurtle():
    t.st()
    return

def getPos(x,y):
    print("(", x, "," ,y,")")
    return

def hideTurtle(x,y):
    t.ht()
    return

def main():
    t.speed(20)
    t.shapesize(1000,1000)
    t.up()
    t.goto(1000,0)
    t.ht()
    t.onkey(showTurtle,"a")
    t.listen()
    t.onclick(getPos)
    t.onrelease(hideTurtle)
    t.mainloop()
main()

另外,如果我班上的任何人发现这个,我是宾厄姆顿的一名 CS 学生,如果你使用这个,我建议不要留下任何痕迹.教授已经看到了这一点,并且会认出它.

Also, in case anyone from my class finds this, I am a CS student in binghamton and if you use this I recommend leaving no trace. The prof has seen this and will recognize it.

相关文章