【发布时间】:2014-04-06 13:03:13
【问题描述】:
我正在用 Python 编写一个程序,以允许我浏览我获取的实验数据,并查看与每个数据集的实验条件相关的元数据。
这个程序的核心是TkinterTreectrl 小部件,它是tk treectrl 的包装器
尽管是一个新手程序员,但到目前为止一切正常 - 我可以添加项目、删除项目、选择它们等等。
但是,我无法弄清楚如何允许将项目拖动到树中的新位置。我在文档中找到了许多关于拖动图像的参考,目前我可以拖动一个诱人的虚线轮廓。但我找不到通过拖放生成的事件。我在手册中找到的最接近的是以下一组事件:
<Drag-begin>
<Drag-receive>
<Drag-end>: Generated whenever the user drag-and-drops a file into a
directory. This event is generated by the filelist-bindings.tcl
library code, which is not used by default.
我不知道如何使用“filelist-bindings.tcl库代码”,但我不确定这是不是正确的方法。
在相关查询中 - 我认为解决此问题和类似问题的一种方法是将我的Treectrl 小部件生成的每个 事件发送到stdout。有没有办法做到这一点?
这里的最小工作示例太大而无法使用,但摘录可能会有所帮助:
# add callbacks for Edit events
self.t.notify_install('<Edit-begin>')
self.t.notify_bind('<Edit-begin>', self.edit_begin)
self.t.notify_install('<Edit-accept>')
self.t.notify_bind('<Edit-accept>', self.edit_accept)
self.t.notify_install('<Edit-end>')
self.t.notify_bind('<Edit-end>', self.edit_end)
# add callbacks for mouse clicks
self.t.bind('<Button-1>', self.OnLeftClick)
self.t.bind('<Button-3>', self.OnRightClick)
# add callbacks for drag events
self.t.notify_install('<Drag-begin>')
self.t.bind('<Drag-begin>', self.OnDrag)
self.t.notify_install('<Drag-recieve>')
self.t.bind('<Drag-recieve>', self.OnDrag)
self.t.notify_install('<Drag-end>')
self.t.bind('<Drag-end>', self.OnDrag)
编辑事件和鼠标点击的回调工作正常,尝试绑定拖动事件会生成_tkinter.TclError: bad event type or keysym "Drag"
【问题讨论】:
-
你看过序列
'<B1-Motion>'吗? -
@Scorpion_God 谢谢,这似乎把我带向了正确的方向。我以为会有一些内置的拖动处理,但也许没有。
标签: python drag-and-drop tree tkinter