TouFa7
TouFa7

Reputation: 53

A button that copy the output into a clipboard using Tkinter

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

Upvotes: 2

Views: 2361

Answers (2)

martineau
martineau

Reputation: 123393

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

Upvotes: 5

TheEngineerGuy
TheEngineerGuy

Reputation: 228

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.

Upvotes: 1

Related Questions