Search code examples
python-3.xpyglet

How to make HTMLLabel() work in Pyglet 2.0?


The section "Displaying text" of the official pyglet document (https://pyglet.readthedocs.io/en/latest/programming_guide/text.html), gives an example of how to use HTMLLabel(). The example works in pyglet 1.5.28 but not in pyglet 2.0.15. Weirdly enough, HTMLLabel() does not raise an error --e.g. "pyglet.text has no method HTMLLabel()" (or something similar) as it is usually the case-- i.e. it is accepted by v2.0, only nothing is shown in the pyglet window. Below is a simple but fully workable code in which I added whatever is needed to run that example:

import pyglet
from pyglet.gl import *

window = pyglet.window.Window(200,200)
glClearColor(1, 1, 1, 1)

# The following 4 lines are copy-pasted from the document 
label = pyglet.text.HTMLLabel(
    '<font face="Times New Roman" size="4">Hello, <i>world</i></font>',
    x=window.width//2, y=window.height//2,
    anchor_x='center', anchor_y='center')

@window.event
def on_draw():
  window.clear()
  label.draw()

pyglet.app.run()

Here's the output produced by v1.15:

enter image description here

Running the same code with v2.0 shows an empty window.

I have searched the Web extensively but could not find not a solution, but even what is the problem. Not even near. So, I hope that someone in here does have a solution for using HTML text as a label (or in any other way) in v2.0.


Solution

  • This looks like a bug in pyglet, introduced in version 2.0.12.

    If you downgrade to version 2.0.10, your code will display the HTML text:

    pip install --force-reinstall -v "pyglet==2.0.10"

    You should create a bug report.