import turtle
def gothere(event):
print event.x
print event.y
turtle.goto(event.x,event.y)
turtle.reset()
turtle.speed(0)
c=turtle.getcanvas()
c.bind("<Button-1>", gothere)
turtle.pendown()
but this seemed to draw in the wrong place (like the coordinates were wrong). Is there a good way to do this?
thanks,
bb
--
Brian Blais
bbl...@bryant.edu
http://web.bryant.edu/~bblais
http://bblais.blogspot.com/
> I'd like to draw on a turtle canvas, but use the mouse to direct the
> turtle. I don't see a good way of getting the mouse coordinates and the
> button state.
I think the right way to do that is by creating an event handler to the
turtle. These docs are for Python 2.7 turtle, but they may be applicable
to older versions as well:
http://docs.python.org/library/turtle.html#turtle.ondrag
I quote:
>>> turtle.ondrag(turtle.goto)
Subsequently, clicking and dragging the Turtle will move it
across the screen thereby producing handdrawings (if pen is down).
That's probably all you need to do.
--
Steven
that's what I tried first, with no luck. I am on 2.6 on Mac OSX (Enthought distribution). The following code:
import turtle
turtle.reset()
turtle.speed(0)
turtle.ondrag(turtle.goto)
turtle.pendown()
running it in ipython brings up a window, but clicking, dragging, or anything like that doesn't move the turtle or draw anything. running it in just plain python brings up the window, but it instantly closes. I added: turtle.mainloop()
which keeps the window open, but the clicking or dragging still doesn't move the turtle or update the window in any way.
> On Fri, 12 Nov 2010 20:48:34 -0500, Brian Blais <bbl...@bryant.edu>
> declaimed the following in gmane.comp.python.general:
>
>> turtle.ondrag(turtle.goto)
>> turtle.pendown()
>>
>>
> I'm not familiar with the turtle module but... would it make more
> sense to drop the pen before dragging the turtle around?
>
> """
> Subsequently, clicking and dragging the Turtle will move it across the
> screen thereby producing handdrawings (if pen is down).
> """
>
> "if pen is down" <===
>
there is no change by changing the order, but I didn't expect one. since ondrag is binding a callback, which is only called when the event happens, I figure that the pen has to be down when the callback happens, not when the binding occurs. since the pen is down (and never lifted), when I start dragging the mouse around after that it should work...but doesn't seem to.
>
> On Nov 12, 2010, at 8:05 PM, Steven D'Aprano wrote:
>
> that's what I tried first, with no luck. I am on 2.6 on Mac OSX (Enthought distribution). The following code:
>
>
> import turtle
>
> turtle.reset()
> turtle.speed(0)
> turtle.ondrag(turtle.goto)
> turtle.pendown()
>
> running it in ipython brings up a window, but clicking, dragging, or anything like that doesn't move the turtle or draw anything. running it in just plain python brings up the window, but it instantly closes. I added: turtle.mainloop()
>
> which keeps the window open, but the clicking or dragging still doesn't move the turtle or update the window in any way.
>
Here is code that "works", with at least one small oddity:
import turtle
def gothere(event):
turtle.penup()
turtle.goto(event.x-360,340-event.y)
turtle.pendown()
def movearound(event):
turtle.goto(event.x-360,340-event.y)
def release(event):
turtle.penup()
def reset(event):
turtle.clear()
turtle.reset()
turtle.speed(0)
c=turtle.getcanvas()
c.bind("<Button-1>", gothere)
c.bind("<B1-Motion>", movearound)
c.bind("<ButtonRelease-1>", release)
c.bind("<Escape>",reset)
s=turtle.Screen()
s.listen()
the oddity is that the coordinate transformations, x-360 and 340-y, are done by eye and do not seem to be related to any of the coordinate values I could find. my screen size is 300x400, the x and y canvas scales are 1 and 1, but if I try to transform with those numbers the mouse is clearly off.
any ideas?
> there is no change by changing the order, but I didn't expect one.
> since ondrag is binding a callback, which is only called when the event
> happens, I figure that the pen has to be down when the callback happens,
> not when the binding occurs. since the pen is down (and never lifted),
> when I start dragging the mouse around after that it should work...but
> doesn't seem to.
Can you tell if your callback is being called at all?
In other words, can you distinguish between these?
* the callback isn't being called;
* the callback is being called, but nothing is being drawn.
--
Steven
> Here is code that "works", with at least one small oddity:
>
> import turtle
>
> def gothere(event):
> turtle.penup()
> turtle.goto(event.x-360,340-event.y)
> turtle.pendown()
>
> def movearound(event):
> turtle.goto(event.x-360,340-event.y)
[...]
> the oddity is that the coordinate transformations, x-360 and 340-y, are
> done by eye and do not seem to be related to any of the coordinate
> values I could find. my screen size is 300x400, the x and y canvas
> scales are 1 and 1, but if I try to transform with those numbers the
> mouse is clearly off.
Have you set the coordinates of the screen?
screen.setworldcoordinates
What do turtle.window_height() and turtle.window_width() return?
--
Steven