验证码: 看不清楚,换一张 查询 注册会员,免验证
  • {{ basic.site_slogan }}
  • 打开微信扫一扫,
    您还可以在这里找到我们哟

    关注我们

AttributeError: ‘NoneType‘ object has no attribute ‘Window‘如何解决

阅读:775 来源:乙速云 作者:代码code

AttributeError: ‘NoneType‘ object has no attribute ‘Window‘如何解决

报错问题

报错代码如下所示:

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 
    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 
    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"

报错翻译

报错信息内容翻译如下所示

属性错误:“NoneType”对象没有属性“Window”
pyglet.canvas.xlib.NoSuch显示异常:无法连接到“None”

报错原因

报错原因:

在其他少数情况下,它不是以一种方式运行而不是以另一种方式工作的。大多数解决方案都涉及更改图形卡设置或运行X11服务器。这在这里似乎不相关,因为它似乎只与VSCode相关。

小伙伴们按照下面的方法配置vscode即可即可!!!

解决方法

这是vscode内部终端的问题,将控制台设置为launch.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'
分享到:
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: hlamps#outlook.com (#换成@)。
相关文章
{{ v.title }}
{{ v.description||(cleanHtml(v.content)).substr(0,100)+'···' }}
你可能感兴趣
推荐阅读 更多>