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

pyglet.window

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. 

Window

from pyglet import *
from pyglet.gl import *
from pyglet.window import Window


new_window = Window(width=640, height=480)
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')


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


@new_window.event
def on_key_press(symbol, modifiers):
    print(chr(symbol), "key pressed")


@new_window.event
def on_hide():
print("Window is minimized")


pyglet.app.run()
You can also try this code with Online Python Compiler
Run Code

In the above code, we created a new window using the window function. We then assigned a key press event using the on_key_press function.

 

We can hide the mouse cursor using the set_exclusive_mouse function, for example.

from pyglet import *
from pyglet.gl import *
from pyglet.window import Window


new_window = Window(fullscreen=True)
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')


new_window.set_exclusive_mouse(True)


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


pyglet.app.run()
You can also try this code with Online Python Compiler
Run Code


 

We can also work with multiple screens using the window function in pyglet. We can use the following code snippet to access multiple screens,

display = pyglet.canvas.get_display()
screens = display.get_screens()
windows = []
for screen in screens:
    windows.append(window.Window(fullscreen=True, screen=screen))
You can also try this code with Online Python Compiler
Run Code

 

We can further set and fetch the location value of the window using the set_location and get_location functions. For example,

from pyglet import *
from pyglet.gl import *
from pyglet.window import Window


new_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')
new_window.set_location(500, 100)
@new_window.event
def on_draw():
new_window.clear()
label.draw()


value = new_window.get_location()
print(value)
pyglet.app.run()
You can also try this code with Online Python Compiler
Run Code

 

Developers can also access the cursor details using the on_mouse_motion and on_mouse_press functions. For example,

from pyglet import *
from pyglet.gl import *
from pyglet.window import Window


new_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')
@new_window.event
def on_draw():
new_window.clear()
label.draw()


@new_window.event
def on_mouse_motion(x, y, dx, dy):
print(x,",",y)


@new_window.event
def on_mouse_press(x, y, button, modifiers):
if(button == 1):
print("Left Mouse Button Pressed")
else:
print("Right Mouse Button Pressed")


pyglet.app.run()
You can also try this code with Online Python Compiler
Run Code

Developers can access many more similar attributes present in the window function.

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.

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 using the on_mouse_motion() function?

The on_mouse_motion() function is used to fetch all the details of the movement of the mouse on the created window. Developers can fetch the actual or the relative movement of the mouse on the created window using the on_mouse_motion function. 

What is the purpose of using the set_exclusive_mouse() function?

The set_exclusive_mouse() function is used to hide the cursor from the created window. It is used in games where the actual position of the mouse is not required. Instead, the relative movement of the mouse is essential. 

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