Introduction
Pyglet is a library provided by python to develop games, cross-platform windowing, and multimedia. Pyglet supports user interface event handling, windowing, game controllers, graphics, loading images, videos, etc. The pyglet library can be imported and works on various operating systems like Windows, OS X, and Linux. The pyglet.text module provides various classes for loading the styled documents from different formats of files and a pyglet-specific markup format. These classes can style the documents with multiple fonts, font sizes, alignments, etc. We can create labels that provide an interface for an application to display the text. Let’s learn how to create these labels for different types of files.
Plain text label
label = pyglet.text.Label('Coding Ninjas',
font_name='Calibri',
font_size=36,
x=15, y=15)
The above code displays the test “Coding Ninjas” in Calibri with a font size of 36. The ‘x’ and ‘y’ represent the coordinates where the text is supposed to be placed.
HTML label
label = pyglet.text.HTMLLabel('<b>Coding Ninjas</b>',
x=15, y=15)
The above code is used to style an HTML label. The text “Coding Ninjas” will be displayed in bold letters, and the ‘x’ and ‘y’ represent the coordinates similar to the code discussed in the plain text label. These labels can be drawn or invoked to display the text using the method label.draw().
Pyglet.text contains the following sub-modules.
- pyglet.text.caret
- pyglet.text.document
- pyglet.text.layout
Let’s learn about these sub-modules in this article.
Pyglet.text.caret
Pyglet.text.caret is a submodule of the module pyglet.text. The caret submodule provides the keyboard and mouse editing procedure. It acts as a visible text insertion marker. The visible text is always created and displayed at an exact position. The visible text selection on the layout is updated along with marks and positions. The graphics of the layout’s batch is used by default, so the caret doesn’t need to be drawn explicitly. We can create a window using the caret with the code shown below.
pyglet.text.caret.Caret(layout, color=(x, x, x))
The above code will create a window using the caret submodule with the layout and color mentioned above. The color argument/property takes the RGB values for choosing the color.