python - tkinter treeview - 拖放?

标签 python tkinter

我可以看到如何在树中剪切和粘贴节点,或者使用按钮或键绑定(bind)将它们上下移动。有没有办法在 TreeView 周围实现拖放节点?

最佳答案

这是一些处理左键单击和 shift-left-click 的工作示例代码:

import Tkinter as tk
import ttk

def bDown_Shift(event):
    tv = event.widget
    select = [tv.index(s) for s in tv.selection()]
    select.append(tv.index(tv.identify_row(event.y)))
    select.sort()
    for i in range(select[0],select[-1]+1,1):
        tv.selection_add(tv.get_children()[i])

def bDown(event):
    tv = event.widget
    if tv.identify_row(event.y) not in tv.selection():
        tv.selection_set(tv.identify_row(event.y))    

def bUp(event):
    tv = event.widget
    if tv.identify_row(event.y) in tv.selection():
        tv.selection_set(tv.identify_row(event.y))    

def bUp_Shift(event):
    pass

def bMove(event):
    tv = event.widget
    moveto = tv.index(tv.identify_row(event.y))    
    for s in tv.selection():
        tv.move(s, '', moveto)

root = tk.Tk()

tree = ttk.Treeview(columns=("col1","col2"), 
                    displaycolumns="col2", 
                    selectmode='none')

# insert some items into the tree
for i in range(10):
    tree.insert('', 'end',iid='line%i' % i, text='line:%s' % i, values=('', i))

tree.grid()
tree.bind("<ButtonPress-1>",bDown)
tree.bind("<ButtonRelease-1>",bUp, add='+')
tree.bind("<B1-Motion>",bMove, add='+')
tree.bind("<Shift-ButtonPress-1>",bDown_Shift, add='+')
tree.bind("<Shift-ButtonRelease-1>",bUp_Shift, add='+')

root.mainloop()

关于python - tkinter treeview - 拖放?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11570786/

相关文章:

python - 使用多处理将函数的返回值分配给变量?关于 IDLE 的问题?

python - 使用 TensorFlow 移动梯度

python - PYODBC 不喜欢 %, "The SQL contains 2 parameter markers, but 1 parameters were supplied."

python - 将 Mechanize 与不在表单内的选择字段一起使用?

python - 如何将 sklearn tfidf 矢量 pandas 输出转换为有意义的格式

python按钮点击后更改文本

python - 通过pip安装时,如何告诉matplotlib如何找到tkinter?

python - 如何在计算器中显示数字,在 Python 中使用 TKinter?

python - Tkinter 线程

python - 如何用不同颜色的tkinter python制作不同的单词?