2013-04-09 70 views
1

所以这里是我的问题,我不得不为我的CS类做一张照片,它真的令人沮丧估计龟。我计划使用.onclick()向我展示位置。Python 3.0使用turtle.onclick

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

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

乌龟文档似乎说,onclick将通过一个函数中的坐标接受两个变量。

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

注意:当我点击箭头它的工作原理,但是那不是我想要的。我想要点击屏幕上的其他位置来找出我应该发送箭头的坐标。

任何帮助,将不胜感激。

+0

这里有什么问题? – 2013-04-09 05:36:00

+0

这适用于我。你是否正确地与龟对接?你希望在哪里写'print'语句? – 2013-04-09 05:45:14

+0

好的,我测试了一下,并在上面添加了一个注释。我不想单击箭头。我想单击箭头所在的空白区域,以便我可以找出我想要发送箭头的坐标。 – Shaw 2013-04-09 16:37:16

回答

0

好吧,我想出了一个工作。它不是一个完美的解决方案,但它工作得很好。因为onclick只会在你点击箭头时做出响应,所以我让箭头包含整个屏幕。然后我把它藏起来了。你需要做的是将鼠标悬停在你想要去的位置上,按“a”,当它变成黑色时,点击屏幕。然后shell会显示你需要的坐标。确保你总是回到(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的学生,如果你使用这个,我建议不留痕迹。教授已经看到了这一点,并会认识到这一点。

0

非常棒的工作,找出你自己的解决方案。

您是否曾浏览过turtle的文档?

http://docs.python.org/2/library/turtle.html

貌似可以从模块导入screen以及turtlescreen有自己的onclick事件,可以达到您的预期效果。

说明如何获得访问screen对象以下行:

The function Screen() returns a singleton object of a TurtleScreen subclass. 
This function should be used when turtle is used as a standalone tool for 
doing graphics. As a singleton object, inheriting from its class is not 
possible. 

免责声明:我从来没有使用过乌龟。

+0

我看了一下。我无法弄清楚这是否奏效。它似乎没有做它说的那样。感谢您的尝试。 – Shaw 2013-04-11 03:26:30

1

您需要使用Screen类。但是,如果您想远离OOP,则可以使用内置方法turtle.onscreenclick(func)。

更换

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

def main(): 
    t.onscreenclick(getPos) 
    t.mainloop() 
main() 
0

你必须得到第一画面对象乌龟借鉴,然后调用屏幕对象的onclick()。这里是一个例子:

import turtle as t 

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

def main(): 
    s = t.getscreen() 
    s.onclick(getPos) 
    t.mainloop() 

main()