gpt4 book ai didi

linux - QClipboard 在 GNU/Linux 下工作很有趣

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:28:52 24 4
gpt4 key购买 nike

#!/usr/bin/python
from PyQt4.QtGui import QApplication, QClipboard
import sys

app = QApplication(sys.argv)
QApplication.clipboard().setText('yo', mode=QClipboard.Clipboard)
input() #wait for input
  1. 当我设置 mode=QClipboard.Clipboard(默认设置)时,它不起作用。它将旧数据留在剪贴板和选择剪贴板中。
  2. 当我将其更改为 mode=QClipboard.Selection(特定于 X 的那个)时,它用 yo 替换了both 选择和主剪贴板>.

问题:为什么“主要的东西”(mode 毕竟默认为 QClipboard.Clipboard)不起作用,而应该只在条件下起作用的东西(QClipboard.supportsSelection()) 做这个工作?我怎样才能使它正常工作?

最佳答案

我实际上无法在我的 Linux 系统上重现该问题:它对我来说一切正常。

然而,docs说剪贴板需要 X11 上的事件循环:

the X11 clipboard is event driven, i.e. the clipboard will not function properly if the event loop is not running. Similarly, it is recommended that the contents of the clipboard are stored or retrieved in direct response to user-input events, e.g. mouse button or key presses and releases. You should not store or retrieve the clipboard contents in response to timer or non-user-input events.

因此,您可以通过强制处理这样的排队事件来让您的示例在您的系统上运行:

app.clipboard().setText('yo')
app.processEvents()
input()

显然这是一个人为的“解决方案”,不过,正确的做法是启动事件循环并遵循文档中的建议。

关于linux - QClipboard 在 GNU/Linux 下工作很有趣,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30426115/

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