How to fix ''AttributeError: module 'pyglet.text' has no attribute 'label'" error in python

How to fix ''AttributeError: module 'pyglet.text' has no attribute 'label'" error in python

我正在尝试用 pyglet 创建一个简单的游戏。我仍在学习这个库,并且正在使用 pyglet 编程指南来学习。但是,我得到

AttributeError: module 'pyglet.text' has no attribute 'label'

每次我 运行 代码都出错。

我已经尝试了 import pyglet.textimport pyglet.text.labelimport pyglet.text 仍然给出

AttributeError: module 'pyglet.text' has no attribute 'label'.

然而,import pyglet.text.label 给出

ModuleNotFoundError: No module named 'pyglet.text.label'.

我的代码是:

import pyglet
import pyglet.text.label

window = pyglet.window.Window()
label = pyglet.text.label('Hello World',
    font_name='Times New Roman',
    font_size=36,
    x=window.width//2,
    y=window.height//2,
    anchor_x='center',
    anchor_y='center')
@window.event
def on_draw():
    window.clear()
    label.draw()
pyglet.app.run()

我希望看到 window 被清除为默认背景颜色,并在屏幕上打印 Hello World。但我总是得到一个空白 window 和

AttributeError: module 'pyglet.text' has no attribute 'label'

印在 shell.

AttributeError: module 'pyglet.text' has no attribute 'label'

您收到此错误是因为您使用的是 pyglet.text.label 而不是 pyglet.text.Label

这是 text

的文档

仅导入 pyglet,然后将标签更改为标签,它应该可以工作。