我使用 python 2.7。我使用 sudo pip install pyperclip
安装了 pyperclip,它安装成功。
每次我使用下面的简单代码
import pyperclip
pyperclip.copy('Hello World')
message=pyperclip.paste()
print (message)
我收到以下错误:
/usr/lib/python2.7/dist-packages/gtk-2.0/gtk/__init__.py:57:
GtkWarning: could not open display warnings.warn(str(e),
_gtk.Warning) /usr/local/lib/python2.7/dist-packages/pyperclip/__init__.py:102:
GtkWarning: IA__gtk_clipboard_get_for_display: assertion 'display !=
NULL' failed cb = gtk.Clipboard() Traceback (most recent call last):
File "first.py", line 401, in <module>
pyperclip.copy('Hello World') File "/usr/local/lib/python2.7/dist-packages/pyperclip/__init__.py", line
102, in _copyGtk
cb = gtk.Clipboard() RuntimeError: could not create GtkClipboard object
最佳答案
阅读文档总是有帮助的。
On Windows, no additional modules are needed. On Mac, the module uses pbcopy and pbpaste, which should come with the os. On Linux, install xclip or xsel via package manager. For example, in Debian: sudo apt-get install xclip
Otherwise on Linux, you will need the gtk or PyQt4 modules installed.
gtk and PyQt4 modules are not available for Python 3, and this module does not work with PyGObject yet.
我可以从您的帖子中看出您使用的是基于 unix 的操作系统。因此,您需要在终端上做的就是 sudo apt install xclip
,然后通过 pip 安装 gtk 和 PyQt4 模块(因为您使用的是 python 2)。
关于python - Pyperclip 报错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32163481/