首頁 > 軟體

解決AttributeError:'NoneTypeobject' has no attribute'Window'的問題(親測有效)

2023-03-17 06:04:54

已解決VSCode執行強化學習程式碼丟擲異常AttributeError: ‘NoneType’ object has no attribute ‘Window’
pyglet.canvas.xlib.NoSuchDisplayException: Cannot connect to “None” 的正確解決方法,親測有效!!

報錯問題

一個小夥伴遇到問題跑來私信我,在VSCode執行強化學習程式碼,但是發生了報錯(當時他心裡瞬間涼了一大截,跑來找我求助,然後順利幫助他解決了,順便記錄一下希望可以幫助到更多遇到這個bug不會解決的小夥伴),報錯程式碼如下所示:

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‘的問題(親測有效)的文章就介紹到這了,更多相關NoneType object has no attribute 內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com