Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You are looking for <a href="http://docs.python.org/2/library/turtle.html#turtle.onscreenclick" rel="nofollow"><code>onscreenclick()</code></a>. It is a method of <code>TurtleScreen</code>. The <a href="http://docs.python.org/2/library/turtle.html#turtle.onclick" rel="nofollow"><code>onclick()</code></a> method of a <code>Turtle</code> refers to mouse clicks on the turtle itself. Confusingly, the <code>onclick()</code> method of <code>TurtleScreen</code> is the same thing as its <code>onscreenclick()</code> method.</p> <blockquote> <h3>24.5.4.3. Using screen events<a href="http://docs.python.org/2/library/turtle.html#using-screen-events" rel="nofollow">¶</a></h3> <p><code>turtle.onclick</code>(<em>fun</em>, <em>btn=1</em>, <em>add=None</em>)<br/> <code>turtle.onscreenclick</code>(<em>fun</em>, <em>btn=1</em>, <em>add=None</em>)<a href="http://docs.python.org/2/library/turtle.html#turtle.onscreenclick" rel="nofollow">¶</a></p> <p><strong>Parameters:</strong> </p> <ul> <li><strong>fun</strong> – a function with two arguments which will be called with the coordinates of the clicked point on the canvas</li> <li><strong>num</strong> – number of the mouse-button, defaults to 1 (left mouse button)</li> <li><strong>add</strong> – <strong><code>True</code></strong> or <strong><code>False</code></strong> – if <strong><code>True</code></strong>, a new binding will be added, otherwise it will replace a former binding</li> </ul> <p>Bind <em>fun</em> to mouse-click events on this screen. If <em>fun</em> is <strong><code>None</code></strong>, existing bindings are removed.</p> <p>Example for a TurtleScreen instance named <code>screen</code> and a Turtle instance named turtle:</p> </blockquote> <pre><code>&gt;&gt;&gt; screen.onclick(turtle.goto) # Subsequently clicking into the TurtleScreen will &gt;&gt;&gt; # make the turtle move to the clicked point. &gt;&gt;&gt; screen.onclick(None) # remove event binding again </code></pre> <blockquote> <p><strong>Note</strong>: This TurtleScreen method is available as a global function only under the name <code>onscreenclick</code>. The global function <code>onclick</code> is another one derived from the Turtle method <code>onclick</code>.</p> </blockquote> <h2>Cutting to the quick...</h2> <p>So, just invoke the method of <code>screen</code> and not <code>turtle</code>. It is as simple as changing it to:</p> <pre><code>screen.onscreenclick(turtle.goto) </code></pre> <p>If you had typed <code>turtle.onclick(lambda x, y: fd(100))</code> (or something like that) you would probably have seen the turtle move forward when you clicked on it. With <code>goto</code> as the <code>fun</code> argument, you would see the turtle go to... its own location.</p> <h3>Printing every time you move</h3> <p>If you want to print every time you move, you should define your own function which will do that as well as tell the turtle to go somewhere. I think this will work because <code>turtle</code> is a singleton.</p> <pre><code>def gotoandprint(x, y): gotoresult = turtle.goto(x, y) print(turtle.xcor(), turtle.ycor()) return gotoresult screen.onscreenclick(gotoandprint) </code></pre> <p>If <code>turtle.goto()</code> returns <code>None</code> (I wouldn't know), then you can actually do this:</p> <pre><code>screen.onscreenclick(lambda x, y: turtle.goto(x, y) or print(turtle.xcor(), turtle.ycor()) </code></pre> <p>Let me know if this works. I don't have tk on my computer so I can't test this.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload