我创建了一个将复制到系统剪贴板的函数。但是,当我从剪贴板粘贴值时,它将自动执行回车。这极大地影响了我程序中的计算。
注意:不能使用Pyperclip或任何其他安装。我只能使用Python IDLE 3.8中包含的内容
我尝试将strip()方法与剪贴板_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
|
答案 0 :(得分:1)
请勿使用os.system
。使用subprocess
,您可以将字符串直接输入到clip
的标准输入中,而无需调用shell管道。
from subprocess import Popen, PIPE
Popen(["clip"], stdin=PIPE).communicate(bytes(solution_answer))
答案 1 :(得分:0)
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()