通过复制和粘贴文本, 复制和粘贴图片, 通过复制和粘贴html来演示剪切板的功能
""" 使用剪切板 """ import sys, math from PyQt5.QtWidgets import * from PyQt5.QtGui import * from PyQt5.QtCore import * class ClipBoard(QDialog): def __init__(self): super(ClipBoard, self).__init__() textCopyButton = QPushButton("复制文本") textPasteButton = QPushButton("粘贴文本") htmlCopyButton = QPushButton("复制HTML") htmlPasteButton = QPushButton("粘贴HTML") imageCopyButton = QPushButton("复制图像") imagePasteButton = QPushButton("粘贴图像") self.textLabel = QLabel("默认文本") self.imageLabel = QLabel() #self.imageLabel.setPixmap(QPixmap("D:\PyQt5_Study\picture\F6fhJr.jpg")) layout = QGridLayout() layout.addWidget(textCopyButton, 0, 0) layout.addWidget(htmlCopyButton, 0, 1) layout.addWidget(imageCopyButton, 0, 2) layout.addWidget(textPasteButton, 1, 0) layout.addWidget(htmlPasteButton, 1, 1) layout.addWidget(imagePasteButton, 1, 2) layout.addWidget(self.textLabel, 2, 0, 1, 2) layout.addWidget(self.imageLabel, 2, 2) textCopyButton.clicked.connect(self.copyText) textPasteButton.clicked.connect(self.pasteText) htmlCopyButton.clicked.connect(self.copyHtml) htmlPasteButton.clicked.connect(self.pasteHtml) imageCopyButton.clicked.connect(self.copyImage) imagePasteButton.clicked.connect(self.pasteImage) self.setWindowTitle("剪切板演示") self.setLayout(layout) #复制文本 def copyText(self): clipboard = QApplication.clipboard() clipboard.setText("hello world") #粘贴文本 def pasteText(self): clipboard = QApplication.clipboard() self.textLabel.setText(clipboard.text()) #复制图片 def copyImage(self): clipboard = QApplication.clipboard() clipboard.setPixmap(QPixmap("D:\PyQt5_Study\picture\F6fhJr.jpg")) #粘贴图片 def pasteImage(self): clipboard = QApplication.clipboard() self.imageLabel.setPixmap(clipboard.pixmap()) #复制html def copyHtml(self): #构造html的类 mimeData = QMimeData() mimeData.setHtml("<b> Bold and <font color = red>Red</font></b>") clipboard = QApplication.clipboard() clipboard.setMimeData(mimeData) #粘贴html def pasteHtml(self): clipboard = QApplication.clipboard() mimeData = clipboard.mimeData() if mimeData.hasHtml(): self.textLabel.setText(mimeData.html()) if __name__ == "__main__": app = QApplication(sys.argv) main = ClipBoard() main.show() sys.exit(app.exec_())