<p>对于windows,可以使用Powershell命令<a href="https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/set-clipboard?view=powershell-7.1" rel="nofollow noreferrer">^{<cd1>}</a>。要运行此命令,请使用子流程模块。既然文件/文件夹已被复制,您现在可以使用<kbd>ctrl</kbd>+<kbd>v</kbd>或右键单击上下文菜单将其粘贴到文件资源管理器中</p>
<p>要处理粘贴,只需使用tkinter提供的<code>clipboard_get()</code>,它将为您提供文件/文件夹的路径。然后,您可以使用<code>shutil.copy</code>/<code>shutil.copytree</code>从应用程序中的src复制内容</p>
<p>然后,可以重新加载树视图以使更改可见</p>
<p>例如:</p>
<pre class="lang-py prettyprint-override"><code>import os
import subprocess
import shutil
import tkinter as tk
import tkinter.ttk as ttk
class App(tk.Frame):
def __init__(self, master, path):
tk.Frame.__init__(self, master)
self.tree = ttk.Treeview(self)
ysb = ttk.Scrollbar(self, orient='vertical', command=self.tree.yview)
xsb = ttk.Scrollbar(self, orient='horizontal', command=self.tree.xview)
self.tree.configure(yscroll=ysb.set, xscroll=xsb.set)
self.tree.heading('#0', text=path, anchor='w')
self.abspath = os.path.abspath(path)
self.tree.bind("<Control-c>",self.copy_to_clipboard)
self.tree.bind("<Control-v>",self.create_directory_from_clipboard)
self.tree.grid(row=0, column=0)
ysb.grid(row=0, column=1, sticky='ns')
xsb.grid(row=1, column=0, sticky='ew')
self.grid()
self.store_path = []
self.reload()
def copy_to_clipboard(self,event,*args):
item = self.tree.focus()
self.store_path.append(self.tree.item(item,"text"))
absolute_path = self.find_absolutePath(item)
#cmd = r"ls '{}' | Set-Clipboard".format(absolute_path) # if you only want the contents of folder to be copied
cmd = r"gi '{}' | Set-Clipboard".format(absolute_path) # copies both folder and its contents
subprocess.run(["powershell", "-command", cmd], shell=True) # windows specific
print("copied")
def find_absolutePath(self, item):
parent_id = self.tree.parent(item)
if parent_id:
parent = self.tree.item(parent_id, 'text')
self.store_path.append(parent)
return self.find_absolutePath(parent_id)
else:
absolute_path = os.path.join(*self.store_path[::-1])
self.store_path = []
return absolute_path
def create_directory_from_clipboard(self, event):
cur_item = self.tree.focus()
self.store_path.append(self.tree.item(cur_item, "text"))
dest = self.find_absolutePath(cur_item)
src_path = self.clipboard_get()
try:
if os.path.isdir(src_path):
src = os.path.basename(os.path.normpath(src_path))
#os.mkdir(os.path.join(src_path, src))
shutil.copytree(src_path, os.path.join(dest, src))
else:
shutil.copy(src_path, dest)
self.reload()
print("pasted")
except (FileExistsError, FileNotFoundError, shutil.SameFileError) as e:
print(f"Error: {e}")
def reload(self):
self.tree.delete(*self.tree.get_children())
root = self.tree.insert('', 'end', text=self.abspath, open=True)
self.process_directory(root, self.abspath)
def process_directory(self, parent, path):
try:
for p in os.listdir(path):
abspath = os.path.join(path, p)
isdir = os.path.isdir(abspath)
oid = self.tree.insert(parent, 'end', text=p, open=False)
if isdir:
self.process_directory(oid, abspath)
except PermissionError:
pass
root = tk.Tk()
path_to_my_project = r'mypath\ '
app = App(root, path=path_to_my_project)
app.mainloop()
</code></pre>
<p>如果你想让它与其他操作系统一起工作,你必须找到相应的命令<a href="https://unix.stackexchange.com/questions/211817/copy-the-contents-of-a-file-into-the-clipboard-without-displaying-its-contents">eg</a></p>
<p>或者,您也可以使用<a href="https://stackoverflow.com/questions/7045264/how-do-i-read-a-jpg-or-png-from-the-windows-clipboard-in-python-and-vice-versa">win32clipboard</a>、<a href="https://stackoverflow.com/search?q=win32clipboard+copy+file">1</a>,或者可以使用PyQt/pyslide的<a href="https://doc.qt.io/qt-5/qclipboard.html" rel="nofollow noreferrer">QClipboard</a>或PyGTK<a href="https://developer.gnome.org/pygtk/stable/class-gtkclipboard.html" rel="nofollow noreferrer">clipboard</a>,它们提供了方便的方法来执行这些类型的操作</p>