pyglet.gui
Input management
- class UIManager(
- window: BaseWindow,
- enable: bool = True,
- cell_size: int = 64,
- order: int = 0,
- cursor: str | MouseCursor | None = None,
Bases:
objectOwn 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:
- on_key_release(symbol: int, modifiers: int) None
Pass the event to the focused widget, or widgets under the mouse.
- Return type:
- 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:
- 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:
- on_mouse_release(x: int, y: int, buttons: int, modifiers: int) None
Pass the event to any widgets that are currently active.
- Return type:
- 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:
- on_resize(width: int, height: int) None
Notify widgets of a frame resize, then rebuild the spatial hash.
- Return type:
- on_text(text: str) None
Pass the event to the focused widget, or widgets under the mouse.
- Return type:
- on_text_motion(motion: int) None
Pass the event to the focused widget, or widgets under the mouse.
- Return type:
- on_text_motion_select(motion: int) None
Pass the event to the focused widget, or widgets under the mouse.
- Return type:
- on_touch_end(
- pointer_id: int,
- x: float,
- y: float,
- dx: float,
- dy: float,
- width: float,
- height: float,
- pressure: float,
- scale: float,
Release widgets when the controlling contact ends.
- Return type:
- on_touch_move(
- pointer_id: int,
- x: float,
- y: float,
- dx: float,
- dy: float,
- width: float,
- height: float,
- pressure: float,
- scale: float,
Drag widgets with the contact that began the interaction.
- Return type:
- on_touch_start(
- pointer_id: int,
- x: float,
- y: float,
- dx: float,
- dy: float,
- width: float,
- height: float,
- pressure: float,
- scale: float,
Use the first touch contact to interact with widgets.
- Return type:
- remove_widget(widget: WidgetBase) None
Remove a Widget from the spatial hash.
- Return type:
- set_focus(widget: WidgetBase | None) None
Set the widget that receives keyboard and text input.
- Return type:
- property focused_widget: WidgetBase | None
The widget currently receiving keyboard and text input.
Widgets
- class WidgetBase(
- parent: UIManager | Frame | ScrollableRegion | None,
- x: float,
- y: float,
- width: float,
- height: float,
- cursor: str | MouseCursor | None = None,
Bases:
EventDispatcher,Generic[_ValueT]The base of all widgets.
- on_focus_lost() None
Handle this widget no longer being the UI manager’s focus target.
- Return type:
- on_mouse_enter_widget(x: int, y: int) None
Event handler called when the cursor enters this widget.
- Return type:
- on_mouse_leave_widget(x: int, y: int) None
Event handler called when the cursor leaves this widget.
- Return type:
- 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:
- 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 parent: UIManager | Frame | ScrollableRegion | None
The immediate container this widget belongs to.
The window-level
managerowns input and focus.
- 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.
- 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',
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:
- on_mouse_leave_widget(x: int, y: int) None
Event handler called when the cursor leaves this widget.
- Return type:
- on_press(widget: PushButton) None
Event: Dispatched when the button is clicked.
- Return type:
- on_release(widget: PushButton) None
Event: Dispatched when the button is released.
- Return type:
- 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',
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:
- on_mouse_leave_widget(x: int, y: int) None
Event handler called when the cursor leaves this widget.
- Return type:
- on_press(widget: TextButton) None
Event: Dispatched when the button is clicked.
- Return type:
- on_release(widget: TextButton) None
Event: Dispatched when the button is released.
- Return type:
- 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',
Bases:
PushButtonInstance 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:
- on_toggle(widget: ToggleButton, value: bool) None
Event: returns True or False to indicate the current state.
- Return type:
- 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',
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.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:
- 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',
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:
- on_focus_lost() None
Handle this widget no longer being the UI manager’s focus target.
- Return type:
- 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.
- 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,
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
cameraorview(or a window with acameraattribute).content_groupapplies 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:
- on_resize(width: int, height: int) None
Forward a window resize event through the region layout and its children.
- Return type:
- update_groups(order: int) None
The region has no drawable of its own; content uses
content_group.- Return type:
- 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 input_parent: ScrollableRegion
Scrollable regions own input for widgets in their content layouts.
Layouts
- class LayoutCell( ) None
A rectangular layout cell, optionally containing one drawable object.
Create a layout cell.
- Parameters:
- on_resize(width: int, height: int) None
Forward a window resize event to this cell’s content, if supported.
- Return type:
- 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,
Bases:
LayoutCell[LayoutStyle]A grid layout with fixed, pixel, percentage, and flexible cell sizes.
parentestablishes 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:
- on_resize(width: int, height: int) None
Forward a window resize event through this layout’s grid.
- Return type:
- realign(new_rect: tuple[float, float, float, float] | None = None) None
Immediately calculate this layout and clear its pending update.
- Return type:
- set_style(style: LayoutStyle) None
Replace the layout style and apply it to every existing cell.
- Return type:
- property input_parent: UIManager | Frame | ScrollableRegion
The object that owns input for widgets placed in this layout.
- 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,
Bases:
_SingleSequenceLayoutLay out content in a single horizontal row.
Append content with
append(). Column widths, margins, padding, and content alignment are configured throughLayoutStyle.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.
- 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,
Bases:
_SingleSequenceLayoutLay out content in a single vertical column.
Append content with
append(). Row heights, margins, padding, and content alignment are configured throughLayoutStyle.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.
- 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,
Bases:
LayoutA visual layout container owned by one
UIManager.Create a layout cell.
- Parameters:
- remove_widget(widget: WidgetBase) None
Remove a child widget from this frame.
- Return type:
- 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,
Bases:
FrameA frame container that the UI manager moves while a modifier is held.
Create a layout cell.
- Parameters:
Styles
- 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),
Colors used by
TextButtoninteraction states.- hover_color: Tuple[int, int, int] | Tuple[int, int, int, int] = (0, 255, 0)
RGB/RGBA text color while the button is hovered.
- 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',
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.
- 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,
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
Nonefor 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).