Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

clicking on turtle

39 views
Skip to first unread message

pyt...@graner.name

unread,
Nov 6, 2012, 4:13:21 PM11/6/12
to pytho...@python.org
I have a problem with the standard "turtle" module. When a turtle has
a custom shape of type "compound", it doesn't seem to respond to click
events. No problem with polygon shapes.

Running python 3.2.3, turtle version 1.1b on Windows XP.

Here is my test file:

##################################################
import turtle
square = ((0,0),(0,20),(20,20),(20,0))
turtle.addshape("sq1", square) # sq1 = polygon shape
s = turtle.Shape("compound")
s.addcomponent(square, "red")
turtle.addshape("sq2", s) # sq2 = compound shape
t1 = turtle.Turtle(shape="sq1")
t2 = turtle.Turtle(shape="sq2")
t2.fd(20) # set the turtles side by side
def click(x,y): print("click at",x,y)
t1.onclick(click)
t2.onclick(click)
turtle.mainloop()
##################################################

When I run this and click on the black square (i.e. t1), the message
"click at..." gets printed on the console. When I click on the red
square (i.e. t2), nothing happens.

Bug or feature?

--Nicolas

Nicolas Graner

unread,
Nov 6, 2012, 4:09:18 PM11/6/12
to pytho...@python.org

Steven D'Aprano

unread,
Nov 6, 2012, 6:20:25 PM11/6/12
to
On Tue, 06 Nov 2012 22:13:21 +0100, python wrote:

> I have a problem with the standard "turtle" module. When a turtle has a
> custom shape of type "compound", it doesn't seem to respond to click
> events. No problem with polygon shapes.
[...]
> When I run this and click on the black square (i.e. t1), the message
> "click at..." gets printed on the console. When I click on the red
> square (i.e. t2), nothing happens.

I don't know enough about turtle graphics to tell whether it's a bug in
your code, or a bug in the turtle, but I can confirm that the same
behaviour occurs in Python 2.7 on Linux.


--
Steven

Peter Otten

unread,
Nov 7, 2012, 7:54:05 AM11/7/12
to pytho...@python.org
Nicolas Graner wrote:

> I have a problem with the standard "turtle" module. When a turtle has
> a custom shape of type "compound", it doesn't seem to respond to click
> events. No problem with polygon shapes.
>
> Running python 3.2.3, turtle version 1.1b on Windows XP.
>
> Here is my test file:
>
> ##################################################
> import turtle
> square = ((0,0),(0,20),(20,20),(20,0))
> turtle.addshape("sq1", square) # sq1 = polygon shape
> s = turtle.Shape("compound")
> s.addcomponent(square, "red")
> turtle.addshape("sq2", s) # sq2 = compound shape
> t1 = turtle.Turtle(shape="sq1")
> t2 = turtle.Turtle(shape="sq2")
> t2.fd(20) # set the turtles side by side
> def click(x,y): print("click at",x,y)
> t1.onclick(click)
> t2.onclick(click)
> turtle.mainloop()
> ##################################################
>
> When I run this and click on the black square (i.e. t1), the message
> "click at..." gets printed on the console. When I click on the red
> square (i.e. t2), nothing happens.
>
> Bug or feature?

I believe it's a bug. Please report it on http://bugs.python.org
As a quick-fix here's a monkey patch that seems to work:

import turtle

def _onclick(self, item, fun, num=1, add=None):
if isinstance(item, list):
for item in item:
self._onclick(item, fun, num, add)
return

if fun is None:
self.cv.tag_unbind(item, "<Button-%s>" % num)
else:
def eventfun(event):
x, y = (self.cv.canvasx(event.x)/self.xscale,
-self.cv.canvasy(event.y)/self.yscale)
fun(x, y)

self.cv.tag_bind(item, "<Button-%s>" % num, eventfun, add)

turtle.TurtleScreenBase._onclick = _onclick
0 new messages