使用turtle.onclick的Python 3.0 [英] Python 3.0 using turtle.onclick

查看:428
本文介绍了使用turtle.onclick的Python 3.0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以这是我的问题,我必须为我的CS课做一张照片,这在使用Turtle进行估算时确实令人沮丧.我计划使用.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!

任何帮助将不胜感激.

Any help would be appreciated.

推荐答案

好的,我想出了一个解决方法.它不是一个完美的解决方案,但效果很好.因为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.

这篇关于使用turtle.onclick的Python 3.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆