Python “draw() 必须以 Label 实例作为第一个参数(而不是 _WindowMetaclass 实例)”

1 投票
2 回答
821 浏览
提问于 2025-04-15 23:53

这是我用Python和pyglet库制作的一个类,用来显示一个窗口。

class Window(pyglet.window.Window):
    def __init__(self):
        super(Window, self).__init__()

        pyglet.text.Label("Prototype")

        windowText = text.Label.draw(Window, "Hello World",
                          font_name = "Times New Roman",
                          font_size = 36,
                          color = (193, 205, 193, 255))

    def on_draw(self):
        self.clear()
        self.label.draw()

每次我尝试运行它时,都会出现一个错误:“TypeError: unbound method draw() must be called with Label instance as first argument (got _WindowMetaclass instance instead)”。我很确定我知道我需要做什么(找到如何获取Label的实例),只是我不知道该怎么做。有人能帮我理解一下怎么让它工作吗?

2 个回答

0

你给了一个类“Window”作为参数,而不是一个实例,试试用“self”。

2

如果让我猜的话,我会说你应该使用你在上面两行创建的那个实例,而不是其他的。

    mylabel = pyglet.text.Label("Prototype")

    windowText = mylabel.draw(...

撰写回答