drawing with the mouse with turtle

B

Brian Blais

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 tried to do something like this:

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
 
S

Steven D'Aprano

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:

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

Brian Blais

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:


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.


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.


bb
 
D

Dennis Lee Bieber

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()
I'm not familiar with the turtle module but... would it make more
sense to drop the pen before dragging the turtle around?

I'm also still on Python 2.5 so don't see an .ondrag() method. Let's
see: Python 2.7 documents

http://docs.python.org/library/turtle.html#turtle.ondrag
"""
Subsequently, clicking and dragging the Turtle will move it across the
screen thereby producing handdrawings (if pen is down).
"""

"if pen is down" <===
 
B

Brian Blais

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.

thanks,
bb
 
B

Brian Blais

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?

bb
 
S

Steven D'Aprano

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

Steven D'Aprano

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?
 
Joined
Jan 19, 2014
Messages
1
Reaction score
0
On Sat, 13 Nov 2010 09:10:41 -0500, Brian Blais wrote:

> 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

You don't need to set world coordinates for this type of situation, all you need to do is replace 'x-360' to "x-400", this will fix your transformations =)

-----
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,818
Latest member
Brigette36

Latest Threads

Top