python3.1安装turtle_使用turtle.onclick的Python 3.0

本文介绍了一种使用Python的turtle库来获取屏幕点击坐标的技巧。通过调整箭头大小并隐藏它,使得点击屏幕任意位置均可反馈坐标,适用于计算机图形课程中的图片绘制任务。

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()

The turtle documentation seems to say that the onclick will pass the coordinates in a function that takes in two variables.

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.

解决方案

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()

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值