简体   繁体   English

PySide剪贴板错误-对象没有属性.setText

[英]PySide clipboard error - object has no attribute .setText

I'm using PyCharm to write my code (Python 3.4). 我正在使用PyCharm编写代码(Python 3.4)。
For some reason the following code returns the error: AttributeError: 'NoneType' object has no attribute 'setText' 由于某些原因,以下代码返回错误:AttributeError:'NoneType'对象没有属性'setText'

The code I'm writing will be run inside a program called Nuke. 我正在编写的代码将在名为Nuke的程序中运行。 I've tested this code inside there and it seems to run fine. 我已经在其中测试了此代码,并且看起来运行良好。 Copying things to and viewing the contents of the system clipboard. 将内容复制到系统剪贴板并查看其内容。 From most of my googling it seems this is supposed to work. 从我的大部分谷歌搜索看来,这应该可行。 I haven't yet found any alternatives :( 我还没有找到任何替代方法:(

It would be helpful if I could get this working in PyCharm! 如果我可以在PyCharm中使用它,将会很有帮助!

from PySide import QtGui

cb = QtGui.QApplication.clipboard()

cb.setText("Yay") # set clipboard
print (cb.text()) # show current clipboard contents

Thanks in advance! 提前致谢!

According to the doc : 根据文档

The PySide.QtGui.QApplication object should already be constructed before accessing the clipboard. 在访问剪贴板之前,应该已经构造了PySide.QtGui.QApplication对象。

The following code will work: 以下代码将起作用:

from PySide import QtGui
import sys

#construct app
app=QtGui.QApplication(sys.argv)

#then get the clipboard
cb = QtGui.QApplication.clipboard()

cb.setText("Yay") # set clipboard
print (cb.text())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM