Blublublub
Blublublub

Reputation: 57

Create a line from an oval to another by dragging

I'm working on a project to represent a friendship network. I'm using ovals to represent the friends, and lines to represent their friendship.

I've looked up how to bind an event, and so far I understood how to bind the event as so :

def line(self, event):
    x1, y1 = (event.x - 1), (event.y - 1)
    x2, y2 = (event.x + 1), (event.y + 1)
    self.display_canvas.create_line(x1, y1, x2, y2, fill="black")

but that only draws one-pixel lines and follows the trajectory of the mouse.

What I would like to do is to create a straight line that also follows the mouse until it stops on an oval, and only if it is started on an oval.

It would look like this (it's the exact same, except I couldn't do it by dragging) : !http://s17.postimg.org/lhveezdi7/line.png

Thank you.

Upvotes: 0

Views: 34

Answers (1)

Bryan Oakley
Bryan Oakley

Reputation: 385830

On a <B1>event, remember the x/y coordinate. On <B1-Motion>, draw a line from the remembered coordinate to the coordinate of the event. At any point in time you can check whether a coordinate is over or near an oval with the find_* functions of the canvas.

Upvotes: 1

Related Questions