PyQt5基础学习-QApplication.clipboard(剪切板) 1.clipboard.setText(设置剪切板的文字) 2.clipboard.text(获得剪切板文字) 3.clip

2022/1/31 23:17:17

本文主要是介绍PyQt5基础学习-QApplication.clipboard(剪切板) 1.clipboard.setText(设置剪切板的文字) 2.clipboard.text(获得剪切板文字) 3.clip,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

通过复制和粘贴文本, 复制和粘贴图片, 通过复制和粘贴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_())

 



这篇关于PyQt5基础学习-QApplication.clipboard(剪切板) 1.clipboard.setText(设置剪切板的文字) 2.clipboard.text(获得剪切板文字) 3.clip的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程