gpt4 book ai didi

python - 如何修复 KeyError : 'DISPLAY' in Python

转载 作者:行者123 更新时间:2023-12-05 05:17:23 26 4
gpt4 key购买 nike

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

>>> 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>})

最佳答案

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

>>> with mss(display=':0') as sct:
... sct.shot()

关于python - 如何修复 KeyError : 'DISPLAY' in Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49308345/

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