这篇文章主要讲解了“AttributeError: ‘NoneType‘ object has no attribute ‘Window‘如何解决”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“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 <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"
报错信息内容翻译如下所示:
属性错误:“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'
感谢各位的阅读,以上就是“AttributeError: ‘NoneType‘ object has no attribute ‘Window‘如何解决”的内容了,经过本文的学习后,相信大家对AttributeError: ‘NoneType‘ object has no attribute ‘Window‘如何解决这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。