Image copied to clipboard doesn#39;t persist on Linux(复制到剪贴板的图像不会在 Linux 上保留)
问题描述
我正在尝试将图像保存到系统剪贴板,所以我写了一些这样的代码:
I'm trying to save an image to the system clipboard, so I wrote some code like this:
#!/usr/bin/python3
from PyQt5.Qt import QApplication
from PyQt5.QtWidgets import QWidget, QPushButton
from PyQt5.Qt import QImage
import sys
class MyWidget(QWidget):
def __init__(self):
super(MyWidget, self).__init__()
self.button = QPushButton(self)
self.button.clicked.connect(self.copyPicToClip)
def copyPicToClip(self):
image = QImage('./test.jpg')
QApplication.clipboard().setImage(image)
self.close()
if __name__ == '__main__':
a = QApplication(sys.argv)
myW = MyWidget()
myW.show()
a.exec()
遗憾的是,我发现它根本不起作用.然后我试图找到解决方案.我尝试的第一件事是:
Sadly, I found it doesn't work at all. Then I tried to find a solution. The first thing I tried was this:
def copyPicToClip(self):
image = QImage('./test.jpg')
QApplication.clipboard().setImage(image)
# self.close()
在这之后,我才发现它起作用了,但是窗口没有自动关闭.
After this, I just found that it worked, but the window does not close automatically.
然后我尝试复制文本:
#!/usr/bin/python3
from PyQt5.Qt import QApplication, QClipboard
from PyQt5.QtWidgets import QWidget, QPushButton
from PyQt5.Qt import QImage
import sys
class MyWidget(QWidget):
def __init__(self):
super(MyWidget, self).__init__()
self.button = QPushButton(self)
self.button.clicked.connect(self.copyPicToClip)
QApplication.clipboard().dataChanged.connect(self.testFunc)
def copyPicToClip(self):
image = QImage('./test.jpg')
QApplication.clipboard().setImage(image)
def testFunc(self):
print('Here')
self.close()
if __name__ == '__main__':
a = QApplication(sys.argv)
myW = MyWidget()
myW.show()
a.exec()
很遗憾,它又失败了.
所以,如果我提前关闭应用程序,图像将不会保存到剪贴板.但我想在将图像复制到剪贴板后关闭它.
So, it seems that if I close the application to early, the image won't be saved to the clipboard. But I want to close it after copying the image to the clipboard.
有什么建议吗?
(PyQt5,ubuntu 16.10,如果有帮助的话).
(PyQt5, ubuntu 16.10, if helps).
推荐答案
不幸的是,这是 Linux 上的正常"行为.默认情况下,当应用程序关闭时,剪贴板数据不会被保留.此问题的通常解决方法是安装剪贴板管理器.对于 Ubuntu,请参阅此 wiki 文章了解更多详细信息:
Unfortunately for you, this is "normal" behaviour on Linux. By default, clipboard data is not persisted when an application closes. The usual work-around for this problem is to install a clipboard manager. For Ubuntu, see this wiki article for more details:
- Ubuntu Wiki:剪贴板持久性
(注意:我自己并没有实际测试过任何建议的解决方案,所以我不知道它们中的任何一个是否适用于 PyQt).
(NB: I have not actually tested any of the suggested solutions myself, so I don't know whether any of them will work with PyQt).
基本问题是在 Linux 上,剪贴板只存储对底层数据的引用.这在存储方面非常有效,因为只有在客户端程序实际请求数据时才会复制数据.但是当然,如果源应用程序关闭,引用将失效,剪贴板将变为空.
The basic problem is that on Linux, the clipboard only stores a reference to the underlying data. This is very efficient in terms of storage, because the data is only copied when the client program actually requests it. But of course if the source application closes, the reference will be invalidated, and the clipboard will become empty.
这篇关于复制到剪贴板的图像不会在 Linux 上保留的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:复制到剪贴板的图像不会在 Linux 上保留
基础教程推荐
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01
- 线程时出现 msgbox 错误,GUI 块 2022-01-01
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- 用于分类数据的跳跃记号标签 2022-01-01
- 筛选NumPy数组 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01