python中的海龟-试图让海龟移动到鼠标点击位置并打印其坐标

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17864085/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 09:20:36  来源:igfitidea点击:

Turtle in python- Trying to get the turtle to move to the mouse click position and print its coordinates

pythonpython-3.xturtle-graphics

提问by JFA

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.

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

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.

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

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

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

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.

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

采纳答案by 2rs2ts

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

您正在寻找onscreenclick(). 它是 的一种方法TurtleScreen。a 的onclick()方法是Turtle指鼠标点击海龟本身。令人困惑的是,的onclick()方法TurtleScreen与它的onscreenclick()方法是一回事。

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)
  • addTrueor False– if True, a new binding will be added, otherwise it will replace a former binding

Bind funto mouse-click events on this screen. If funis None, existing bindings are removed.

Example for a TurtleScreen instance named screenand a Turtle instance named turtle:

24.5.4.3. 使用屏幕事件?

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

参数:

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

乐趣绑定到此屏幕上的鼠标单击事件。如果funNone,则删除现有绑定。

名为 TurtleScreen 实例screen和名为 turtle 的 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 onclickis another one derived from the Turtle method onclick.

注意:此 TurtleScreen 方法仅在名称下可用作全局函数onscreenclick。全局函数onclick是另一种源自 Turtle 方法的函数onclick

Cutting to the quick...

快速切割...

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

所以,只需调用screenand not的方法turtle。只需将其更改为:

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 gotoas the funargument, you would see the turtle go to... its own location.

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

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 turtleis a singleton.

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

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:

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

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.

让我知道这个是否奏效。我的电脑上没有 tk,所以我无法对此进行测试。