python - 如何在 Tkinter 中关闭拖放功能?
问题描述
我有一些代码:
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
,会创建一个带有按钮的 Canvas,我可以在其中移动它。当我单击 时B2
,我应该无法在画布中拖放。
解决方案
首先,您需要保存以下实例的引用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>")
推荐阅读
- java - 遍历向量中的元素,以循环方式从最后一个元素环绕到第一个元素
- javascript - 如何使用 AJAX 调用返回的数据创建或填充表
- asynchronous - 使用龙卷风的 AsyncHTTPClient 与在 run_in_executor() 中包装阻塞 http 调用之间是否存在显着差异?
- typescript - Typescript 函数类型顺序问题 |
- c++ - 在 main() 中获取不带参数的命令行参数
- azure-devops - Azure DevOps iframe 中的 Blazor 路由失败
- telerik - 搜索框支持以逗号分隔的多个搜索
- excel - 如何比较excel中的两张表和输出的异同?
- android - 我想获取 IP 地址,例如如果 VPN 已激活,它将以正确的格式获取此 VPN IP 地址
- flutter - 单个 Flutter Stream 和 2 个 Stream 构建器的问题