复制到剪贴板的图像在Linux上不持久 [英] Image copied to clipboard doesn't persist on Linux

查看:147
本文介绍了复制到剪贴板的图像在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()

不幸的是,它再次失败。

Sadly, it failed again.

所以,它似乎,如果我尽早关闭该应用程序,该图像将不会保存到剪贴板。但是我想在将图像复制到剪贴板后关闭它。

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: Clipboard Persistence

(注意:我还没有实际测试过

(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上不持久的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆