如何在python中粘贴从键盘复制的文本 [英] How do I paste the copied text from keyboard in python

查看:86
本文介绍了如何在python中粘贴从键盘复制的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我执行此代码,它工作正常.但是,如果我使用键盘 (Ctrl+C) 复制某些内容,那么如何将剪贴板上的文本粘贴到 python 中的任何输入框或文本框中?

If I execute this code, it works fine. But if I copy something using the keyboard (Ctrl+C), then how can I paste the text present on clipboard in any entry box or text box in python?

import pyperclip
pyperclip.copy('The text to be copied to the clipboard.')
spam = pyperclip.paste()

推荐答案

您需要将 pyperclip.paste() 传递到您为条目或文本小部件插入放置字符串的相同位置.

You will want to pass pyperclip.paste() the same place you would place a string for your entry or text widget inserts.

看看这个示例代码.

有一个按钮可以复制输入字段中的内容,还有一个按钮可以粘贴到输入字段.

There is a button to copy what is in the entry field and one to paste to entry field.

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

Alternatively you could just do Ctrl+V :D

这篇关于如何在python中粘贴从键盘复制的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆