python3+PyQt5实现使用剪贴板做复制与粘帖示例
本文是对《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 heapq使用详解及实例代码
Pythonheapq详解Python有一个内置的模块,heapq标准的封装了最小堆的算法实现。下面看两个不错的应用。小顶堆(求TopK大)话说需求是这样的:定长的序列
在Django同1个页面中的多表单处理详解
快速上手Django实现项目近期公司在做1个海淘的项目,APP为pylot。由于时间比较赶,加上隔壁那哥们不在,只能自己挑大梁了。结果,当项目做出来之后,被领导
Python下的Softmax回归函数的实现方法(推荐)
Softmax回归函数是用于将分类结果归一化。但它不同于一般的按照比例归一化的方法,它通过对数变换来进行归一化,这样实现了较大的值在归一化过程
编辑:编程语言
标签:详解,项目,函数,定长,文本