溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

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

發(fā)布時(shí)間:2023-03-16 11:27:55 來(lái)源:億速云 閱讀:182 作者:iii 欄目:開發(fā)技術(shù)

這篇文章主要講解了“AttributeError: ‘NoneType‘ object has no attribute ‘Window‘如何解決”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來(lái)研究和學(xué)習(xí)“AttributeError: ‘NoneType‘ object has no attribute ‘Window‘如何解決”吧!

報(bào)錯(cuò)問(wèn)題

報(bào)錯(cuò)代碼如下所示:

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

報(bào)錯(cuò)信息內(nèi)容如下所示

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"

報(bào)錯(cuò)翻譯

報(bào)錯(cuò)信息內(nèi)容翻譯如下所示

屬性錯(cuò)誤:“NoneType”對(duì)象沒(méi)有屬性“Window”
pyglet.canvas.xlib.NoSuch顯示異常:無(wú)法連接到“None”

報(bào)錯(cuò)原因

報(bào)錯(cuò)原因:

在其他少數(shù)情況下,它不是以一種方式運(yùn)行而不是以另一種方式工作的。大多數(shù)解決方案都涉及更改圖形卡設(shè)置或運(yùn)行X11服務(wù)器。這在這里似乎不相關(guān),因?yàn)樗坪踔慌cVSCode相關(guān)。

小伙伴們按照下面的方法配置vscode即可即可!?。?/p>

解決方法

這是vscode內(nèi)部終端的問(wèn)題,將控制臺(tái)設(shè)置為launch.json中的另一個(gè)選項(xiàng)解決了這個(gè)問(wèn)題:

{
    // 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"
        }
    ]
}

因此,事實(shí)上,在python腳本中添加以下內(nèi)容可以解決問(wèn)題(至少結(jié)合切換到Wayland):

import os
os.environ['DISPLAY'] = ':1'

感謝各位的閱讀,以上就是“AttributeError: ‘NoneType‘ object has no attribute ‘Window‘如何解決”的內(nèi)容了,經(jīng)過(guò)本文的學(xué)習(xí)后,相信大家對(duì)AttributeError: ‘NoneType‘ object has no attribute ‘Window‘如何解決這一問(wèn)題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是億速云,小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI