gpt4 book ai didi

visual-studio-code - pyglet.canvas.xlib.NoSuchDisplayException : Cannot connect to "None" only happens in VSCode terminal

转载 作者:行者123 更新时间:2023-12-04 13:37:30 39 4
gpt4 key购买 nike

如果我在 VSCode 终端中运行此代码

import pyglet
window = pyglet.window.Window(500, 500)

我得到
Traceback (most recent call last):
File "/home/justin/.local/lib/python3.8/site-packages/pyglet/__init__.py", line 334, in __getattr__
return getattr(self._module, name)
AttributeError: 'NoneType' object has no attribute 'Window'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/home/justin/Dropbox/jr/py/pyglet_games/pyglet_demo/displaytest.py", line 5, in <module>
window = pyglet.window.Window(500, 500)
File "/home/justin/.local/lib/python3.8/site-packages/pyglet/__init__.py", line 340, in __getattr__
__import__(import_name)
File "/home/justin/.local/lib/python3.8/site-packages/pyglet/window/__init__.py", line 1891, in <module>
gl._create_shadow_window()
File "/home/justin/.local/lib/python3.8/site-packages/pyglet/gl/__init__.py", line 220, in _create_shadow_window
_shadow_window = Window(width=1, height=1, visible=False)
File "/home/justin/.local/lib/python3.8/site-packages/pyglet/window/xlib/__init__.py", line 171, in __init__
super(XlibWindow, self).__init__(*args, **kwargs)
File "/home/justin/.local/lib/python3.8/site-packages/pyglet/window/__init__.py", line 573, in __init__
display = pyglet.canvas.get_display()
File "/home/justin/.local/lib/python3.8/site-packages/pyglet/canvas/__init__.py", line 94, in get_display
return Display()
File "/home/justin/.local/lib/python3.8/site-packages/pyglet/canvas/xlib.py", line 123, in __init__
raise NoSuchDisplayException('Cannot connect to "%s"' % name)
pyglet.canvas.xlib.NoSuchDisplayException: Cannot connect to "None"

但是,如果我在命令行中运行它,则会按预期出现一个窗口。如果我在我用来教的 Thonny 中运行它,它也能按预期工作。我已经验证 sys.version 和 sys.path 在终端或 VSCode 中运行时是相同的。

我已经搜索了这个问题的解决方案,但在存在的少数其他情况下,它并不是以一种方式运行而不是另一种方式运行的情况。大多数解决方案都涉及更改显卡设置或运行 X11 服务器。这似乎与此处无关,因为它似乎仅与 VSCode 相关。

谢谢。

最佳答案

我遇到了同样的问题,不仅仅是 pyglet但是任何试图打开 window 的东西。
看起来是vs code的内部终端有问题。设置 consolelaunch.json 中的替代方案为我修好了。

{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "externalTerminal"
}
]
}
因此,确实,将以下内容添加到我的 python 脚本中可以对问题进行排序(至少与切换到 Wayland 相结合):
import os
os.environ['DISPLAY'] = ':1'
有谁知道如何将其设置为永久设置,以便在我编写的需要输出到显示器的每个模块中都不需要它?

关于visual-studio-code - pyglet.canvas.xlib.NoSuchDisplayException : Cannot connect to "None" only happens in VSCode terminal,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60922076/

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