Table of contents
1.
Introduction
2.
Font
3.
Frequently Asked Questions
3.1.
What is pyglet?
3.2.
What is the purpose of the font function?
3.3.
Why would it be advisable for us to utilize Pyglet?
3.4.
What are some execution capacities in Pyglet?
3.5.
What is the purpose of the have_font method?
4.
Conclusion
Last Updated: Mar 27, 2024

pyglet.font

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Pyglet is a multimedia and cross-platform gaming package in Python. Pyglet can be used to create games and other visual applications. The font function in pyglet is used to load any system-installed fonts. The developer can also add additional third-party fonts using the attributes present in the fonts function. 

Font

A simple pyglet code will look something like this,

import pyglet
new_window = pyglet.window.Window()

label = pyglet.text.Label('Hey Ninja!', bold = True, font_name ='Cooper', font_size = 16, x = new_window.width//2, y = new_window.height//2, anchor_x ='center', anchor_y ='center')

label1 = pyglet.text.Label('This is a new window created using pyglet', font_name ='Cooper', font_size = 12, x = new_window.width//2, y = new_window.height//2-40, anchor_x ='center', anchor_y ='center')

@new_window.event
def on_draw():
new_window.clear()
label.draw()
label1.draw()
pyglet.app.run()
You can also try this code with Online Python Compiler
Run Code


Let us add a new font using the font function.

from pyglet import *
from pyglet.gl import *

font.add_file(os.path.join(os.path.dirname(os.path.abspath(__file__)), "Zuka Doodle.ttf"))
new_window = pyglet.window.Window()
label = pyglet.text.Label('Hey Ninja!', bold = True, font_name ='Cooper', font_size = 16, x = new_window.width//2, y = new_window.height//2, anchor_x ='center', anchor_y ='center')

label1 = pyglet.text.Label('This is a new window created using pyglet', font_name ='Cooper', font_size = 12, x = new_window.width//2, y = new_window.height//2-40, anchor_x ='center', anchor_y ='center')

PlayLabel = pyglet.text.Label('New Font', font_name='Zuka Doodle', font_size=20, x=new_window.width // 2, y=new_window.height//2-80, anchor_x='center', anchor_y='center') 

@new_window.event
def on_draw():
new_window.clear()
label.draw()
label1.draw()
PlayLabel.draw()
pyglet.app.run()
You can also try this code with Online Python Compiler
Run Code

In this example, we downloaded an external font and then added it to our system using the add_file attribute of the font function.

 

We can also use the have_font method to verify if a font is present in the system.

from pyglet import *
font.add_file(os.path.join(os.path.dirname(os.path.abspath(__file__)), "Zuka Doodle.ttf"))
print(font.have_font("Zuka Doodle"))
You can also try this code with Online Python Compiler
Run Code

Frequently Asked Questions

What is pyglet?

Pyglet is a multimedia and cross-platform gaming package in Python. Pyglet can be used to create games and other visual applications.

What is the purpose of the font function?

The font function in pyglet is used to load any system-installed fonts. The developer can also add additional third-party fonts using the attributes present in the fonts function. 

Why would it be advisable for us to utilize Pyglet?

Pyglet gives an item arranged application programming connection point for the production of games and other mixed media applications. Along these lines assuming we wish to make any such application, Pyglet is the best other option.

What are some execution capacities in Pyglet?

Let pyglet answer application events like the mouse and control center. Your event regulators will presently be called as required, and the run() system will return strictly when all application windows have been closed. It utilizes picture watching, playing sound and music, Handling mouse and console occasions, and so forth.

What is the purpose of the have_font method?

The have_font method is used to verify if a given font name is present in the system or not. 

Conclusion

This Blog covered all the necessary points about the font function in the pyglet package. We also looked at the various parameters of the font function. In the end, we discussed a few codes to get a better grasp of the topic. 

Hey Ninjas! Don’t stop here; check out Coding Ninjas for Python, more unique courses, and guided paths. Also, try Coding Ninjas Studio for more exciting articles, interview experiences, and excellent Machine Learning and Python problems. 

Live masterclass