I need to "Copy" the return of a Label in tkinter Python

Question:

I have a question where I have a python code where I make a request in an API, as well as request data via tkinter Entry, however I need the result it generates for the user to "Copy".

My code:

from cgitb import text
from os import link
from tokenize import String
from turtle import back
import requests
from tkinter import *


def buscaBoleto():
    boleto = vboleto.get()
    token = vtoken.get()

    url = "www.teste.com.br" + boleto + "?api_token=" + token
    headers = {"Accept": "application/json"}
    response = requests.get(url, headers=headers)
    start = '"secure_url":"'
    end = '","customer_id'
    s = response.text
    #link = (s.split(start))[1].split(end)[0]
    link = 'www.teste.com.br/12345678'

    if s == '{"errors":"Unauthorized"}':
        resposta["text"] = str("Não Autorizado, tente novamente")
    else:
        resposta["text"] = link


janela = Tk()

janela.title("Visualizador de boletos")
janela.geometry("400x300")
janela.configure(background="#dde")

Label(janela,text="Insira ID:",background="#dde",foreground="#009",anchor=W).place(x=10,y=20, width=200, height=20)
vboleto=Entry(janela)
vboleto.place(x=10,y=40, width=200, height=20)

Label(janela,text="Insira o token:",background="#dde",foreground="#009",anchor=W).place(x=10,y=80, width=200, height=20)
vtoken=Entry(janela)
vtoken.place(x=10,y=100, width=200, height=20)

Button(janela, text="Gerar", command=buscaBoleto).place(x=10, y=140, width=100, height=20)


resposta = Label(janela,text="",background="#dde",foreground="#009",state="normal",anchor=W)
resposta.place(x=10,y=200, width=400, height=20)



janela = mainloop()

Part of the code where I pull the variable "link" to the text field of the label.
I need a way so that my "link" result can be copied on the Tkinter screen

    if s == '{"errors":"Unauthorized"}':
        resposta["text"] = str("Não Autorizado, tente novamente")
    else:
        resposta["text"] = link

resposta = Label(janela,text="",background="#dde",foreground="#009",state="normal",anchor=W)
resposta.place(x=10,y=200, width=400, height=20)

Is there a better way that I can "Copy" this result generated on the tkinter screen?

Attached image of what I need.my result

Asked By: William Kliemann

||

Answers:

Try this:

def copy():
    janela.clipboard_clear()
    janela.clipboard_append(resposta["text"])
    janela.update()
 
Button(janela, text="Copiar o link", command=copy).place(x=10, y=220, width=100, height=20)
Answered By: Manoel Neto
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.