簡體   English   中英

停用拖放功能

[英]Deactivate the drag-and-drop feature

def drag_and_drop(self):
    """
    Method allowing to drag and drop a pawn
    """
    if self.game.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

上面的代碼允許我將棋子拖放到棋盤中。 drag_and_drop與檢查按鈕(即與 tkinter)相關聯。 當我在界面中選中該框時,將激活拖放功能。 當我取消選中該框時,我想恢復舊設置,即單擊一次案例源,然后單擊第二次目標案例以將棋子移動到新案例。 我想我必須實現方法do_Something()來停用拖放功能? 我怎樣才能做到這一點?

當我選中該框時,拖放已很好地激活,但是當我取消選中該框時,function 拖放仍然激活。 我還沒有找到停用它的方法。

更新

我可以這樣解綁嗎?

self.tag_bind("piece", "<ButtonPress-1>")
self.tag_bind("piece", "<ButtonRelease-1>")
self.tag_bind("piece", "<B1-Motion>")

有幾種方法可以打開和關閉拖放功能。

  1. 正如 OP 所要求的,可以取消綁定標簽綁定: tag_unbind的使用非常簡單: .tag_unbind(<tag>, <sequence>)將取消綁定到具有給定標簽的項目的給定序列的所有綁定。 在這種情況下:

     def drag_and_drop(self): """ Method allowing to drag and drop a pawn """ if self.game.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: self.tag_unbind("piece", "<ButtonPress-1>") self.tag_unbind("piece", "<ButtonRelease-1>") self.tag_unbind("piece", "<B1-Motion>")

    下面是一個完整的例子:

     import tkinter as tk class Board(tk.Canvas): def __init__(self, master, drag_enabled, **kw): tk.Canvas.__init__(self, master, **kw) self.drag_enabled = drag_enabled self._drag_data = {"x": 0, "y": 0, "item": None} self.create_oval(10, 10, 50, 50, fill='white', tags='piece') def drag_and_drop(self): """ Method allowing to drag and drop a pawn """ if self.drag_enabled.get(): 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: self.tag_unbind("piece", "<ButtonPress-1>") self.tag_unbind("piece", "<ButtonRelease-1>") self.tag_unbind("piece", "<B1-Motion>") 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 root = tk.Tk() drag_enabled = tk.BooleanVar(root) board = Board(root, drag_enabled) tk.Checkbutton(root, text='Enable drag', variable=drag_enabled, command=board.drag_and_drop).pack(side='bottom') board.pack() root.mainloop()
  2. 正如評論中所建議的,可以檢查是否在綁定調用的函數中激活了拖放功能,如果禁用,則無需執行任何操作即可返回。 就像是:

     def <drag function>(self, event): if self.drag_enabled.get(): # drag and drop actions

    在這種情況下, drag_and_drop() function 就不需要了,可以放入板子的初始化中:

     import tkinter as tk class Board(tk.Canvas): def __init__(self, master, drag_enabled, **kw): tk.Canvas.__init__(self, master, **kw) self.drag_enabled = drag_enabled self._drag_data = {"x": 0, "y": 0, "item": None} self.create_oval(10, 10, 50, 50, fill='white', tags='piece') 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) def drag_beg(self, event): """Begining drag of an object""" # record the item and its location if self.drag_enabled.get(): 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 if self.drag_enabled.get(): 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 root = tk.Tk() drag_enabled = tk.BooleanVar(root) b = Board(root, drag_enabled) tk.Checkbutton(root, text='Enable drag', variable=drag_enabled).pack(side='bottom') b.pack(fill='both', expand=True) root.mainloop()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM