views:

58

answers:

2

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.

A: 

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.

Don O'Donnell
It's not python 3 and using // gives the same error, thank you though for trying.
Amorack
+3  A: 

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.

Alex Martelli
Darn it. You beat me by 30 seconds.
jcao219
Thank you, I seem to be having problems with instances but I think i get it now.
Amorack
@Amorack, you're welcome!
Alex Martelli