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

发布于 2025-01-11 16:03:44 字数 1252 浏览 0 评论 0原文

我刚刚使用 tkinter 在 python 中创建了一个程序,该程序生成随机数,并为用户提供 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()

提前致谢

I've just created a program in python using tkinter that generate random numbers with a 3 input provide for the user (btw I'm just a beginner in Python)
The problem is i want to create a button that will save the output in clipboard?
Here is the code:

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()

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

秋意浓 2025-01-18 16:03:44

您可以使用 tkinter 的通用 clipboard_append( ) 小部件方法,这意味着您不需要下载并安装额外的第三方模块,例如 剪贴板。 (请注意,还有一个 clipboard_get() 方法,链接的文档没有提及。)

以下是如何修改代码来执行此操作:我添加了一个 Copy To Clipboard 按钮并定义了一个 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()

屏幕截图

You can do it by using one of tkinter's universal clipboard_append() widget method which means you wouldn't need to download and install an additional third-party module such as clipboard. (Note that there's also a clipboard_get() method that the linked documentation doesn't mention.)

Here's how to modify your code to do it: I've added a Copy To Clipboard button and defined a copy_to_cliboard() function that will be called when it's clicked. The function uses the clipboard_append() method (after first clearing the clipboard via another universal widget method named 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()

Screenshot

雄赳赳气昂昂 2025-01-18 16:03:44

使用剪贴板模块。此代码片段中的示例...

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 安装该模块。

如果这种方法还不够,这里是一些其他方法。

With the clipboard module. Example in this code snippet...

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()

You can also paste strings from the clipboard with cb.paste() and assigning it to a variable. You'll need to install the module from pip.

If this method doesn't suffice, here is some other methods.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文