我创建了一个将复制到系统剪贴板的函数。但是,当我从剪贴板粘贴值时,它会自动执行回车。这极大地影响了我的程序中的计算。
注意:不能使用Pyperclip或任何其他安装。我只能使用Python IDLE 3.8中包含的内容
我已经尝试将strip()方法与clipboard_answer变量一起使用。它仍然返回到下一行
def copy(solution_answer):
clipboard_answer = str(solution_answer)
command = 'echo ' + clipboard_answer.strip() + '| clip' # Creates command variable, then passes it to the os.system function as an argument. CMD opens and applys echo (number calculated) | clip and runs the clipboard function
os.system(command)
print("\n\n\n\n",solution_answer, "has been copied to your clipboard") # Used only for confirmation to ensure copy function runs
假装“|”图标是光标
我有一个复制到剪贴板的解决方案,即25
当我在程序中按CTRL + V时,我希望它能够做到这一点
25 |
但实际上,光标就是这样
25
|
不要使用os.system
。使用subprocess
,您可以直接将字符串提供给clip
的标准输入,而无需调用shell管道。
from subprocess import Popen, PIPE
Popen(["clip"], stdin=PIPE).communicate(bytes(solution_answer))
import pyperclip
pyperclip.copy(solution)
这应该可以解决问题。
编辑:tkinter解决方案再次,因为pyperclip不是OP的选项。
from tkinter import Tk
r = Tk()
r.withdraw()
r.clipboard_clear()
r.clipboard_append("hello world")
r.update()