How do I paste the copied text from the keyboard in Python?

TkinterPythonGUI-Programming

Python offers many built-in libraries and modules which provides a way to implement the additional features in developing various python applications. pyperclip is one of the cross-platform python modules to implement the copyandpaste operation in any Python application. To use it in Python application, you've to install it using the following command,

pip install pyperclip

The practical use-case can be implemented by developing an application which copies the text from the clipboard and displays on the screen. Additionally, we can also display the copied text in an Entry widget or Text widget which accepts the user input in the form of text.

Example

Let us understand this with an example.

# Import required libraries
from tkinter import *
import pyperclip

# Create an instance of tkinter frame or window
win = Tk()

# Set the size of the window
win.geometry("700x350")

# Create a text widget
my_clip= Text(win, height=15)
my_clip.pack()

def update_text():
   global my_clip
   my_clip.insert(END,pyperclip.paste())

# Create a button to paste the copied text from clipboard
button=Button(win, text= "Paste Here", command=update_text)
button.pack()

win.mainloop()

Output

If you run the above code snippet, it will display a window with a button and a text editor where the copied text is pasted and displayed.

raja
Updated on 22-Dec-2021 12:56:37

Advertisements