python PyQt 剪贴板不会复制到系统剪贴板
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1073550/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PyQt clipboard doesn't copy to system clipboard
提问by hasen
The following snippet of code doesn't seem to affect the system clipboard at all:
以下代码片段似乎根本不会影响系统剪贴板:
clipboard = QtGui.QApplication.clipboard()
clipboard.setText(text)
According to the Qt documentation, this is how you copy text to clipboard,
根据 Qt 文档,这是将文本复制到剪贴板的方式,
Why isn't it working?
为什么它不起作用?
Googling turned thisup.
谷歌搜索发现了这一点。
It suggests adding this after the above code:
它建议在上面的代码之后添加这个:
event = QtCore.QEvent(QtCore.QEvent.Clipboard)
app.sendEvent(clipboard, event)
But this one behaves odd: it only copies the text to the clipboard after the program exits. Plus, some people in that link reported that this doesn't work with linux.
但是这个行为很奇怪:它只在程序退出后将文本复制到剪贴板。另外,该链接中的一些人报告说这不适用于 linux。
UPDATE:
更新:
Nevermind, I was doing something wrong else where, instead of binding the copy slot to the copy button, I connected it to the "quit" button.
没关系,我在其他地方做错了,我没有将复制槽绑定到复制按钮,而是将它连接到“退出”按钮。
回答by Zv_oDD
回答by Jason Coon
I know you are not using Windows, but maybe this will give you some ideas... I used this in a PyQt program to copy URLs to the clipboard:
我知道你没有使用 Windows,但也许这会给你一些想法......我在 PyQt 程序中使用它来将 URL 复制到剪贴板:
import win32clipboard
s = 'copy this to the clipboard'
try:
win32clipboard.OpenClipboard()
win32clipboard.EmptyClipboard()
win32clipboard.SetClipboardText(s)
win32clipboard.CloseClipboard()
except:
print 'Could not copy clipboard data.'
回答by tgray
You might try gtk.Clipboardfrom PyGTK. I believe it is multi-platform.
你可以尝试gtk.Clipboard从PyGTK的。我相信它是多平台的。
This might be part of the reason you're having trouble with PyQt's QClipboardobject:
这可能是您在使用 PyQt 的QClipboard对象时遇到问题的部分原因:
QClipboard QApplication.clipboard ()
Returns a pointer to the application global clipboard.
Note: The QApplication object should already be constructed before accessing the clipboard.
QClipboard QApplication.clipboard()
返回指向应用程序全局剪贴板的指针。
注意:在访问剪贴板之前应该已经构建了 QApplication 对象。
It's pointing to the applicationclipboard, not the system clipboard. You'll probably have to use something other than the QClipboard object to achieve your end.
它指向应用程序剪贴板,而不是系统剪贴板。您可能必须使用 QClipboard 对象以外的其他东西来实现您的目的。
Edit:
编辑:
The above conclusion from the cited documentation is incorrect. According to the actual PyQt documentation of the QClipboardobject:
上述引用文档的结论是不正确的。根据QClipboard对象的实际PyQt 文档:
The QClipboard class provides access to the window system clipboard.
QClipboard 类提供对窗口系统剪贴板的访问。