Python“调用draw()时必须使用Label实例作为第一个参数(改为获取_WindowMetaclass实例)”

发布于 2024-09-05 01:23:51 字数 654 浏览 0 评论 0原文

这是我使用 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:必须使用 Label 实例作为第一个参数调用未绑定的方法 draw()(改为使用 _WindowMetaclass 实例)”。我很确定我知道我必须做什么(找到如何获取 Label 的实例),但不知道如何去做。有人可以帮助我了解如何进行这项工作吗?

This is a class I made using Python with pyglet to display a window.

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()

Every time I try to run it I get the error "TypeError: unbound method draw() must be called with Label instance as first argument (got _WindowMetaclass instance instead)". I'm pretty sure I know what I have to do (find how to get Label's instance) just not how to do it. Could someone help me understand how to make this work?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

笑红尘 2024-09-12 01:23:51

如果我不得不猜测,我会说您应该绑定上面 2 行创建的实例并使用它。

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

    windowText = mylabel.draw(...

If I had to guess, I'd say that you should bind the instance you create 2 lines above and use that instead.

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

    windowText = mylabel.draw(...
我纯我任性 2024-09-12 01:23:51

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

you give a class "Window" instead of an instance as argument, try "self"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文