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

我尝试了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 - Python将变量的内容复制到剪贴板,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/60279745/

10-12 16:48
查看更多