python3+PyQt5实现使用剪贴板做复制与粘帖示例

yipeiwu_com6年前Python基础

本文是对《Python Qt GUI快速编程》的第10章的例子剪贴板用Python3+PyQt5进行改写,分别对文本,图片和html文本的复制与粘帖,三种做法大同小异。

#!/usr/bin/env python3
import os
import sys
from PyQt5.QtCore import (QMimeData, Qt)
from PyQt5.QtWidgets import (QApplication, QDialog, QGridLayout, QLabel,
    QPushButton)
from PyQt5.QtGui import QPixmap

class Form(QDialog):

  def __init__(self, parent=None):
    super(Form, self).__init__(parent)

    textCopyButton = QPushButton("&Copy Text")
    textPasteButton = QPushButton("Paste &Text")
    htmlCopyButton = QPushButton("C&opy HTML")
    htmlPasteButton = QPushButton("Paste &HTML")
    imageCopyButton = QPushButton("Co&py Image")
    imagePasteButton = QPushButton("Paste &Image")
    self.textLabel = QLabel("Original text")
    self.imageLabel = QLabel()
    self.imageLabel.setPixmap(QPixmap(os.path.join(
        os.path.dirname(__file__), "images/clock.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(imagePasteButton, 1, 1)
    layout.addWidget(htmlPasteButton, 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("Clipboard")


  def copyText(self):
    clipboard = QApplication.clipboard()
    clipboard.setText("I've been clipped!")


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


  def copyImage(self):
    clipboard = QApplication.clipboard()
    clipboard.setPixmap(QPixmap(os.path.join(
        os.path.dirname(__file__), "images/gvim.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)
  form = Form()
  form.show()
  app.exec_()

运行结果:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

python配置文件写入过程详解

python配置文件有.conf,.ini,.txt等多种 python集成的 标准库的 ConfigParser 模块提供一套 API 来读取和操作配置文件 我的配置文件如下 [M...

Python安装模块的常见问题及解决方法

1、error: command ‘x86_64-linux-gnu-gcc' failed with exit status 解决办法: # Python 3 $ sudo apt...

Python采用raw_input读取输入值的方法

本文较为详细的介绍了python中raw_input的用法,使用raw_input 能够很方便的丛控制台读入数据。具体用法示例如下: 1.输入字符串 #13222319810101*...

matplotlib savefig 保存图片大小的实例

在用matplotlib画图时,如果图例比较大,画在图中就会挡着线条,这时可以用以下语句把图例画到图外面: plt.legend(bbox_to_anchor=(1.01, 1),...

python3获取文件中url内容并下载代码实例

这篇文章主要介绍了python3获取文件中url内容并下载代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 #!/usr/b...