How it works...

To bind events to items, we use the tag_bind() method from the Canvas class. This adds the event binding to all the items that match the item specifier, in our example, the "draggable" tag.

Even the method is named tag_bind(); it would be also valid to pass an item identifier instead of a tag:

        self.canvas.tag_bind("draggable", "<ButtonPress-1>",
self.button_press)
self.canvas.tag_bind("draggable", "<Button1-Motion>",
self.button_motion)

Keep in mind that this only affects existing tagged items, so if we add new items later on with the "draggable" tag, they will not have these bindings attached.

The button_press() method is the handler invoked when an item is clicked. As usual, a common pattern to retrieve the clicked item is to call canvas.find_withtag(tk.CURRENT).

This item identifier and the x and y coordinates of the click event are stored in the dnd_item field. These values will be used later to move the item in sync with the mouse motion:

    def button_press(self, event):
item = self.canvas.find_withtag(tk.CURRENT)
self.dnd_item = (item, event.x, event.y)

The button_motion() method processes the mouse motion events while the primary button is being held.

To set the distance that the item should be moved, we calculate the difference from the current event position to the previously stored coordinates. These values are passed to the canvas.move() method and saved again in the dnd_item field:

    def button_motion(self, event):
x, y = event.x, event.y
item, x0, y0 = self.dnd_item
self.canvas.move(item, x - x0, y - y0)
self.dnd_item = (item, x, y)

There are some variations of this drag and drop functionality that also implement a handler for the <ButtonRelease-1> sequence, which unsets the currently dragged item.

However, this is not necessary because once this type of event occurs, the <Button1-Motion> binding will not be triggered until an item is clicked again. This also saves us from checking whether dnd_item is not None at the beginning of the button_motion() handler.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset