Python将变量的内容复制到剪贴板

标签 python copy paste

我有一个变量,用于存储用户在对话框中键入的文本。 我需要复制此文本,然后将其粘贴到另一个字段中(以进行搜索)

我尝试了 pyperclip,但它仅适用于纯文本,不适用于变量。这是对话框的代码,e 是我的变量。

from tkinter import *
master = Tk()
e = Entry(master)
e.pack()

e.focus_set()


def callback():
    print(e.get())  # This is the text I want to use later


b = Button(master, text="insert", width=10, command=callback)
b.pack()


mainloop()

最佳答案

您需要创建 tkinter 字符串来存储该值,我已将其包含在下面的代码中。

from tkinter import *
master = Tk()

estring = StringVar(master)

e = Entry(master, textvariable = estring,)
e.pack()

e.focus_set()




def callback():
    print(estring.get())  # This is the text I want to use later


b = Button(master,  text="insert", width=10, command=callback)
b.pack()


mainloop()

下面是使用 pyperclip 的示例,将输入文本复制到剪贴板。

from tkinter import *
import pyperclip

master = Tk()

estring = StringVar(master)

e = Entry(master, textvariable = estring,)
e.pack()

e.focus_set()


def callback():
    pyperclip.copy(estring.get())

b = Button(master,  text="copy", width=10, command=callback)
b.pack()


mainloop()

输入文本并按下复制按钮后,文本就会出现在剪贴板上。

关于Python将变量的内容复制到剪贴板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60279745/

相关文章:

python - python yield 是否意味着继续?

python - 用“来自python中的infile”分割行

Python从xhtml中抓取属性值:link

Python C API 深拷贝

Delphi泛型: How to spec "class that references its own type"

javascript - TinyMCE 粘贴按钮仅适用于 Internet Explorer

python - 使用 PySide 在 QGraphicsScene 中选择项目?

excel - 有条件复制粘贴

cygwin - 如何在 Cygwin 中右键粘贴?

javascript - 如何使用 onPaste 事件将粘贴在文本框中的文本传递给 javascript 函数?