既然楼主说换其他库也行,那就直接用pyqt库就好了
pyqt库里面的接口天然支持剪切板与图片的各种写入和读取
QMimeData类提供一个容器,用于数据记录有关它的MIME类型的信息。
QMimeData是用来描述可以存储在信息clipboard,并经由传输drag and drop机制。 QMimeData对象,它们与相应的MIME类型保存数据关联起来,以确保信息可以在应用程序之间进行安全转移,并在同一应用程序内各地复制。
对于最常见的MIME类型, QMimeData提供方便的功能来访问数据:
- Tester
- hasText()
- hasHtml()
- hasUrls()
- hasImage()
- hasColor()
- Getter
- text()
- html()
- urls()
- imageData()
- colorData()
- Setter
- setText()
- setHtml()
- setUrls()
- setImageData()
- setColorData()
- MIME Types
- text/plain
- text/html
- text/uri-list
- image/ *
- application/x-color
关于自定义类型
在Windows上,formats()
也将返回的MIME数据可自定义格式,使用x-qt-windows-mime
亚型,表明它们代表了非标准格式的数据。该格式将采取以下形式:
application/x-qt-windows-mime;value="<custom type>"
比如我在vscode复制后,调用formats()
显示如下:(换行是我自己加的)
['application/x-qt-windows-mime;value="Shell IDList Array"',
'application/x-qt-windows-mime;value="DataObjectAttributes"',
'application/x-qt-windows-mime;value="DataObjectAttributesRequiringElevation"',
'application/x-qt-windows-mime;value="Shell Object Offsets"',
'application/x-qt-windows-mime;value="Preferred DropEffect"',
'application/x-qt-windows-mime;value="AsyncFlag"',
'text/uri-list',
'application/x-qt-windows-mime;value="FileName"',
'application/x-qt-windows-mime;value="FileContents"',
'application/x-qt-windows-mime;value="FileNameW"',
'application/x-qt-windows-mime;value="FileGroupDescriptorW"']
样例测试,(测试方法就是,先复制一个东西,然后运行这段代码,就可以清楚的看到剪切板里面有什么内容了)
import sys
from PyQt5.QtWidgets import QApplication
from PyQt5.QtGui import QClipboard
app = QApplication(sys.argv)
cb = QApplication.clipboard()
print(cb.mimeData(QClipboard.Mode.Clipboard).formats())
if cb.mimeData().hasColor():
print("hasColor")
if cb.mimeData().hasImage():
print("hasImage")
# qt_img = cb.image()
qt_img = cb.mimeData().imageData()
print(qt_img)
if cb.mimeData().hasHtml():
print("hasHtml")
print(cb.mimeData().html())
if cb.mimeData().hasText():
print("hasText")
print(cb.mimeData().text())
# print(cb.text())
if cb.mimeData().hasUrls():
print("hasUrls")
print(cb.mimeData().urls())
其他接口可以看官方文档
至于如何复制一个图片进剪切板,我给你一个代码参考,可以直接运行的(代码里面有一段读取./images/python.png位置的图片需要你自己修改位置)
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/python.png'))
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/python.png'))
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_())