4

ツリー内のノードをカットアンドペーストする方法、またはボタンやキーバインディングを使用してノードを上下に移動する方法を確認できます。ツリービューの周りにノードのドラッグアンドドロップを実装する方法はありますか?

4

3 に答える 3

10

左クリックとシフト左クリックを処理するいくつかの実用的なサンプル コードを次に示します。

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()
于 2012-11-13T01:40:15.680 に答える
5

これは自分で設定する必要がありますが、間違いなく可能です。<ButtonPress-1>(ドラッグするアイテムを識別する)、<ButtonRelease-1>(ドロップを実装する)、および<B1-Motion>(ドラッグ中にフィードバックを提供する) の適切なバインディングを作成する必要があるだけです。

于 2012-07-20T13:53:18.240 に答える
3

解決策についてコメントを残すことができなかったので、ここに私の 50c を追加しました: マルチレベル ツリーを介して上下にドラッグする問題を解決するには、以下の行に従って、移動時に親 ID を指定する必要があります。

tv.move(s, tv.identify_row(event.y), moveto)

これは誰かにとって役立つかもしれません...

于 2017-01-11T19:45:35.903 に答える