Here's some sample code I copy pasted to try and get a Pyglet layout to work. I just get a blank screen. Every time I try to write my own code based on the docs or copy/paste some example code it just will not render the layout.

Interwebs reveal a few other people with this issue but no answers given.

Labels seem to work fine although even they are quirky.

import pyglet

white = (255, 255, 255, 255)

window = pyglet.window.Window(width=200, height=200, caption='Text layout')

# document
document = pyglet.text.decode_text(
    ('Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! '
     'Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World!'))
document.set_style(start=0, end=0, attributes=dict(font_size=12, color=white))

# text layout
text_layout = pyglet.text.layout.TextLayout(document=document,
text_layout.color = (.5,.5,.5,1.0)

def on_draw():


2 Answers 2


It turns out I need to declare a font type in the attributes of the document.

I added:

font_name="Times New Roman" 

to the attributes dictionary and voila. Frustrating that all the examples I found did not explicitly declare a font type, and yet for me this seems to be a requirement. I was just about to give up and roll my own text renderer.

  • \$\begingroup\$ The lack of community support for this otherwise nice framework, has made me decide to stop using Pyglet altogether. Far less frustrating(and more educational) to just roll my own text renderer in PyOpenGL than to try and guess my way through the various bugs I'm getting. I could read the documentation more thoroughly but even then there is no guarantee I'll find my answer. I did like the look of the API for rendering text but its just not worth the headaches. \$\endgroup\$ Jun 25, 2017 at 4:03

This was the only example I could find for use of text_layout, but to make it work, I needed the change the following line:

document.set_style(start=0, end=0, attributes=dict(font_name="Times New Roman" 
    ,font_size=12, color=white))


document.set_style(start=0, end=len(document.text), attributes=dict(
    font_name="Times New Roman" ,font_size=12, color=white))

I would have expected that end=0 would do the whole document, but no.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .