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()
如果您已经在代码中使用了
tkinter
,那么您只需要剪贴板中的内容。然后tkinter
有一个内置的方法来完成这项工作。要在
tkinter
条目/Textbox中添加复制的文本,可以使用tkinter
变量:并将该变量链接到Entry小部件。
您需要删除行:
因为它会覆盖使用键盘复制的内容。
例如,我复制了问题的标题,下面是我如何粘贴到python shell中的:
您需要将
pyperclip.paste()
传递到为条目或文本小部件插入放置字符串的同一位置。看看这个示例代码。
有一个按钮可以复制输入字段中的内容,还有一个按钮可以粘贴到输入字段。
或者,您可以只执行Ctrl+V:D
相关问题 更多 >
编程相关推荐