使用 Tkinter 将输出复制到剪贴板的按钮
我刚刚使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用
tkinter
的通用clipboard_append( )
小部件方法,这意味着您不需要下载并安装额外的第三方模块,例如剪贴板
。 (请注意,还有一个clipboard_get()
方法,链接的文档没有提及。)以下是如何修改代码来执行此操作:我添加了一个 Copy To Clipboard 按钮并定义了一个
copy_to_cliboard()
函数,单击该按钮时将调用该函数。该函数使用clipboard_append()
方法(首先通过另一个名为clipboard_clear()
的通用小部件方法清除剪贴板之后)。You can do it by using one of
tkinter
's universalclipboard_append()
widget method which means you wouldn't need to download and install an additional third-party module such asclipboard
. (Note that there's also aclipboard_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 theclipboard_append()
method (after first clearing the clipboard via another universal widget method namedclipboard_clear()
).使用
剪贴板
模块。此代码片段中的示例...您还可以使用
cb.paste()
从剪贴板粘贴字符串并将其分配给变量。您需要从 pip 安装该模块。如果这种方法还不够,这里是一些其他方法。
With the
clipboard
module. Example in this code snippet...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.