[Edu-sig] drawing with the mouse with turtle...solved?
Brian Blais
bblais at bryant.edu
Sat Nov 13 15:10:41 CET 2010
On Nov 12, 2010, at 8:48 PM, Brian Blais wrote:
>
> On Nov 12, 2010, at 8:05 PM, Steven D'Aprano wrote:
>
>> 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.
>
>
> 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
--
Brian Blais
bblais at bryant.edu
http://web.bryant.edu/~bblais
http://bblais.blogspot.com/
More information about the Edu-sig
mailing list