gpt4 book ai didi

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

转载 作者:qq735679552 更新时间:2022-09-27 22:32:09 27 4
gpt4 key购买 nike

CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.

这篇CFSDN的博客文章python3+PyQt5实现使用剪贴板做复制与粘帖示例由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/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_()

运行结果:

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我.

原文链接:http://blog.csdn.net/xiaoyangyang20/article/details/54706539 。

最后此篇关于python3+PyQt5实现使用剪贴板做复制与粘帖示例的文章就讲到这里了,如果你想了解更多关于python3+PyQt5实现使用剪贴板做复制与粘帖示例的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com