【问题标题】:Python TkinterTreectrl drag itemsPython Tkinter Treectrl 拖动项目
【发布时间】: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"

【问题讨论】:

  • 你看过序列'&lt;B1-Motion&gt;'吗?
  • @Scorpion_God 谢谢,这似乎把我带向了正确的方向。我以为会有一些内置的拖动处理,但也许没有。

标签: python drag-and-drop tree tkinter


【解决方案1】:

忽略这个问题不是最新问题的事实 - 您使用 self.t.bind 而不是 self.t.notify_bind 是否有特定原因?

对我来说,这看起来很像 Michael 的 tktreecrtl 接口没有使用包装器功能,而是尝试直接将 &lt;Drag-begin&gt; 绑定到基于 @ 的 Treectrl 实例987654325@

  • 来源摘录:

    类 Treectrl(tkinter.Widget):

tkinter.Widget 不可能找到 '' 事件,因为它是在 tktreectrl 库中生成的。所以你得到了

错误的事件类型或键符“拖动”

更改为self.t.notify_bind 应该可以解决您的问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-08-09
    • 2017-08-17
    • 2013-04-06
    • 2023-03-12
    • 2021-03-25
    • 1970-01-01
    • 1970-01-01
    • 2013-10-04
    相关资源
    最近更新 更多