Copy from clipboard using Python and Tkinter


To copy from clipboard, we can use the clipboard_get() method of Tkinter. Let's take an example and see how to get the data from the clipboard and display it on a Tkinter window.

Steps −

  • Import the tkinter library and create an instance of tkinter frame.

  • Set the size of the frame using geometry method.

  • Next, call clipboard_get() to get the text from the clipboard and store the data in a variable "cliptext".

  • Create a label to the display the clipboard text. Pass cliptext as text, "text=cliptext".

  • Finally, run the mainloop of the application window.

Example

# Import the tkinter library
from tkinter import *

# Instance of tkinter canvas
win = Tk()
win.geometry("700x250")
win.title("Data from Clipboard")

# Get the data from the clipboard
cliptext = win.clipboard_get()

# Label to print clipboard text
lab=Label(win, text = cliptext, font=("Calibri",15,"bold"))
lab.pack(padx=20, pady=50)

# Run the mainloop
win.mainloop()

Output

It will produce the following output −

It will display the contents of the clipboard on the window.

Updated on: 26-Oct-2021

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements