2013-12-23 32 views

回答

-1

按照turtle docs你可以使用:

turtle.position() 

哦,等等 - 你问对于鼠标的位置... ...研究

貌似closest thing is

turtle.onscreenclick() 

这显然只在你点击鼠标按钮时才起作用。

0

我们可以进入乌龟的Tk基础,以启用'<Motion>'事件。我投的功能设置/取消该事件看起来像乌龟屏幕的方法,但你可以调用它的奇异画面实例turtle.Screen()

import turtle 

def onmove(self, fun, add=None): 
    """ 
    Bind fun to mouse-motion event on screen. 

    Arguments: 
    self -- the singular screen instance 
    fun -- a function with two arguments, the coordinates 
     of the mouse cursor on the canvas. 

    Example: 

    >>> onmove(turtle.Screen(), lambda x, y: print(x, y)) 
    >>> # Subsequently moving the cursor on the screen will 
    >>> # print the cursor position to the console 
    >>> screen.onmove(None) 
    """ 

    if fun is None: 
     self.cv.unbind('<Motion>') 
    else: 
     def eventfun(event): 
      fun(self.cv.canvasx(event.x)/self.xscale, -self.cv.canvasy(event.y)/self.yscale) 
     self.cv.bind('<Motion>', eventfun, add) 

def goto_handler(x, y): 
    onmove(turtle.Screen(), None) # avoid overlapping events 
    turtle.setheading(turtle.towards(x, y)) 
    turtle.goto(x, y) 
    onmove(turtle.Screen(), goto_handler) 

turtle.shape('turtle') 

onmove(turtle.Screen(), goto_handler) 

turtle.mainloop() 

我的代码包括例如运动事件处理程序,使龟像追逐激​​光指示器的猫一样跟随光标。没有必要的点击(除了最初的点击使窗口激活)。:

enter image description here

相关问题