类型错误:不支持的操作数类型

发布于 2024-09-07 17:40:37 字数 1116 浏览 2 评论 0原文

这是我正在编写的一个程序,应该在窗口中显示一些文本......

import pyglet
from pyglet import window
from pyglet.text.layout import TextLayout

class Window(pyglet.window.Window):
    def __init__(self):
        super(Window, self).__init__(width = 800, height = 600,
                                 caption = "Prototype")

        self.disclaimer = pyglet.text.Label("Hello World",
                                   font_name = 'Times New Roman',
                                   font_size=36,
                                   color = (255, 255, 255, 255),
                                   x = TextLayout.width / 2,
                                   y = TextLayout.height / 2,
                                   anchor_x='center', anchor_y='center')

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

if __name__ == '__main__':
    window = Window()
    pyglet.app.run()

但是每次我尝试运行它时都会收到此错误

line 16
x = TextLayout.width / 2,
TypeError: unsupported operand type(s) for /: 'property' and 'int'

我很确定这意味着我试图分割一个字符串但是在 Pyglet 文档中,它说宽度和高度是整数。我不知道我做错了什么。

This is a program I'm writing that's supposed to display some text in a window...

import pyglet
from pyglet import window
from pyglet.text.layout import TextLayout

class Window(pyglet.window.Window):
    def __init__(self):
        super(Window, self).__init__(width = 800, height = 600,
                                 caption = "Prototype")

        self.disclaimer = pyglet.text.Label("Hello World",
                                   font_name = 'Times New Roman',
                                   font_size=36,
                                   color = (255, 255, 255, 255),
                                   x = TextLayout.width / 2,
                                   y = TextLayout.height / 2,
                                   anchor_x='center', anchor_y='center')

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

if __name__ == '__main__':
    window = Window()
    pyglet.app.run()

...however every time I try to run it I get this error

line 16
x = TextLayout.width / 2,
TypeError: unsupported operand type(s) for /: 'property' and 'int'

I'm pretty sure this means that I tried to divide a string but in the Pyglet Documentation it says that width and height are ints. I have no idea what I'm doing wrong.

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

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

发布评论

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

评论(2

泅人 2024-09-14 17:40:37

TextLayout 是一个——所以 TextLayout.width 是一个原始属性,对你来说毫无用处;您想要从 TextLayout 类的实例获取width,而不是从类本身!此外,该类专门用于布局文本文档,因此我真的不明白为什么您想要获取它(因为您周围没有文档对象)。

我怀疑您真正想要的是:

                               x = self.width / 2,
                               y = self.height / 2,

并删除 TextLayout 的导入和所有提及。

TextLayout is a class -- so TextLayout.width is a raw property, pretty useless to you; you want to get width from an instance of the TextLayout class, not from the class itself! Moreover, the class is specifically used to lay out text documents, so I don't really see why you would want to get it at all (since you have no document object around).

I suspect that what actually you want is:

                               x = self.width / 2,
                               y = self.height / 2,

and remove the import of, and all mentions of, TextLayout.

虐人心 2024-09-14 17:40:37

如果您使用的是 Python 版本 3.x,除法运算符 / 会生成浮点类型数字。使用 // 获得截断(传统风格)整数除法。

If you're using Python version 3.x the division operator / results in a float type number. Use // to get truncated (traditional style) integer division.

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