問(wèn)題描述
我正在嘗試將圖像保存到系統(tǒng)剪貼板,所以我寫(xiě)了一些這樣的代碼:
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()
遺憾的是,我發(fā)現(xiàn)它根本不起作用.然后我試圖找到解決方案.我嘗試的第一件事是:
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()
在這之后,我才發(fā)現(xiàn)它起作用了,但是窗口沒(méi)有自動(dòng)關(guān)閉.
After this, I just found that it worked, but the window does not close automatically.
然后我嘗試復(fù)制文本:
#!/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()
很遺憾,它又失敗了.
所以,如果我提前關(guān)閉應(yīng)用程序,圖像將不會(huì)保存到剪貼板.但我想在將圖像復(fù)制到剪貼板后關(guān)閉它.
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 上的正常"行為.默認(rèn)情況下,當(dāng)應(yīng)用程序關(guān)閉時(shí),剪貼板數(shù)據(jù)不會(huì)被保留.此問(wèn)題的通常解決方法是安裝剪貼板管理器.對(duì)于 Ubuntu,請(qǐng)參閱此 wiki 文章了解更多詳細(xì)信息:
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:剪貼板持久性
(注意:我自己并沒(méi)有實(shí)際測(cè)試過(guò)任何建議的解決方案,所以我不知道它們中的任何一個(gè)是否適用于 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).
基本問(wèn)題是在 Linux 上,剪貼板只存儲(chǔ)對(duì)底層數(shù)據(jù)的引用.這在存儲(chǔ)方面非常有效,因?yàn)橹挥性诳蛻舳顺绦驅(qū)嶋H請(qǐng)求數(shù)據(jù)時(shí)才會(huì)復(fù)制數(shù)據(jù).但是當(dāng)然,如??果源應(yīng)用程序關(guān)閉,引用將失效,剪貼板將變?yōu)榭?
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.
這篇關(guān)于復(fù)制到剪貼板的圖像不會(huì)在 Linux 上保留的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!