1

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.

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.

ggorlen
  • 44,755
  • 7
  • 76
  • 106
Shaw
  • 169
  • 1
  • 3
  • 12
  • 1
    What is the question here? – Fredrik Håård Apr 09 '13 at 05:36
  • This works for me. Are you interfacing with turtle correctly? Where do you expect the `print` statement to write to? – Waleed Khan Apr 09 '13 at 05:45
  • Ok I tested it out and added a note above. I dont want to be clicking on the arrow head. I want to click on the white space where the arrow head is not so that I may find out what coordinates I want to send the arrow head to. – Shaw Apr 09 '13 at 16:37

6 Answers6

3

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

Replace

def main():
    t.onclick(getPos)
    t.mainloop()
main()

with

def main():
    t.onscreenclick(getPos)
    t.mainloop()
main()
jamesw6811
  • 448
  • 3
  • 10
2

Awesome job figuring out a solution on your own.

Did you ever look through the docs for turtle?

http://docs.python.org/2/library/turtle.html

Looks like you can import screen as well as turtle from the module. screen has an onclick event of its own that does what you expect it to.

Note the following line on how to get access to the screen object:

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.

Chris Pfohl
  • 18,220
  • 9
  • 68
  • 111
  • I took a look at it. I couldn't figure out if that worked. It doesn't seem to do what it says it does. Thanks for the try though. – Shaw Apr 11 '13 at 03:26
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()

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.

Shaw
  • 169
  • 1
  • 3
  • 12
0

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:

import turtle as t

def getPos(x,y):
    print("(", x, "," ,y,")")
    return

def main():
    s = t.getscreen()
    s.onclick(getPos)
    t.mainloop()

main()
0

Python 3.7 version.

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.

Webucator
  • 2,397
  • 24
  • 39
0

I have a small child coder but I learn a lot.

I like to do coding only that for somebody say that you created good but I also love to share out what I learn.

You came here for codes onclick codes are here:

You need to do the following:

# import package 
import turtle 
   
# screen object 
wn = turtle.Screen() 
  
# method to perform action 
def fxn(x, y): 
  turtle.goto(x, y) 
  turtle.write(str(x)+","+str(y)) 
  
# onclick action  
wn.onclick(fxn) 
wn.mainloop()`

Now, just click on the screen and see the magic

William Prigol Lopes
  • 1,803
  • 14
  • 31