其他分享
首页 > 其他分享> > PyQT5 (二十九) 关于剪贴板使用 的案例

PyQT5 (二十九) 关于剪贴板使用 的案例

作者:互联网

 关于剪贴板使用 的案例:

import sys
from PyQt5.QtCore import Qt, QMimeData
from PyQt5.QtGui import QIcon, QPainter, QBrush, QPixmap
from PyQt5.QtWidgets import QApplication, QWidget, QComboBox, QFormLayout, QLabel, QLineEdit, QPushButton, QGridLayout

'''
关于剪贴板使用 的案例
'''


class clipboardDemo(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        # 设置定位和左上角坐标
        self.setGeometry(300, 300, 350, 220)
        # 设置窗口标题
        self.setWindowTitle('关于剪贴板使用 的演示')
        # 设置窗口图标
        # self.setWindowIcon(QIcon('../web.ico'))
        textCopyButton = QPushButton('复制文本')
        textPasteButton = QPushButton('粘贴文本')

        htmlCopyButton = QPushButton('复制HTML')
        htmlPasteButton = QPushButton('粘贴HTML')

        imageCopyButton = QPushButton('复制图像')
        imagePasteButton = QPushButton('粘贴图像')

        self.textLabel = QLabel('默认文本')
        self.imageLable = QLabel()
        # self.imageLable.setPixmap(QPixmap('../web.ico'))

        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.imageLable, 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)

    def copyText(self):
        clipboard = QApplication.clipboard()
        clipboard.setText('你好 PYQT5')

    def pasteText(self):
        clipboard = QApplication.clipboard()
        self.textLabel.setText(clipboard.text())

    def copyImage(self):
        clipboard = QApplication.clipboard()
        clipboard.setPixmap(QPixmap('../web.ico'))

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

    def copyHtml(self):
        mimeData = QMimeData()
        mimeData.setHtml('<b> 粗体 黑色 和<font color=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)
    # 设置应用图标
    app.setWindowIcon(QIcon('../web.ico'))
    w = clipboardDemo()
    w.show()
    sys.exit(app.exec_())

标签:剪贴板,layout,self,QApplication,PyQT5,二十九,clipboard,addWidget,def
来源: https://blog.csdn.net/gixome/article/details/120890953