python中的海龟-试图让海龟移动到鼠标点击位置并打印其坐标 [英] Turtle in python- Trying to get the turtle to move to the mouse click position and print its coordinates

查看:579
本文介绍了python中的海龟-试图让海龟移动到鼠标点击位置并打印其坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过 Python 乌龟获取鼠标位置.一切正常,只是我无法让乌龟跳到鼠标点击的位置.

导入海龟def startmap(): #接下来的方法是绘制地图screen.bgcolor("#101010")screen.title("欢迎,指挥官.")screen.setup(1000,600,1,-1)screen.setworldcoordinates(0,600,1000,0)drawcontinents() #draws 一堆东西,按它应该的方式工作,但对这个问题并不重要龟.pu()乌龟.onclick(乌龟.goto)打印(turtle.xcor(),turtle.ycor())屏幕.听()

据我所知,turtle.onclick(turtle.goto)"行应该将海龟发送到我单击鼠标的任何位置,但事实并非如此.打印行是一个测试,但它只返回我最后发送乌龟的位置,名义上是 (0, 650) 尽管这没有重大意义.

我尝试在 pydoc 中查找教程,但到目前为止我还没有成功写出这个.

感谢您的帮助.谢谢.

我需要乌龟去点击位置(完成)但我也需要它来打印坐标.

解决方案

您正在寻找 onscreenclick().它是TurtleScreen 的一个方法.onclick() 方法Turtle 指的是鼠标点击海龟本身.令人困惑的是,TurtleScreenonclick() 方法与其 onscreenclick() 方法是一样的.

<块引用>

24.5.4.3.使用屏幕事件

turtle.onclick(有趣btn=1add=None)
turtle.onscreenclick(有趣btn=1add=None)

参数:

  • fun – 一个带有两个参数的函数,将使用画布上点击点的坐标进行调用
  • num – 鼠标按钮的编号,默认为 1(鼠标左键)
  • addTrueFalse – 如果 True,会添加一个新的绑定,否则会替换之前的绑定

fun 绑定到此屏幕上的鼠标点击事件.如果 funNone,则删除现有绑定.

名为screen 的TurtleScreen 实例和名为turtle 的Turtle 实例的示例:

<预><代码>>>>screen.onclick(turtle.goto) # 随后点击进入 TurtleScreen 将>>># 让海龟移动到点击点.>>>screen.onclick(None) # 再次移除事件绑定

<块引用>

注意:此 TurtleScreen 方法仅在名称 onscreenclick 下可用作全局函数.全局函数 onclick 是从 Turtle 方法 onclick 派生的另一个函数.

快速切入...

所以,只调用screen 的方法而不是turtle.只需将其更改为:

screen.onscreenclick(turtle.goto)

如果您输入了 turtle.onclick(lambda x, y: fd(100))(或类似的东西),您可能会在点击乌龟时看到它向前移动.使用 goto 作为 fun 参数,你会看到海龟去......它自己的位置.

每次移动都打印

如果你想在每次移动时打印,你应该定义你自己的函数,它会这样做,并告诉乌龟去某个地方.我认为这会起作用,因为 turtle 是一个单例.

def gotoandprint(x, y):gotoresult = 龟.goto(x, y)打印(turtle.xcor(),turtle.ycor())返回 gotoresultscreen.onscreenclick(gotoandprint)

如果 turtle.goto() 返回 None(我不知道),那么您实际上可以这样做:

screen.onscreenclick(lambda x, y: turtle.goto(x, y) 或 print(turtle.xcor(), turtle.ycor())

让我知道这是否有效.我的电脑上没有 tk,所以无法测试.

I'm trying to get the mouse position through Python turtle. Everything works except that I cannot get the turtle to jump to the position of the mouse click.

import turtle

def startmap(): #the next methods pertain to drawing the map
   screen.bgcolor("#101010")
   screen.title("Welcome, Commadore.")
   screen.setup(1000,600,1,-1)
   screen.setworldcoordinates(0,600,1000,0)
   drawcontinents()    #draws a bunch of stuff, works as it should but not really important to the question
   turtle.pu()
   turtle.onclick(turtle.goto)
   print(turtle.xcor(),turtle.ycor())
   screen.listen()

As I understand, the line that says 'turtle.onclick(turtle.goto)' should send the turtle to wherever I click the mouse, but it does not. The print line is a test, but it only ever returns the position that I sent the turtle last, nominally (0, 650) although this does not have major significance.

I tried looking up tutorials and in the pydoc, but so far I have not been able to write this successfully.

I appreciate your help. Thank you.

Edit: I need the turtle to go to the click position(done) but I also need it to print the coordinates.

解决方案

You are looking for onscreenclick(). It is a method of TurtleScreen. The onclick() method of a Turtle refers to mouse clicks on the turtle itself. Confusingly, the onclick() method of TurtleScreen is the same thing as its onscreenclick() method.

24.5.4.3. Using screen events

turtle.onclick(fun, btn=1, add=None)
turtle.onscreenclick(fun, btn=1, add=None)

Parameters:

  • fun – a function with two arguments which will be called with the coordinates of the clicked point on the canvas
  • num – number of the mouse-button, defaults to 1 (left mouse button)
  • addTrue or False – if True, a new binding will be added, otherwise it will replace a former binding

Bind fun to mouse-click events on this screen. If fun is None, existing bindings are removed.

Example for a TurtleScreen instance named screen and a Turtle instance named turtle:

>>> screen.onclick(turtle.goto) # Subsequently clicking into the TurtleScreen will
>>>                             # make the turtle move to the clicked point.
>>> screen.onclick(None)        # remove event binding again

Note: This TurtleScreen method is available as a global function only under the name onscreenclick. The global function onclick is another one derived from the Turtle method onclick.

Cutting to the quick...

So, just invoke the method of screen and not turtle. It is as simple as changing it to:

screen.onscreenclick(turtle.goto)

If you had typed turtle.onclick(lambda x, y: fd(100)) (or something like that) you would probably have seen the turtle move forward when you clicked on it. With goto as the fun argument, you would see the turtle go to... its own location.

Printing every time you move

If you want to print every time you move, you should define your own function which will do that as well as tell the turtle to go somewhere. I think this will work because turtle is a singleton.

def gotoandprint(x, y):
    gotoresult = turtle.goto(x, y)
    print(turtle.xcor(), turtle.ycor())
    return gotoresult

screen.onscreenclick(gotoandprint)

If turtle.goto() returns None (I wouldn't know), then you can actually do this:

screen.onscreenclick(lambda x, y: turtle.goto(x, y) or print(turtle.xcor(), turtle.ycor())

Let me know if this works. I don't have tk on my computer so I can't test this.

这篇关于python中的海龟-试图让海龟移动到鼠标点击位置并打印其坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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