The clipboard holds data you copy and paste. You can copy text from one program to another, like copying text from a website into notepad.
We will create a program that has access to the clipboard using PyQt. If we copy text from any program, it will be added into the textarea of the PyQt application.
We can access the clipboard using the class QClipboard. Then we can output that text. We connect the clipboard to a method using the line:
QApplication.clipboard().dataChanged.connect()
Then we grab the contents using
QApplication.clipboard().text()
Try the example below
# PyQt5 clipboard # # Gets text from the system clipboard # If you copy text to the clipboard, # output is shown in the console. # # pythonprogramminglanguage.com #
import sys from PyQt5.Qt import QApplication, QClipboard from PyQt5 import QtCore, QtWidgets from PyQt5.QtWidgets import QMainWindow, QWidget, QPlainTextEdit from PyQt5.QtCore import QSize
# Add text field self.b = QPlainTextEdit(self) self.b.insertPlainText("Use your mouse to copy text to the clipboard.\nText can be copied from any application.\n") self.b.move(10,10) self.b.resize(400,200)