Python 3.0 using turtle.onclick

前端 未结 6 1748
梦毁少年i
梦毁少年i 2020-12-04 03:51

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.

6条回答
  •  天涯浪人
    2020-12-04 04:18

    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.

提交回复
热议问题