Python 3.0 使用turtle.onclick
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15893236/
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
Python 3.0 using turtle.onclick
提问by Shaw
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.
所以这是我的问题,我必须为我的 CS 课制作一张照片,在乌龟中进行估算真的很令人沮丧。我打算使用 .onclick() 来显示我的位置。
import turtle as t
def getPos(x,y):
print("(", x, "," ,y,")")
return
def main():
t.onclick(getPos)
t.mainloop()
main()
The turtle documentation seems to say that the onclick will pass the coordinates in a function that takes in two variables.
海龟文档似乎说 onclick 将在一个接受两个变量的函数中传递坐标。
http://docs.python.org/3.1/library/turtle.html#turtle.onclick
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.
任何帮助,将不胜感激。
采纳答案by Shaw
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).
好吧,我想出了一个解决办法。它不是一个完美的解决方案,但效果很好。因为 onclick 只会在您单击箭头时响应,所以我让箭头包含整个屏幕。然后我把它藏起来了。你需要做的是将鼠标悬停在你想要去的位置上,按“a”,当它变黑时点击屏幕。然后外壳将显示您需要的坐标。确保你总是回到 (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()
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.
另外,如果我班上的任何人发现了这一点,我是宾厄姆顿的 CS 学生,如果您使用它,我建议您不要留下任何痕迹。教授已经看到了这一点,并且会认出它。
回答by Chris Pfohl
Awesome job figuring out a solution on your own.
自己找出解决方案的出色工作。
Did you ever look through the docs for turtle?
你有没有浏览过文档turtle?
http://docs.python.org/2/library/turtle.html
http://docs.python.org/2/library/turtle.html
Looks like you can import screenas well as turtlefrom the module. screenhas an onclickevent of its own that does what you expect it to.
看起来您可以导入screen以及turtle从模块导入。 screen有一个onclick自己的事件,可以做你期望的事情。
Note the following line on how to get access to the screenobject:
请注意以下有关如何访问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.
Disclaimer: I've never used turtle before.
免责声明:我以前从未使用过乌龟。
回答by jamesw6811
You need to use the Screen class. However, if you want to stay away from OOP, you can use the built-in method turtle.onscreenclick(func).
您需要使用 Screen 类。但是,如果您想远离 OOP,则可以使用内置方法turtle.onscreenclick(func)。
Replace
代替
def main():
t.onclick(getPos)
t.mainloop()
main()
with
和
def main():
t.onscreenclick(getPos)
t.mainloop()
main()
回答by Levente Buttyán
You have to get first the screen object the turtle is drawing on and then call onclick() of the screen object. Here is an example:
您必须首先获得海龟正在绘制的屏幕对象,然后调用屏幕对象的 onclick() 。下面是一个例子:
import turtle as t
def getPos(x,y):
print("(", x, "," ,y,")")
return
def main():
s = t.getscreen()
s.onclick(getPos)
t.mainloop()
main()
回答by Webucator
Python 3.7 version.
Python 3.7 版本。
import turtle as t
def click(x,y):
print('Clicked:', x, y)
def main():
s = t.Screen()
s.onclick(click)
s.mainloop()
main()
Note that the printing takes place at the console, not on Turtle.
请注意,打印是在控制台上进行的,而不是在 Turtle 上进行。

