| 1 |
.. _guide_gui: |
| 2 |
|
| 3 |
Simple Widgets & GUI |
| 4 |
==================== |
| 5 |
The :py:mod:`pyglet.gui` module provides a selection of widgets that can be used |
| 6 |
to add user interface elements to your game or application. The selection is limited, |
| 7 |
but should cover the most common use cases. For example: the configuration screen in |
| 8 |
a game, or a set of toolbar buttons for a visualization program. |
| 9 |
|
| 10 |
Widgets are internally constructed from other high level pyglet objects, and are |
| 11 |
therefore fairly simple in design. They should blend in well with any pyglet project. |
| 12 |
For example, Widgets are event handlers that receive keyboard and mouse events from the |
| 13 |
Window. They can then in turn dispatch their own custom events, because they subclass |
| 14 |
:py:class:`~pyglet.event.EventDispatcher`. Widgets can take a Batch and Group, similar |
| 15 |
to other pyglet objects, to allow for :ref:`guide_batched-rendering`. |
| 16 |
|
| 17 |
Before reading further, it is important to understand how event handling and dispatching |
| 18 |
work in pyglet. If you have not yet done so, it is recommended that you first read |
| 19 |
through the :ref:`guide_events` section of the documentation. Widgets are by nature |
| 20 |
very tightly associated with input events, so this is necessary to fully grasp their |
| 21 |
usage. |
| 22 |
|
| 23 |
Example code can be found in ``examples/gui/widgets.py`` in the pyglet source repository. |
| 24 |
|
| 25 |
|
| 26 |
Creating a Widget |
| 27 |
----------------- |
| 28 |
Included Widgets are :py:class:`~pyglet.gui.widgets.PushButton`, |
| 29 |
:py:class:`~pyglet.gui.widgets.ToggleButton`, :py:class:`~pyglet.gui.widgets.Slider`, |
| 30 |
and :py:class:`~pyglet.gui.widgets.TextEntry`. They each have different arguments, |
| 31 |
which will be shown in the API documentation. For our example, we will create a |
| 32 |
'PushButton' widget, which requires you to provide at least two images. These two |
| 33 |
images will visually represent the "pressed" and "unpressed" states of the button. |
| 34 |
This widget can also take an optional image for 'hover', but we'll skip that for now:: |
| 35 |
|
| 36 |
pressed_img = pyglet.resource.image("button_pressed.png") |
| 37 |
unpressed_img = pyglet.resource.image("button_unpressed.png") |
| 38 |
|
| 39 |
pushbutton = pyglet.gui.PushButton( |
| 40 |
x=100, y=300, pressed=pressed_img, |
| 41 |
unpressed=unpressed_img, batch=batch, |
| 42 |
) |
| 43 |
|
| 44 |
We now have a PushButton widget, but it won't yet do anything. It will be drawn on |
| 45 |
screen, however, if included as part of a :py:class:`~pyglet.graphics.Batch` as shown |
| 46 |
above. In order to get the widget to react to the mouse, we need to set it to handle |
| 47 |
events dispatched by the Window:: |
| 48 |
|
| 49 |
my_window.push_handlers(pushbutton) |
| 50 |
|
| 51 |
The widget should now change appearance when you click on it. It will switch between |
| 52 |
the provided images (pressed and unpressed states). You can try adding the 'hover' |
| 53 |
image as well, for more visual feedback. |
| 54 |
|
| 55 |
Now that our widget is receiving events, we can now take of the events that are |
| 56 |
produced *by* the widget. In this case, the PushButton widget dispatches two |
| 57 |
events: 'on_press' and 'on_release'. To wire these up, we simply set handlers |
| 58 |
for them:: |
| 59 |
|
| 60 |
def my_on_press_handler(widget): |
| 61 |
print("Button Pressed!") |
| 62 |
|
| 63 |
def my_on_release_handler(widget): |
| 64 |
print("Button Released...") |
| 65 |
|
| 66 |
pushbutton.set_handler('on_press', my_on_press_handler) |
| 67 |
pushbutton.set_handler('on_release', my_on_release_handler) |
| 68 |
|
| 69 |
If we try this code, we should see messages printed to the console whenever we |
| 70 |
press and release the button. You now have a way to make the widgets interact with |
| 71 |
code in your project. |
| 72 |
|
| 73 |
Other widgets are used in a similar way, but have different arguments depending |
| 74 |
on their nature. Have a look at the API documentation for each one, and also |
| 75 |
see the example in the source repository. |
| 76 |
|
| 77 |
|
| 78 |
Frame objects |
| 79 |
------------- |
| 80 |
pyglet also provides an optional :py:class:`~pyglet.gui.frame.Frame` object. |
| 81 |
If you only need a few widgets at a time, then you can ignore this object. |
| 82 |
However, if you several dozen widgets at once, you might find that it's |
| 83 |
wasteful to have every widget receiving the Window events at the same |
| 84 |
time. This is where the Frame can be useful. Essentially, it acts as a |
| 85 |
"middle-man" between the Window events and the Widgets. The Frame implements |
| 86 |
a simple 2D spatial hash, which only passes on Window events to those Widgets |
| 87 |
that are actually near the mouse pointer. This works well for Widgets that |
| 88 |
generally care about mouse clicks and keyboard keys, but has some limitations |
| 89 |
with drag-and-drop type events (more on that later). |
| 90 |
|
| 91 |
Without a Frame, the general widget usage is: |
| 92 |
|
| 93 |
1. Make one or more Widget instances. |
| 94 |
2. Push the Wigets as event handlers on your Window. |
| 95 |
3. All Widgets receives all Window events. |
| 96 |
|
| 97 |
If a Frame is introduced, the following occurs: |
| 98 |
|
| 99 |
1. Make a single Frame instance. |
| 100 |
2. Set the Frame as a handler for Window events. |
| 101 |
3. Make one or more Widget instances. |
| 102 |
4. Add your widget instances to the Frame. |
| 103 |
5. Only Widgets near the mouse pointer will receive Window events. |
| 104 |
|
| 105 |
This works quite well for most cases, but has some limitations. When using the |
| 106 |
TextEntry widget, for instance, the widget may become unresponsive if you use |
| 107 |
click-and-drag to select text, but your mouse pointer moves far enough |
| 108 |
away from the widget. For this reason, Frames may not be suitable. |
| 109 |
|
| 110 |
The Frame concept may be developed further in a future release, but for now it |
| 111 |
serves a limited but useful purpose. |
| 112 |
|
| 113 |
|
| 114 |
Custom widgets |
| 115 |
-------------- |
| 116 |
For users who are interested in creating their own custom Widgets, the |
| 117 |
:py:class:`~pyglet.gui.widgets.WidgetBase` base class is available for |
| 118 |
subclassing. This base class has most of the relevant Window events |
| 119 |
pre-defined, and is ready to be pushed as a handler. Custom subclasses |
| 120 |
can then override whichever mouse or keyboard events they need, depending |
| 121 |
on the application. Some additional helper properties are also provided. |
| 122 |
|
| 123 |
It is recommended look through the pyglet source code to have a better |
| 124 |
understanding of how this looks in practice. Because Widgets are made |
| 125 |
up of other high-level pyglet objects, you might find that it's not |
| 126 |
terribly complex. The PushButton Widget, for example, is less than |
| 127 |
100 lines of code. This may be a good starting point to design a custom |
| 128 |
Widget for your specific use case. |
| 129 |
|
| 130 |
This section may be expanded further in a future release. |