Turtle in python- Trying to get the turtle to move to the mouse click position and print its coordinates -


i'm trying mouse position through python turtle. works except cannot turtle jump position of mouse click.

import turtle  def startmap(): #the next methods pertain drawing map    screen.bgcolor("#101010")    screen.title("welcome, commadore.")    screen.setup(1000,600,1,-1)    screen.setworldcoordinates(0,600,1000,0)    drawcontinents()    #draws bunch of stuff, works should not important question    turtle.pu()    turtle.onclick(turtle.goto)    print(turtle.xcor(),turtle.ycor())    screen.listen() 

as understand, line says 'turtle.onclick(turtle.goto)' should send turtle wherever click mouse, not. print line test, ever returns position sent turtle last, nominally (0, 650) although not have major significance.

i tried looking tutorials , in pydoc, far have not been able write successfully.

i appreciate help. thank you.

edit: need turtle go click position(done) need print coordinates.

you looking onscreenclick(). method of turtlescreen. onclick() method of turtle refers mouse clicks on turtle itself. confusingly, onclick() method of turtlescreen same thing onscreenclick() method.

24.5.4.3. using screen events

turtle.onclick(fun, btn=1, add=none)
turtle.onscreenclick(fun, btn=1, add=none)

parameters:

  • fun – function 2 arguments called coordinates of clicked point on canvas
  • num – number of mouse-button, defaults 1 (left mouse button)
  • addtrue or false – if true, new binding added, otherwise replace former binding

bind fun mouse-click events on screen. if fun none, existing bindings removed.

example turtlescreen instance named screen , turtle instance named turtle:

>>> screen.onclick(turtle.goto) # subsequently clicking turtlescreen >>>                             # make turtle move clicked point. >>> screen.onclick(none)        # remove event binding again 

note: turtlescreen method available global function under name onscreenclick. global function onclick 1 derived turtle method onclick.

cutting quick...

so, invoke method of screen , not turtle. simple changing to:

screen.onscreenclick(turtle.goto) 

if had typed turtle.onclick(lambda x, y: fd(100)) (or that) have seen turtle move forward when clicked on it. goto fun argument, see turtle go to... own location.

printing every time move

if want print every time move, should define own function tell turtle go somewhere. think work because turtle singleton.

def gotoandprint(x, y):     gotoresult = turtle.goto(x, y)     print(turtle.xcor(), turtle.ycor())     return gotoresult  screen.onscreenclick(gotoandprint) 

if turtle.goto() returns none (i wouldn't know), can this:

screen.onscreenclick(lambda x, y: turtle.goto(x, y) or print(turtle.xcor(), turtle.ycor()) 

let me know if works. don't have tk on computer can't test this.


Comments

Popular posts from this blog

curl - PHP fsockopen help required -

HTTP/1.0 407 Proxy Authentication Required PHP -

c# - Resource not found error -