如果我执行此代码,它工作正常。但是,如果我使用键盘 (Ctrl+C) 复制某些内容,那么如何将剪贴板上的文本粘贴到 python 中的任何输入框或文本框中?
import pyperclip
pyperclip.copy('The text to be copied to the clipboard.')
spam = pyperclip.paste()
最佳答案
你会想通过 pyperclip.paste()
与您为条目或文本小部件插入放置字符串的位置相同。
看看这个示例代码。
有一个按钮可以复制输入字段中的内容,还有一个按钮可以粘贴到输入字段。
import tkinter as tk
from tkinter import ttk
import pyperclip
root = tk.Tk()
some_entry = tk.Entry(root)
some_entry.pack()
def update_btn():
global some_entry
pyperclip.copy(some_entry.get())
def update_btn_2():
global some_entry
# for the insert method the 2nd argument is always the string to be
# inserted to the Entry field.
some_entry.insert(tk.END, pyperclip.paste())
btn = ttk.Button(root, text="Copy to clipboard", command = update_btn)
btn.pack()
btn2 = ttk.Button(root, text="Paste current clipboard", command = update_btn_2)
btn2.pack()
root.mainloop()
或者你可以只做 Ctrl+V :D
关于python - 如何在python中粘贴从键盘复制的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46817290/