【问题标题】:How to turn off drag and drop in Tkinter?如何在 Tkinter 中关闭拖放功能?
【发布时间】:2021-07-06 15:16:28
【问题描述】:

我有一些代码:

from tkinter import *
from tkinter.dnd import Tester as DragWindow, Icon as Dragable

# Make a root window and hide it, since we don't need it.
root = Tk()
root.withdraw()
# Make the actual main window, which can have dragable objects on.
main = DragWindow(root)

def make_btn():
    # The functional part of the main window is the canvas.
    Dragable('B').attach(main.canvas)

def dragoff():
    pass  # What do I write here?

# Make a button and bind it to our button creating function.
B1 = Button(main.top, text='A', command=make_btn)
B1.pack()
B2 = Button(main.top, text='Drag Off', command=dragoff)
B2.pack()

root.mainloop()

我正在使用tkinter.dnd 进行拖放功能。但是我在关闭拖放时遇到问题。 所以,基本的想法是,当我单击B1 时,会创建一个带有按钮的画布,我可以在其中移动它。当我点击B2时,我应该无法在画布中拖放。

【问题讨论】:

    标签: python tkinter drag-and-drop


    【解决方案1】:

    首先你需要保存Dragable实例的引用:

    dragable_item = None
    
    def make_btn():
        global dragable_item
        if dragable_item is None:
            dragable_item = Dragable('B')
            dragable_item.attach(main.canvas)
    

    然后你可以调用dragable_item.label.unbind("<ButtonPress>")来禁用DnD:

    def dragoff():
        if dragable_item:
            dragable_item.label.unbind("<ButtonPress>")
    

    【讨论】:

      猜你喜欢
      • 2017-02-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-17
      • 1970-01-01
      • 1970-01-01
      • 2013-04-15
      相关资源
      最近更新 更多