使用 Tkinter 将输出复制到剪贴板的按钮

A button that copy the output into a clipboard using Tkinter

我刚刚在 python 中使用 tkinter 创建了一个程序,该程序生成随机数并为用户提供 3 个输入(顺便说一句,我只是 Python 中的初学者) 问题是我想创建一个将输出保存在剪贴板中的按钮? 这是代码:

from tkinter import *
import numpy as nmpi
import random



def generate():
    srt = int(entrynbr1.get())
    rng = int(entrynbr2.get())
    sze = int(entrynbr3.get())
    values = random.sample(range(srt, rng), sze)
    hola = nmpi.array(values)

    text_entry.delete('1.0', END)
    text_entry.insert(END, str(hola))



top = Tk()

top.title("Number Generator")
top.minsize(600, 700)
top.resizable(0, 0)


lblnbr1 = Label(top, text="Start",bg='azure')
lblnbr1.place(x=120, y=50)

entrynbr1 = Entry(top, border=2)
entrynbr1.place(x=200, y=50)

lblnbr2 = Label(top, text="Range",bg='azure')
lblnbr2.place(x=120, y=100)

entrynbr2 = Entry(top, border=2)
entrynbr2.place(x=200, y=100)

lblnbr3 = Label(top, text="Size",bg='azure')
lblnbr3.place(x=120, y=150)

entrynbr3 = Entry(top, border=2)
entrynbr3.place(x=200, y=150)

gen = Button(top, border=4 ,text="Generate Numbers", bg='LightBlue1', command=generate)
gen.place(x=220, y=200)


text_entry = Text(top, width=80, height=27, border=4, relief=RAISED)
text_entry.pack()
text_entry.place(x=10, y=250)


top['background'] = 'azure'


top.mainloop()

提前致谢

使用clipboard模块。此代码段中的示例...

import clipboard as cb
from tkinter import * #Testing

win = Tk()
foo = "bar"

copyBtn = Button(win, text="Copy To Clipboard")
copyBtn.pack()

def copyToClipboard(stringToCopy: str):
    cb.copy(stringToCopy)

copyBtn.bind("<Button-1>", lambda e: copyToClipboard(foo))
win.mainloop()

您还可以使用 cb.paste() 粘贴剪贴板中的字符串并将其分配给变量。您需要从 pip 安装模块。

如果这个方法不行,here还有一些其他的方法。

您可以使用 tkinter 的通用 clipboard_append() widget method which means you wouldn't need to download and install an additional third-party module such as clipboard 之一来完成。 (请注意,还有一个 clipboard_get() 链接文档未提及的方法。)

以下是修改代码的方法:我添加了一个 复制到剪贴板 按钮并定义了一个 copy_to_cliboard() 函数,该函数将在单击时调用.该函数使用 clipboard_append() 方法(首先通过另一个名为 clipboard_clear() 的通用小部件方法清除剪贴板后)。

import tkinter as tk  # PEP 8 suggests avoiding `import *`
import numpy as nmpi
import random


def generate():
    srt = int(entrynbr1.get())
    rng = int(entrynbr2.get())
    sze = int(entrynbr3.get())
    values = random.sample(range(srt, rng), sze)
    hola = nmpi.array(values)

    text_entry.delete('1.0', tk.END)
    text_entry.insert(tk.END, str(hola))


def copy_to_clipboard():
    """Copy current contents of text_entry to clipboard."""
    top.clipboard_clear()  # Optional.
    top.clipboard_append(text_entry.get('1.0', tk.END).rstrip())


top = tk.Tk()

top.title("Number Generator")
top.minsize(600, 700)
top.resizable(0, 0)

lblnbr1 = tk.Label(top, text="Start",bg='azure')
lblnbr1.place(x=120, y=50)

entrynbr1 = tk.Entry(top, border=2)
entrynbr1.place(x=200, y=50)

lblnbr2 = tk.Label(top, text="Range",bg='azure')
lblnbr2.place(x=120, y=100)

entrynbr2 = tk.Entry(top, border=2)
entrynbr2.place(x=200, y=100)

lblnbr3 = tk.Label(top, text="Size",bg='azure')
lblnbr3.place(x=120, y=150)

entrynbr3 = tk.Entry(top, border=2)
entrynbr3.place(x=200, y=150)

gen = tk.Button(top, border=4 ,text="Generate Numbers", bg='LightBlue1', command=generate)
gen.place(x=220, y=200)

clp = tk.Button(top, border=4 ,text="Copy To Clipboard", bg='LightBlue1',
                command=copy_to_clipboard)
clp.place(x=220, y=240)

text_entry = tk.Text(top, width=80, height=27, border=4, relief=tk.RAISED)
text_entry.pack()
text_entry.place(x=10, y=280)

top['background'] = 'azure'
top.mainloop()