We'll begin with the requisite "Hello, World" introduction. This program will open a window with some text in it and wait to be closed. You can find the entire program in the examples/programming_guide/hello_world.py file.
Begin by importing the modules from pyglet that we need:
from pyglet import font from pyglet import window
Create a Window by calling its default constructor. The window will be visible as soon as it's created, and will have reasonable default values for all its parameters:
win = window.Window()
To display the text, we'll create a font.Text. The text requires a font, which we'll load from the system:
ft = font.load('Arial', 36) text = font.Text(ft, 'Hello, World!')
Every pyglet application requires a "run loop". You'll always need to write this loop yourself. Within the loop, we need to:
The run loop is shown below:
while not win.has_exit: win.dispatch_events() win.clear() text.draw() win.flip()
By default, windows in pyglet are double-buffered, which is necessary for flicker-free animation. The flip method makes all previous drawing operations visible.