# Subclassing Window

A useful pattern in pyglet is to subclass Window for each type of window you will display, or as your main application class. There are several benefits:

- You can load font and other resources from the constructor, ensuring the OpenGL context has already been created.
- You can add event handlers simply be defining them on the class. The on_resize event will be called as soon as the window is created (this doesn't usually happen, as you must create the window before you can attach event handlers).
- There is reduced need for global variables, as you can maintain application state on the window.

The following example shows the same "Hello World" application as presented in Writing a pyglet application, using a subclass of Window:

    class HelloWorldWindow(pyglet.window.Window):
        def __init__(self):
            super(HelloWorldWindow, self).__init__()

            self.label = pyglet.text.Label('Hello, world!')

        def on_draw(self):
            self.clear()
            self.label.draw()

    if __name__ == '__main__':
        window = HelloWorldWindow()
        pyglet.app.run()

This example program is located in examples/programming_guide/window_subclass.py.