問題描述
所以這是我的問題,我必須為我的 CS 課制作一張照片,在海龜中進(jìn)行估計(jì)真的很令人沮喪.我打算使用 .onclick() 來顯示我的位置.
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.
import turtle as t
def getPos(x,y):
print("(", x, "," ,y,")")
return
def main():
t.onclick(getPos)
t.mainloop()
main()
海龜文檔似乎說 onclick 將在一個(gè)接受兩個(gè)變量的函數(shù)中傳遞坐標(biāo).
The turtle documentation seems to say that the onclick will pass the coordinates in a function that takes in two variables.
http://docs.python.org/3.1/library/turtle.html#turtle.onclick
注意:當(dāng)我單擊箭頭時(shí)它會(huì)起作用,但這不是我想要的.我想點(diǎn)擊屏幕上的其他位置來找出我應(yīng)該將箭頭發(fā)送到哪個(gè)坐標(biāo)!
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!
任何幫助將不勝感激.
推薦答案
好吧,我想出了一個(gè)解決辦法.它不是一個(gè)完美的解決方案,但效果很好.因?yàn)橹挥挟?dāng)你點(diǎn)擊箭頭時(shí) onclick 才會(huì)響應(yīng),所以我讓箭頭包含了整個(gè)屏幕.然后我把它藏了起來.你需要做的是將鼠標(biāo)懸停在你想去的位置上,按a",當(dāng)它變黑時(shí)點(diǎn)擊屏幕.然后外殼將顯示您需要的坐標(biāo).確保你總是回到 (1000,0).
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).
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()
另外,如果我班上的任何人發(fā)現(xiàn)這個(gè),我是賓厄姆頓的一名 CS 學(xué)生,如果你使用這個(gè),我建議不要留下任何痕跡.教授已經(jīng)看到了這一點(diǎn),并且會(huì)認(rèn)出它.
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.
這篇關(guān)于Python 3.0 使用 turtle.onclick的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!