Table of contents
1.
Introduction
1.1.
Plain text label
1.2.
HTML label
2.
Pyglet.text.caret
3.
pyglet.text.Document
4.
Pyglet.text.layout
4.1.
TextLayout
4.2.
ScrollableTextLayout
4.3.
IncrementalTextLayout
5.
FAQs
5.1.
What is pyglet?
5.2.
Why is pyglet used?
5.3.
Is pyglet faster than Pygame?
5.4.
What are the submodules of pyglet.text?
5.5.
What does the pyglet.text.caret module do?
6.
Key Takeaways
Last Updated: Mar 27, 2024
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

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)
You can also try this code with Online Python Compiler
Run Code


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)
You can also try this code with Online Python Compiler
Run Code


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))
You can also try this code with Online Python Compiler
Run Code


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. 

pyglet.text.Document

The pyglet.text.Document is a submodule of pyglet.text. The document sub-module provides formatted and unformatted interfaces that are used by the text layouts. An unformatted document always grabs a user's attention and doesn’t look presentable. So the document sub-module provides us with the formatted documents. The formatted documents increase the user readability and interaction by adding structure, such as headings, indentation, font, etc. It formats the document layout according to the devices and screen size. We use the FormattedDocument() method to create a window and a formatted document.

pyglet.text.document.FormattedDocument(text)
You can also try this code with Online Python Compiler
Run Code

 

Read about Batch Operating System here.

Pyglet.text.layout

The pyglet.text.layout is a submodule of pyglet.text. The layout sub module renders the text and formatted documents. It provides us with three classes.

TextLayout

The TextLayout class provides a layout for the text and groups the layouts in the same batch to render multiple batches simultaneously. The entire layout will be changed again if there are any changes to the properties, text, or document.

ScrollableTextLayout

The ScrollableTextLayout class arranges the layout of the text such that any overflow in the text is hidden, and a scroll bar is provided to access the content hidden from the current display.

IncrementalTextLayout

Whenever any part of the text or document is modified or rendered, only that specific part is laid out again instead of the entire document or layout. If there is a scroll bar, only the visible part of the document is rendered, and the other part is rendered when the user scrolls down to that content. We can render the layout using the following syntax.

pyglet.text.layout.IncrementalTextLayout(document, width, height)
You can also try this code with Online Python Compiler
Run Code

FAQs

What is pyglet?

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. It can be imported and works on various operating systems like Windows, OS X, and Linux. 

Why is pyglet used?

Pyglet is used to build games and other visual applications. It provides classes and submodules that can style the documents, text files, HTML files, etc. It provides font, size, layout, position, etc., for the content.

Is pyglet faster than Pygame?

Pyglet is faster than pygame because pygame doesn’t provide 3D support, and updating every smallest part of the screen and remembering it can be complex and consumes a lot of time. 

What are the submodules of pyglet.text?

The pyglet.text module in python has three submodules that style and format the content and its layout. They are

pyglet.text.caret

pyglet.text.document

pyglet.text.layout

What does the pyglet.text.caret module do?

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. 

Key Takeaways

We have discussed the concept of the pyglet and the module pyglet.text in python in this article. 

Hey Ninjas! We hope this blog helped you better to understand the pyglet.text module in python. Please check out Coding Ninjas for more unique courses and guided paths. Also, try Coding Ninjas Studio for more exciting articles, interview experiences, and fantastic Data Structures and Algorithms problems. Please upvote our blog to help the other ninjas grow.

Happy Coding!

Live masterclass