dimanche 26 juillet 2020

Deactivate the drag-and-drop feature

def drag_and_drop(self):
    """
    Method allowing to drag and drop a pawn
    """
    if self.jeu.onOff.get() == 1:
       self._drag_data = {"x": 0, "y": 0, "item": None}
       self.tag_bind("piece", "<ButtonPress-1>", self.drag_beg)
       self.tag_bind("piece", "<ButtonRelease-1>", self.drag_end)
       self.tag_bind("piece", "<B1-Motion>", self.drag)
    else:
       do_Something()

def drag_beg(self, event):
    """Begining drag of an object"""
    # record the item and its location
    self._drag_data["item"] = self.find_closest(event.x, event.y)[0]
    self._drag_data["x"] = event.x
    self._drag_data["y"] = event.y

def drag_end(self, event):
    """End drag of an object"""
    # reset the drag information
    self._drag_data["item"] = None
    self._drag_data["x"] = 0
    self._drag_data["y"] = 0

def drag(self, event):
    """Handle dragging of an object"""
    # compute how much the mouse has moved
    delta_x = event.x - self._drag_data["x"]
    delta_y = event.y - self._drag_data["y"]
    # move the object the appropriate amount
    self.move(self._drag_data["item"], delta_x, delta_y)
    # record the new position
    self._drag_data["x"] = event.x
    self._drag_data["y"] = event.y

The code above allows me to drag and drop a pawn in a checkerboard. The drag_and_drop function is associated with a checkbutton (i.e. with tkinter). When I check the box in my interface, the drag and drop is activated. When I unchecked the box, I would like to bring back the old setup, i.e. I click once on a case source and I click a second time target case to move the pawn to the new case. I think I have to implement the method do_Something() to deactivate the drag-and-drop feature? How can I do that?




Aucun commentaire:

Enregistrer un commentaire