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

drawing with the mouse with turtle

128 views
Skip to first unread message

Brian Blais

unread,
Nov 12, 2010, 7:24:50 PM11/12/10
to python-list@python.org list
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


--
Brian Blais
bbl...@bryant.edu
http://web.bryant.edu/~bblais
http://bblais.blogspot.com/

Steven D'Aprano

unread,
Nov 12, 2010, 8:05:42 PM11/12/10
to
On Fri, 12 Nov 2010 19:24:50 -0500, Brian Blais wrote:

> 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

Brian Blais

unread,
Nov 12, 2010, 8:48:34 PM11/12/10
to Steven D'Aprano, pytho...@python.org


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.

Message has been deleted

Brian Blais

unread,
Nov 13, 2010, 6:22:59 AM11/13/10
to Dennis Lee Bieber, pytho...@python.org
On Nov 13, 2010, at 1:31 AM, Dennis Lee Bieber wrote:

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

Brian Blais

unread,
Nov 13, 2010, 9:10:41 AM11/13/10
to Brian Blais, edu...@python.org, python-list@python.org list, Steven D'Aprano

On Nov 12, 2010, at 8:48 PM, Brian Blais wrote:

>
> 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?

Steven D'Aprano

unread,
Nov 13, 2010, 5:55:16 PM11/13/10
to
On Sat, 13 Nov 2010 06:22:59 -0500, Brian Blais wrote:

> 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

Steven D'Aprano

unread,
Nov 13, 2010, 6:09:18 PM11/13/10
to
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

solart...@gmail.com

unread,
Feb 10, 2015, 12:28:14 PM2/10/15
to
excuse me :) i know this somewhat disscussion was still on the past few years way back 2010 can i still ask about something? :) what syntax or coding will i use if i wanted to do multiple strokes of the mouse?
because the [turtle.ondrag(turtle.goto)] that you have suggested have really helped but what if i want to write more strokes on different left clicks? like i want to draw 2 lines, like some syntax or code that enables me to add more or just reset the other line and make more or replace it? can you help me :) please :D

i know i'm just some random stranger but please help me :D
0 new messages