pyglet.gui

Input management

class UIManager(
window: BaseWindow,
enable: bool = True,
cell_size: int = 64,
order: int = 0,
cursor: str | MouseCursor | None = None,
) → None

Bases: object

Own a window’s GUI input, focus, and widget registration.

A UI manager is the single event handler for a window’s GUI. It uses a spatial hash to dispatch pointer events efficiently, and owns keyboard focus and mouse capture. Construct widgets, frames, and layouts with this manager as their parent to register them automatically.

Create a UI manager for window.

Parameters:
  • window (BaseWindow) – The SpatialHash will receive events from this Window. Appropriate events will be passed on to all added Widgets.

  • enable (bool) – Whether to enable this manager.

  • cell_size (int) – The cell (“bucket”) size for each cell in the hash. Widgets may span multiple cells.

  • order (int) – Widgets use internal ordered Groups for draw sorting. This is the base value for these Groups.

  • cursor (str | MouseCursor | None) – System cursor name or custom mouse cursor to show when no widget supplies one.

on_key_press(symbol: int, modifiers: int) → None

Pass the event to the focused widget, or widgets under the mouse.

Return type:

None

on_key_release(symbol: int, modifiers: int) → None

Pass the event to the focused widget, or widgets under the mouse.

Return type:

None

on_mouse_drag(
x: int,
y: int,
dx: int,
dy: int,
buttons: int,
modifiers: int,
) → None

Pass drag events to active widgets and update hover state.

Return type:

None

on_mouse_motion(x: int, y: int, dx: int, dy: int) → None

Dispatch widget enter/leave transitions and motion within the spatial hash.

Return type:

None

on_mouse_press(x: int, y: int, buttons: int, modifiers: int) → None

Pass the event to any widgets within range of the mouse.

Return type:

None

on_mouse_release(x: int, y: int, buttons: int, modifiers: int) → None

Pass the event to any widgets that are currently active.

Return type:

None

on_mouse_scroll(x: int, y: int, scroll_x: float, scroll_y: float) → None

Pass the event to any widgets within range of the mouse.

Return type:

None

on_resize(width: int, height: int) → None

Notify widgets of a frame resize, then rebuild the spatial hash.

Return type:

None

on_text(text: str) → None

Pass the event to the focused widget, or widgets under the mouse.

Return type:

None

on_text_motion(motion: int) → None

Pass the event to the focused widget, or widgets under the mouse.

Return type:

None

on_text_motion_select(motion: int) → None

Pass the event to the focused widget, or widgets under the mouse.

Return type:

None

on_touch_end(
pointer_id: int,
x: float,
y: float,
dx: float,
dy: float,
width: float,
height: float,
pressure: float,
scale: float,
) → None

Release widgets when the controlling contact ends.

Return type:

None

on_touch_move(
pointer_id: int,
x: float,
y: float,
dx: float,
dy: float,
width: float,
height: float,
pressure: float,
scale: float,
) → None

Drag widgets with the contact that began the interaction.

Return type:

None

on_touch_start(
pointer_id: int,
x: float,
y: float,
dx: float,
dy: float,
width: float,
height: float,
pressure: float,
scale: float,
) → None

Use the first touch contact to interact with widgets.

Return type:

None

remove_widget(widget: WidgetBase) → None

Remove a Widget from the spatial hash.

Return type:

None

set_focus(widget: WidgetBase | None) → None

Set the widget that receives keyboard and text input.

Return type:

None

property enable: bool

Whether to enable this UI manager.

Type:

bool

property focused_widget: WidgetBase | None

The widget currently receiving keyboard and text input.

property manager: UIManager

Return this manager so widgets can derive it from their parent.

Widgets

class WidgetBase(
parent: UIManager | Frame | ScrollableRegion | None,
x: float,
y: float,
width: float,
height: float,
cursor: str | MouseCursor | None = None,
) → None

Bases: EventDispatcher, Generic[_ValueT]

The base of all widgets.

on_focus_gain() → None

Handle this widget becoming the UI manager’s focus target.

Return type:

None

on_focus_lost() → None

Handle this widget no longer being the UI manager’s focus target.

Return type:

None

on_mouse_enter_widget(x: int, y: int) → None

Event handler called when the cursor enters this widget.

Return type:

None

on_mouse_leave_widget(x: int, y: int) → None

Event handler called when the cursor leaves this widget.

Return type:

None

on_resize(width: int, height: int) → None

Handle a resize of the owning UI manager’s window.

Subclasses can override this to update their position or size in response to the new window dimensions.

Return type:

None

property aabb: tuple[float, float, float, float]

Bounding box of the widget.

The “left”, “bottom”, “right”, and “top” coordinates of the widget. This is expressed as (x, y, x + width, y + height).

property cursor: str | MouseCursor | None

System cursor name or custom mouse cursor shown while hovering.

property enabled: bool

Get/set whether this widget is enabled.

To react to changes in this value, override _set_enabled() on widgets. For example, you may want to cue the user by:

  • Playing an animation and/or sound

  • Setting a highlight color

  • Displaying a toast or notification

property height: float

Height of the widget.

property manager: UIManager | None

The UI manager that owns this widget’s input and focus.

property parent: UIManager | Frame | ScrollableRegion | None

The immediate container this widget belongs to.

The window-level manager owns input and focus.

property position: tuple[float, float]

The x, y coordinate of the widget as a tuple.

property value: _ValueT

Query or set the Widget’s value.

This property allows you to set the value of a Widget directly, without any user input. This could be used, for example, to restore Widgets to a previous state, or if some event in your program is meant to naturally change the same value that the Widget controls. Note that events are not dispatched when changing this property directly.

property width: float

Width of the widget.

property x: float

X coordinate of the widget.

property y: float

Y coordinate of the widget.

class PushButton(
parent: UIManager | Frame | ScrollableRegion,
x: int,
y: int,
pressed: Texture,
unpressed: Texture,
hover: Texture | None = None,
batch: Batch | None = None,
group: Group | None = None,
style: ButtonStyle | None = None,
cursor: str | MouseCursor | None = 'hand',
) → None

Bases: WidgetBase[bool]

Instance of a push button.

Triggers the event ‘on_press’ when it is clicked by the mouse. Triggers the event ‘on_release’ when the mouse is released.

Create a push button.

Parameters:
  • x (int) – X coordinate of the push button.

  • y (int) – Y coordinate of the push button.

  • pressed (Texture) – Image to display when the button is pressed.

  • unpressed (Texture) – Image to display when the button isn’t pressed.

  • hover (Texture | None) – Image to display when the button is being hovered over.

  • batch (Batch | None) – Optional batch to add the push button to.

  • group (Group | None) – Optional parent group of the push button.

  • cursor (str | MouseCursor | None) – System cursor name or custom mouse cursor to show while hovering. Defaults to the pointing hand cursor.

on_mouse_enter_widget(x: int, y: int) → None

Event handler called when the cursor enters this widget.

Return type:

None

on_mouse_leave_widget(x: int, y: int) → None

Event handler called when the cursor leaves this widget.

Return type:

None

on_press(widget: PushButton) → None

Event: Dispatched when the button is clicked.

Return type:

None

on_release(widget: PushButton) → None

Event: Dispatched when the button is released.

Return type:

None

property value: bool

Whether the button is pressed or not.

class TextButton(
parent: UIManager | Frame | ScrollableRegion,
x: int,
y: int,
text: str,
pressed_color: Tuple[int, int, int] | Tuple[int, int, int, int] = (255, 0, 0),
unpressed_color: Tuple[int, int, int] | Tuple[int, int, int, int] = (255, 255, 255),
hover_color: Tuple[int, int, int] | Tuple[int, int, int, int] = (0, 255, 0),
batch: Batch | None = None,
group: Group | None = None,
style: TextButtonStyle | None = None,
cursor: str | MouseCursor | None = 'hand',
) → None

Bases: WidgetBase[bool]

Instance of a text push button.

Triggers the event ‘on_press’ when it is clicked by the mouse. Triggers the event ‘on_release’ when the mouse is released.

Create a push button.

Parameters:
  • x (int) – X coordinate of the push button.

  • y (int) – Y coordinate of the push button.

  • pressed_color (Tuple[int, int, int] | Tuple[int, int, int, int]) – Color to change text when the button is pressed.

  • unpressed_color (Tuple[int, int, int] | Tuple[int, int, int, int]) – Color to change text when the button isn’t pressed.

  • hover_color (Tuple[int, int, int] | Tuple[int, int, int, int]) – Color to change text when the button is being hovered over.

  • batch (Batch | None) – Optional batch to add the push button to.

  • group (Group | None) – Optional parent group of the push button.

  • cursor (str | MouseCursor | None) – System cursor name or custom mouse cursor to show while hovering. Defaults to the pointing hand cursor.

on_mouse_enter_widget(x: int, y: int) → None

Event handler called when the cursor enters this widget.

Return type:

None

on_mouse_leave_widget(x: int, y: int) → None

Event handler called when the cursor leaves this widget.

Return type:

None

on_press(widget: TextButton) → None

Event: Dispatched when the button is clicked.

Return type:

None

on_release(widget: TextButton) → None

Event: Dispatched when the button is released.

Return type:

None

property height: float

Height of the button hit area.

property value: bool

Whether the button is pressed or not.

property width: float

Width of the button hit area.

class ToggleButton(
parent: UIManager | Frame | ScrollableRegion,
x: int,
y: int,
pressed: Texture,
unpressed: Texture,
hover: Texture | None = None,
batch: Batch | None = None,
group: Group | None = None,
style: ButtonStyle | None = None,
cursor: str | MouseCursor | None = 'hand',
) → None

Bases: PushButton

Instance of a toggle button.

Triggers the event ‘on_toggle’ when the mouse is pressed or released.

Create a push button.

Parameters:
  • x (int) – X coordinate of the push button.

  • y (int) – Y coordinate of the push button.

  • pressed (Texture) – Image to display when the button is pressed.

  • unpressed (Texture) – Image to display when the button isn’t pressed.

  • hover (Texture | None) – Image to display when the button is being hovered over.

  • batch (Batch | None) – Optional batch to add the push button to.

  • group (Group | None) – Optional parent group of the push button.

  • cursor (str | MouseCursor | None) – System cursor name or custom mouse cursor to show while hovering. Defaults to the pointing hand cursor.

on_mouse_leave_widget(x: int, y: int) → None

Keep the pressed image while this toggle remains active.

Return type:

None

on_toggle(widget: ToggleButton, value: bool) → None

Event: returns True or False to indicate the current state.

Return type:

None

class Slider(
parent: UIManager | Frame | ScrollableRegion,
x: int,
y: int,
base: Texture,
knob: Texture,
edge: int = 0,
batch: Batch | None = None,
group: Group | None = None,
cursor: str | MouseCursor | None = 'hand',
) → None

Bases: WidgetBase[float]

Instance of a slider made of a base and a knob image.

Triggers the event ‘on_change’ when the knob position is changed. The knob position can be changed by dragging with the mouse, or scrolling the mouse wheel.

Create a slider.

Parameters:
  • x (int) – X coordinate of the slider.

  • y (int) – Y coordinate of the slider.

  • base (Texture) – Image to display as the background to the slider.

  • knob (Texture) – Knob that moves to show the position of the slider.

  • edge (int) – Pixels from the maximum and minimum position of the slider, to the edge of the base image.

  • batch (Batch | None) – Optional batch to add the slider to.

  • group (Group | None) – Optional parent group of the slider.

  • cursor (str | MouseCursor | None) – System cursor name or custom mouse cursor to show while hovering. Defaults to the pointing hand cursor.

on_change(widget: Slider, value: float) → None

Event: Returns the current value when the slider is changed.

Return type:

None

property value: float

Query or set the Widget’s value.

This property allows you to set the value of a Widget directly, without any user input. This could be used, for example, to restore Widgets to a previous state, or if some event in your program is meant to naturally change the same value that the Widget controls. Note that events are not dispatched when changing this property directly.

class TextEntry(
parent: UIManager | Frame | ScrollableRegion,
text: str,
x: int,
y: int,
width: int,
color: Tuple[int, int, int, int] = (255, 255, 255, 255),
text_color: Tuple[int, int, int, int] = (0, 0, 0, 255),
caret_color: Tuple[int, int, int] | Tuple[int, int, int, int] = (0, 0, 0, 255),
batch: Batch | None = None,
group: Group | None = None,
cursor: str | MouseCursor | None = 'text',
) → None

Bases: WidgetBase[str]

Instance of a text entry widget. Allows the user to enter and submit text.

Triggers the event ‘on_commit’, when the user hits the Enter or Return key. The current text string is passed along with the event.

Create a text entry widget.

Parameters:
  • text (str) – Initial text to display.

  • x (int) – X coordinate of the text entry widget.

  • y (int) – Y coordinate of the text entry widget.

  • width (int) – The width of the text entry widget.

  • color (Tuple[int, int, int, int]) – The color of the outline box in RGBA format.

  • text_color (Tuple[int, int, int, int]) – The color of the text in RGBA format.

  • caret_color (Tuple[int, int, int] | Tuple[int, int, int, int]) – The color of the caret (when it is visible) in RGBA or RGB format.

  • batch (Batch | None) – Optional batch to add the text entry widget to.

  • group (Group | None) – Optional parent group of text entry widget.

  • cursor (str | MouseCursor | None) – System cursor name or custom mouse cursor to show while hovering. Defaults to the text caret cursor.

on_commit(widget: TextEntry, text: str) → None

Event: dispatches the current text when committed via Enter/Return key.

Return type:

None

on_focus_gain() → None

Handle this widget becoming the UI manager’s focus target.

Return type:

None

on_focus_lost() → None

Handle this widget no longer being the UI manager’s focus target.

Return type:

None

property height: float

Height of the widget.

property value: str

Query or set the Widget’s value.

This property allows you to set the value of a Widget directly, without any user input. This could be used, for example, to restore Widgets to a previous state, or if some event in your program is meant to naturally change the same value that the Widget controls. Note that events are not dispatched when changing this property directly.

property width: float

Width of the widget.

class ScrollableRegion(
parent: UIManager | Frame | None,
x: int,
y: int,
width: int,
height: int,
window: BaseWindow | None = None,
*,
camera: BaseCamera | None = None,
view: _CameraViewBase | None = None,
batch: Batch | None = None,
group: Group | None = None,
horizontal: bool = True,
vertical: bool = True,
scroll_step: float = 40,
) → None

Bases: WidgetBase, LayoutCell[LayoutCellStyle]

Clip and scroll content using a child camera view.

Construct it with a UI manager or frame to receive input. Child widgets use the region as their constructor parent; their input is translated after the parent manager dispatches to the region. No nested manager is created. Pass a camera or view (or a window with a camera attribute). content_group applies the child view and clip to content.

on_mouse_leave_widget(x: int, y: int) → None

Clear hover state for children when the cursor leaves this region.

Return type:

None

on_resize(width: int, height: int) → None

Forward a window resize event through the region layout and its children.

Return type:

None

update_groups(order: int) → None

The region has no drawable of its own; content uses content_group.

Return type:

None

property aabb: tuple[float, float, float, float]

Bounding box of the widget.

The “left”, “bottom”, “right”, and “top” coordinates of the widget. This is expressed as (x, y, x + width, y + height).

property height: float

Height of the widget.

property input_parent: ScrollableRegion

Scrollable regions own input for widgets in their content layouts.

property position: tuple[float, float]

The x, y coordinate of the widget as a tuple.

property width: float

Width of the widget.

property x: float

X coordinate of the widget.

property y: float

Y coordinate of the widget.

Layouts

class LayoutCell(
style: StyleT | None = None,
batch: Batch | None = None,
group: Group | None = None,
) → None

A rectangular layout cell, optionally containing one drawable object.

Create a layout cell.

Parameters:
  • style (TypeVar(StyleT, bound= LayoutCellStyle) | None) – The cell’s visual and content-alignment style.

  • batch (Batch | None) – Optional batch for a generated background rectangle.

  • group (Group | None) – Optional group for a generated background rectangle.

on_resize(width: int, height: int) → None

Forward a window resize event to this cell’s content, if supported.

Return type:

None

set_style(style: StyleT) → None

Replace the cell style with a typed style object.

Return type:

None

property style: StyleT

The typed style applied to this cell.

class Layout(
parent: UIManager | Frame | Layout | ScrollableRegion,
x: float,
y: float,
width: float,
height: float,
rows: int,
columns: int,
style: LayoutStyle | None = None,
batch: Batch | None = None,
group: Group | None = None,
) → None

Bases: LayoutCell[LayoutStyle]

A grid layout with fixed, pixel, percentage, and flexible cell sizes.

parent establishes the layout tree at construction time. Use a frame, UI manager, another layout, or a scrollable region as the parent.

Create a layout cell.

Parameters:
  • style (LayoutStyle | None) – The cell’s visual and content-alignment style.

  • batch (Batch | None) – Optional batch for a generated background rectangle.

  • group (Group | None) – Optional group for a generated background rectangle.

on_resize(width: int, height: int) → None

Forward a window resize event through this layout’s grid.

Return type:

None

realign(new_rect: tuple[float, float, float, float] | None = None) → None

Immediately calculate this layout and clear its pending update.

Return type:

None

set_style(style: LayoutStyle) → None

Replace the layout style and apply it to every existing cell.

Return type:

None

property input_parent: UIManager | Frame | ScrollableRegion

The object that owns input for widgets placed in this layout.

property manager: UIManager | None

The UI manager derived from this layout’s parent.

property parent: UIManager | Frame | Layout | ScrollableRegion

The layout container that owns this layout.

property style: LayoutStyle

The typed style applied to this layout and its cells.

class HBox(
parent: UIManager | Frame | Layout | ScrollableRegion,
x: float,
y: float,
width: float,
height: float,
style: LayoutStyle | None = None,
batch: Batch | None = None,
group: Group | None = None,
) → None

Bases: _SingleSequenceLayout

Lay out content in a single horizontal row.

Append content with append(). Column widths, margins, padding, and content alignment are configured through LayoutStyle.

Create a horizontal layout.

Parameters:
  • parent (UIManager | Frame | Layout | ScrollableRegion) – The layout’s containing UI object.

  • x (float) – Left coordinate of the layout.

  • y (float) – Bottom coordinate of the layout.

  • width (float) – Layout width.

  • height (float) – Layout height.

  • style (LayoutStyle | None) – Optional layout style.

  • batch (Batch | None) – Optional batch for layout backgrounds.

  • group (Group | None) – Optional group for layout backgrounds and content.

property count: int

Number of content items in the sequence.

class VBox(
parent: UIManager | Frame | Layout | ScrollableRegion,
x: float,
y: float,
width: float,
height: float,
style: LayoutStyle | None = None,
batch: Batch | None = None,
group: Group | None = None,
) → None

Bases: _SingleSequenceLayout

Lay out content in a single vertical column.

Append content with append(). Row heights, margins, padding, and content alignment are configured through LayoutStyle.

Create a vertical layout.

Parameters:
  • parent (UIManager | Frame | Layout | ScrollableRegion) – The layout’s containing UI object.

  • x (float) – Left coordinate of the layout.

  • y (float) – Bottom coordinate of the layout.

  • width (float) – Layout width.

  • height (float) – Layout height.

  • style (LayoutStyle | None) – Optional layout style.

  • batch (Batch | None) – Optional batch for layout backgrounds.

  • group (Group | None) – Optional group for layout backgrounds and content.

property count: int

Number of content items in the sequence.

class Frame(
parent: UIManager,
x: float,
y: float,
width: float,
height: float,
rows: int = 1,
columns: int = 1,
style: LayoutStyle | None = None,
batch: Batch | None = None,
group: Group | None = None,
) → None

Bases: Layout

A visual layout container owned by one UIManager.

Create a layout cell.

Parameters:
  • style (LayoutStyle | None) – The cell’s visual and content-alignment style.

  • batch (Batch | None) – Optional batch for a generated background rectangle.

  • group (Group | None) – Optional group for a generated background rectangle.

remove_widget(widget: WidgetBase) → None

Remove a child widget from this frame.

Return type:

None

property input_parent: Frame

Frames own input for their direct and nested layout widgets.

property manager: UIManager

The UI manager that owns this frame’s input routing.

class MovableFrame(
parent: UIManager,
x: float,
y: float,
width: float,
height: float,
rows: int = 1,
columns: int = 1,
style: LayoutStyle | None = None,
batch: Batch | None = None,
group: Group | None = None,
modifier: int = 0,
) → None

Bases: Frame

A frame container that the UI manager moves while a modifier is held.

Create a layout cell.

Parameters:
  • style (LayoutStyle | None) – The cell’s visual and content-alignment style.

  • batch (Batch | None) – Optional batch for a generated background rectangle.

  • group (Group | None) – Optional group for a generated background rectangle.

move(dx: float, dy: float) → None

Move this frame and its children by dx, dy.

Return type:

None

Styles

class WidgetStyle(
background: Tuple[int, int, int] | Tuple[int, int, int, int] | Background | None = None,
) → None

Common visual style fields shared by GUI components.

background: Tuple[int, int, int] | Tuple[int, int, int, int] | Background | None = None

RGB/RGBA background color, a drawable background object, or None.

class ButtonStyle(
background: Tuple[int, int, int] | Tuple[int, int, int, int] | Background | None = None,
) → None

Base style for button widgets, reserved for shared button properties.

class TextButtonStyle(
background: Tuple[int, int, int] | Tuple[int, int, int, int] | Background | None = None,
pressed_color: Tuple[int, int, int] | Tuple[int, int, int, int] = (255, 0, 0),
unpressed_color: Tuple[int, int, int] | Tuple[int, int, int, int] = (255, 255, 255),
hover_color: Tuple[int, int, int] | Tuple[int, int, int, int] = (0, 255, 0),
) → None

Colors used by TextButton interaction states.

hover_color: Tuple[int, int, int] | Tuple[int, int, int, int] = (0, 255, 0)

RGB/RGBA text color while the button is hovered.

pressed_color: Tuple[int, int, int] | Tuple[int, int, int, int] = (255, 0, 0)

RGB/RGBA text color while the button is pressed.

unpressed_color: Tuple[int, int, int] | Tuple[int, int, int, int] = (255, 255, 255)

RGB/RGBA text color while the button is idle.

class LayoutCellStyle(
background: Tuple[int, int, int] | Tuple[int, int, int, int] | Background | None = None,
padding: int | tuple[int, int, int, int] = 0,
stretch_content: bool | tuple[bool, bool] = False,
content_alignment: str | tuple[Literal['left', 'center', 'right'], Literal['bottom', 'center', 'top']] = 'center',
) → None

Style for one layout cell and its contained object.

content_alignment: str | tuple[Literal['left', 'center', 'right'], Literal['bottom', 'center', 'top']] = 'center'

A horizontal value or (horizontal, vertical) content alignment tuple.

padding: int | tuple[int, int, int, int] = 0

Interior spacing as one integer or (top, right, bottom, left).

stretch_content: bool | tuple[bool, bool] = False

One boolean or (horizontal, vertical) flags for content stretching.

class LayoutStyle(
background: Tuple[int, int, int] | Tuple[int, int, int, int] | Background | None = None,
padding: int | tuple[int, int, int, int] = 0,
stretch_content: bool | tuple[bool, bool] = True,
content_alignment: str | tuple[Literal['left', 'center', 'right'], Literal['bottom', 'center', 'top']] = 'center',
cell_margin: int | float | tuple[float, float] = 1,
cell_background: Tuple[int, int, int] | Tuple[int, int, int, int] | Background | None = None,
cell_padding: int | tuple[int, int, int, int] = 0,
cell_stretch_content: bool | tuple[bool, bool] = False,
cell_content_alignment: str | tuple[Literal['left', 'center', 'right'], Literal['bottom', 'center', 'top']] = 'center',
row_size: int | float | str | None = None,
column_size: int | float | str | None = None,
) → None

Style for a layout and the cells it creates.

cell_background: Tuple[int, int, int] | Tuple[int, int, int, int] | Background | None = None

RGB/RGBA color, drawable object, or None for each cell background.

cell_content_alignment: str | tuple[Literal['left', 'center', 'right'], Literal['bottom', 'center', 'top']] = 'center'

Default cell content alignment.

cell_margin: int | float | tuple[float, float] = 1

Spacing between cells as one number or (horizontal, vertical).

cell_padding: int | tuple[int, int, int, int] = 0

Default cell padding as one integer or (top, right, bottom, left).

cell_stretch_content: bool | tuple[bool, bool] = False

Default cell content stretching flags.

column_size: int | float | str | None = None

Number, "Npx", "N%", or None for flexible column sizing.

row_size: int | float | str | None = None

Number, "Npx", "N%", or None for flexible row sizing.

stretch_content: bool | tuple[bool, bool] = True

Stretch this layout when it is content in another layout cell.