1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
| import tkinter
from tkinter import ttk
import time
def setscrl(s, d, f):
d = float(d)
f = float(f)
if d <= 0 and f >= 1:
s.grid_remove()
else:
s.set(d, f)
s.grid()
def movemouse(event):
t = event.widget
n = t.selection()
moveitem.set(t.focus())
x = event.x
y = event.y
mitem.geometry("%dx%d+%d+%d" % (20, 10, x, y+90))
mitem.deiconify()
t['cursor'] = "hand2"
def mouserelease(event):
mitem.withdraw()
t = event.widget
t['cursor'] = save_cursor
src = moveitem.get()
if src and t.identify_region(event.x, event.y) == "tree":
dest = t.identify_row(event.y)
if t.set(dest, "type") == "file":
dest = t.parent(dest)
if t.set(src, "type") == "file":
if t.parent(src) == dest:
moveitem.set("")
return
else:
if src == dest:
moveitem.set("")
return
if t.set(src, "type") == "dir":
id = t.insert(dest, "end", text=t.item(src, "text"),
values=[t.set(src, "chemin"), "dir"])
for e in t.get_children(src):
txt = t.item(e, "text")
chm = t.set(e, "chemin")
tp = t.set(e, "type")
t.delete(e)
#t.update()
# pour simuler le mouvement
time.sleep(1)
elem = t.insert(id, "end", text=txt, values=[chm, tp])
t.see(elem)
t.update()
t.delete(src)
else:
txt = t.item(src, "text")
chm = t.set(src, "chemin")
t.delete(src)
time.sleep(1)
id = t.insert(dest, "end", text=txt, values=[chm, "file"])
t.see(id)
t.update()
moveitem.set("")
r = tkinter.Tk()
r.title("ttk.Treeview test")
moveitem = tkinter.StringVar()
vs = ttk.Scrollbar(orient="vertical")
t = ttk.Treeview(r, columns=("chemin", "type"), selectmode="browse",
displaycolumns=("chemin"),
yscrollcommand=lambda deb, fin: setscrl(vs, deb, fin))
vs["command"] = t.yview
t.heading("chemin", text="chemin")
t.insert("", 0, "dir1", text="directory 1", values=("d1", "dir"))
t.insert("dir1", "end", "dir 1", text="file 1", values=("f1", "file"))
id = t.insert("", "end", "dir2", text="directory 2", values=("d1", "dir"))
t.insert("dir2", "end", text="dir 2", values=("d2", "dir"))
t.insert(id, "end", text="dir 3", values=("d3", "dir"))
t.insert(id, "end", text="file 2", values=("f1", "file"))
t.grid(column=0, row=0, sticky="nswe")
vs.grid(column=1, row=0, sticky="ns")
save_cursor = t['cursor'] or ""
mitem = tkinter.Toplevel(r)
mitem.overrideredirect(1)
mitem.config(bg="white")
mitem.withdraw()
t.bind("<ButtonRelease-1>", mouserelease)
t.bind("<B1-Motion>", movemouse)
r.mainloop() |
Partager