Open In App

PYGLET – Creating window

Last Updated : 31 May, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

In this article we will see how we can create a window in PYGLET module in python. Pyglet is easy to use but powerful library for developing visually rich GUI applications like games, multimedia etc on Windows, Mac OS and Linux. This library is created purely in Python and it supports many features like windowing, user interface event handling, Joysticks, OpenGL graphics, loading images, and videos, and playing sounds and music. A window is a “heavyweight” object occupying operating system resources. Windows may appear as floating regions or can be set to fill an entire screen (fullscreen). When floating, windows may appear borderless or decorated with a platform-specific frame (including, for example, the title bar, minimize and close buttons, resize handles, and so on).
 

In order to create window we use Window method with the pyglet.window 
Syntax : pyglet.window.Window(width, height, title)
Argument : It takes optional arguments i.e width, height and title of window
Return : It returns pyglet.window.win32.Win32Window object 
 

Note : Either we provide no argument or we have to prove all three arguments else error would generate
Example : 
 

Python3




# importing pyglet module
from pyglet import *
 
# width of window
width = 400
 
# height of window
height = 400
 
# caption i.e title of the window
title = "PYGLET GfG"
 
# creating a window
win = window.Window(width, height, title)
 
# start running the application
app.run()


Output : 
 

Another Example : 
 

Python3




# importing pyglet module
import pyglet
 
# width of window
width = 500
 
# height of window
height = 500
 
# caption i.e title of the window
title = "Geeksforgeeks"
 
# creating a window
window = pyglet.window.Window(width, height, title)
 
# creating alabel
label = pyglet.text.Label('GeeeksforGeeks',
                          font_name ='Times New Roman',
                          font_size = 36,
                          x = window.width//2, y = window.height//2,
                          anchor_x ='center', anchor_y ='center')
 
# drawing label
@window.event
def on_draw():
    window.clear()
    label.draw()
 
# start running the application
pyglet.app.run()




Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads