您好,登錄后才能下訂單哦!
這篇文章主要講解了“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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。