cerate a lable in tkinter that can be copied in python

To create a label in tkinter that can be copied, you can use the Label widget and bind it with a function that copies the text to clipboard. Here's an example code:

main.py
import tkinter as tk
import pyperclip

# function to copy text to clipboard
def copy_text(event):
    text = event.widget['text']
    pyperclip.copy(text)

# create tkinter window
root = tk.Tk()

# create label widget
label_text = "This text can be copied"
label = tk.Label(root, text=label_text)

# bind the label with the copy_text function
label.bind("<Button-1>", copy_text)

# pack the label in the window
label.pack()

# run the window
root.mainloop()
458 chars
24 lines

In this code, we import the pyperclip module to copy the text to clipboard. Then we create a tkinter window and a label widget with the text "This text can be copied". Next, we bind the label with the copy_text function which extracts the text from the widget and copies it to clipboard. Finally, we pack the label in the window and run the window.

gistlibby LogSnag