如何修复''AttributeError:module'pyglet.text'在python中没有属性'label'“错误

问题描述 投票:2回答:1

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

AttributeError:模块'pyglet.text'没有属性'label'

每次运行代码时都会出错。

我试过import pyglet.textimport pyglet.text.labelimport pyglet.text仍然给出

AttributeError:模块'pyglet.text'没有属性'label'。

但是,import pyglet.text.label给出了

ModuleNotFoundError:没有名为'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()

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

AttributeError:模块'pyglet.text'没有属性'label'

印在贝壳上。

python pyglet
1个回答
1
投票
AttributeError: module 'pyglet.text' has no attribute 'label'

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

这是text的文档

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

© www.soinside.com 2019 - 2024. All rights reserved.