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
CodePudding user response:
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.