3

我在 Python 中使用 mss 库来截取屏幕截图并将其保存在当前路径上。它使用 PyCharm 在我的 Mac 上完美运行。但是当我尝试在 Ubuntu 16 上运行相同的东西时,我得到一个错误,即使我完全按照文档所说的那样做:

>>> from mss import mss
>>> with mss() as sct:
...     sct.shot()

但我收到此错误,如何解决?

Traceback (most recent call last):
  File "/usr/local/lib/python3.5/dist-packages/mss/linux.py", line 132, in __init__
    display = os.environ['DISPLAY'].encode('utf-8')
  File "/usr/lib/python3.5/os.py", line 725, in __getitem__
    raise KeyError(key) from None
KeyError: 'DISPLAY'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.5/dist-packages/mss/factory.py", line 34, in mss
    return MSS(**kwargs)
  File "/usr/local/lib/python3.5/dist-packages/mss/linux.py", line 134, in __init__
    raise ScreenShotError('$DISPLAY not set.', locals())
mss.exception.ScreenShotError: ('$DISPLAY not set.', {'display': None, 'self': <mss.linux.MSS object at 0x7f06ce881d30>})
4

1 回答 1

2

实际上,在 GNU/Linux 上,您必须DISPLAY设置 envar。如果不是,这并不常见,您可以尝试设置display关键字参数,例如:

>>> with mss(display=':0') as sct:
...     sct.shot()
于 2018-05-07T09:38:00.603 回答