python3+PyQt5實(shí)現(xiàn)使用剪貼板做復(fù)制與粘帖示例
本文是對(duì)《Python Qt GUI快速編程》的第10章的例子剪貼板用Python3+PyQt5進(jìn)行改寫(xiě),分別對(duì)文本,圖片和html文本的復(fù)制與粘帖,三種做法大同小異。
#!/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_()
運(yùn)行結(jié)果:

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python?UnicodedecodeError編碼問(wèn)題解決方法匯總
本文主要介紹了Python?UnicodedecodeError編碼問(wèn)題解決方法匯總,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08
tensorflow saver 保存和恢復(fù)指定 tensor的實(shí)例講解
今天小編就為大家分享一篇tensorflow saver 保存和恢復(fù)指定 tensor的實(shí)例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-07-07
Python中實(shí)現(xiàn)輸入超時(shí)及如何通過(guò)變量獲取變量名
這篇文章主要介紹了Python中實(shí)現(xiàn)輸入超時(shí)以及通過(guò)變量獲取變量的名字,本文給大家分享了解決思路主要是通過(guò)多線程法實(shí)現(xiàn),需要的朋友可以參考下2020-01-01
python如何實(shí)現(xiàn)convolution neural network卷積神經(jīng)網(wǎng)絡(luò)算法
卷積神經(jīng)網(wǎng)絡(luò)(CNN)是深度學(xué)習(xí)中重要的算法之一,主要應(yīng)用于圖像識(shí)別和處理領(lǐng)域,其基本原理是模擬人類視覺(jué)系統(tǒng),通過(guò)卷積層、激活函數(shù)和池化層等組件提取圖像的特征,并通過(guò)全連接層進(jìn)行分類或其他任務(wù),CNN訓(xùn)練過(guò)程中使用大量標(biāo)記圖像數(shù)據(jù)2024-10-10
Python實(shí)現(xiàn)簡(jiǎn)單求解給定整數(shù)的質(zhì)因數(shù)算法示例
這篇文章主要介紹了Python實(shí)現(xiàn)簡(jiǎn)單求解給定整數(shù)的質(zhì)因數(shù)算法,結(jié)合實(shí)例形式分析了Python正整數(shù)分解質(zhì)因數(shù)的相關(guān)操作技巧,需要的朋友可以參考下2018-03-03
python數(shù)據(jù)解析BeautifulSoup爬取三國(guó)演義章節(jié)示例
這篇文章主要介紹了python數(shù)據(jù)解析BeautifulSoup爬取三國(guó)演義章節(jié)示例,文中附含詳細(xì)示例代碼,有需要的朋友可以借鑒參考下,希望能夠有所幫助2021-09-09
Python動(dòng)態(tài)屬性與反射機(jī)制方式
深入探索Python中的反射機(jī)制和動(dòng)態(tài)屬性的細(xì)節(jié),對(duì)我們來(lái)說(shuō)是編寫(xiě)具有適應(yīng)性和高可擴(kuò)展性程序的關(guān)鍵,本篇文章旨在通過(guò)詳盡的概念介紹和精心設(shè)計(jì)的代碼示例,加強(qiáng)您對(duì)這些核心概念的把握,并助您在實(shí)踐中運(yùn)用自如2024-06-06

