类型错误:不支持的操作数类型
这是我正在编写的一个程序,应该在窗口中显示一些文本......
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
TextLayout
是一个类——所以TextLayout.width
是一个原始属性,对你来说毫无用处;您想要从TextLayout
类的实例获取width
,而不是从类本身!此外,该类专门用于布局文本文档,因此我真的不明白为什么您想要获取它(因为您周围没有文档对象)。我怀疑您真正想要的是:
并删除
TextLayout
的导入和所有提及。TextLayout
is a class -- soTextLayout.width
is a raw property, pretty useless to you; you want to getwidth
from an instance of theTextLayout
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:
and remove the import of, and all mentions of,
TextLayout
.如果您使用的是 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.