<bdo id='0bjiq'></bdo><ul id='0bjiq'></ul>
  • <tfoot id='0bjiq'></tfoot>
    <i id='0bjiq'><tr id='0bjiq'><dt id='0bjiq'><q id='0bjiq'><span id='0bjiq'><b id='0bjiq'><form id='0bjiq'><ins id='0bjiq'></ins><ul id='0bjiq'></ul><sub id='0bjiq'></sub></form><legend id='0bjiq'></legend><bdo id='0bjiq'><pre id='0bjiq'><center id='0bjiq'></center></pre></bdo></b><th id='0bjiq'></th></span></q></dt></tr></i><div id='0bjiq'><tfoot id='0bjiq'></tfoot><dl id='0bjiq'><fieldset id='0bjiq'></fieldset></dl></div>
    1. <small id='0bjiq'></small><noframes id='0bjiq'>

        <legend id='0bjiq'><style id='0bjiq'><dir id='0bjiq'><q id='0bjiq'></q></dir></style></legend>

        复制到剪贴板的图像不会在 Linux 上保留

        时间:2023-08-04
          <tbody id='ETn4s'></tbody>

            <small id='ETn4s'></small><noframes id='ETn4s'>

            <legend id='ETn4s'><style id='ETn4s'><dir id='ETn4s'><q id='ETn4s'></q></dir></style></legend>
              <bdo id='ETn4s'></bdo><ul id='ETn4s'></ul>

              <tfoot id='ETn4s'></tfoot>
            • <i id='ETn4s'><tr id='ETn4s'><dt id='ETn4s'><q id='ETn4s'><span id='ETn4s'><b id='ETn4s'><form id='ETn4s'><ins id='ETn4s'></ins><ul id='ETn4s'></ul><sub id='ETn4s'></sub></form><legend id='ETn4s'></legend><bdo id='ETn4s'><pre id='ETn4s'><center id='ETn4s'></center></pre></bdo></b><th id='ETn4s'></th></span></q></dt></tr></i><div id='ETn4s'><tfoot id='ETn4s'></tfoot><dl id='ETn4s'><fieldset id='ETn4s'></fieldset></dl></div>

                  本文介绍了复制到剪贴板的图像不会在 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 上保留的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:在 PyQt5 中,如何将普通的 QLineEdit(文本框)转换为 下一篇:将信号连接到插槽立即导致信号被发射

                  相关文章

                  最新文章

                  <tfoot id='Op2gu'></tfoot>
                • <i id='Op2gu'><tr id='Op2gu'><dt id='Op2gu'><q id='Op2gu'><span id='Op2gu'><b id='Op2gu'><form id='Op2gu'><ins id='Op2gu'></ins><ul id='Op2gu'></ul><sub id='Op2gu'></sub></form><legend id='Op2gu'></legend><bdo id='Op2gu'><pre id='Op2gu'><center id='Op2gu'></center></pre></bdo></b><th id='Op2gu'></th></span></q></dt></tr></i><div id='Op2gu'><tfoot id='Op2gu'></tfoot><dl id='Op2gu'><fieldset id='Op2gu'></fieldset></dl></div>
                    <legend id='Op2gu'><style id='Op2gu'><dir id='Op2gu'><q id='Op2gu'></q></dir></style></legend>

                      <small id='Op2gu'></small><noframes id='Op2gu'>

                        <bdo id='Op2gu'></bdo><ul id='Op2gu'></ul>