PyQt5实现使用剪贴板做复制与粘帖示例_粘贴板并不是黏贴图片文件

'''
使用剪贴板


'''

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('./images/30.jpg'))

layout = QGridLayout()
layout.addWidget(textCopyButton,0,0)
layout.addWidget(imageCopyButton,0,1)
layout.addWidget(htmlCopyButton,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)

self.setLayout(layout)

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('剪贴板演示')

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('./images/puke3.gif'))

def pasteImage(self):
clipboard = QApplication.clipboard()
self.imageLabel.setPixmap(clipboard.pixmap())

def copyHtml(self):
mimeData = QMimeData()
mimeData.setHtml('<b>Bold and <font color=red>Red</font></b>')
clipboard = QApplication.clipboard()
clipboard.setMimeData(mimeData)

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_())
posted @ 2021-02-26 15:24  zechariah1  阅读(551)  评论(0编辑  收藏  举报