Widget

class pygame_menu.widgets.core.widget.Widget(title='', widget_id='', onchange=None, onreturn=None, args=None, kwargs=None)[source]

Widget abstract class.

Parameters:
  • title (str) – Widget title
  • widget_id (str) – Widget identifier
  • onchange (function, None) – Callback when changing the selector
  • onreturn (callable, None) – Callback when pressing return button
  • args – Optional arguments for callbacks
  • kwargs – Optional keyword-arguments for callbacks
apply(*args)[source]

Run on_return callback when return event. A callback function receives the following arguments:

callback_func( value, *args, *widget._args, **widget._kwargs )
with:
  • value (if something is returned by get_value())
  • args given to this method
  • args of the widget
  • kwargs of the widget
Parameters:args – Extra arguments passed to the callback
Returns:None
change(*args)[source]

Run on_change callback after change event is triggered. A callback function receives the following arguments:

callback_func( value, *args, *widget._args, **widget._kwargs )
with:
  • value (if something is returned by get_value())
  • args given to this method
  • args of the widget
  • kwargs of the widget
Parameters:args – Extra arguments passed to the callback
Returns:None
draw(surface)[source]

Draw the widget shape.

Parameters:surface (pygame.Surface) – Surface to draw
Returns:None
draw_selection(surface)[source]

Draw selection effect on widget.

Parameters:surface (pygame.Surface) – Surface to draw
Returns:None
get_alignment()[source]

Returns widget alignment.

Returns:Widget align, see locals
Return type:str
get_attribute(key, default)[source]

Get attribute value.

Parameters:
  • key – Key of the attribute
  • default – Value if does not exists
Returns:

Attribute data

get_font_info()[source]

Return a dict with the information of the widget font.

Returns:Dict, keys: size (int), name (str), color (tuple), selected_color (tuple), antialias (bool)
Return type:dict
get_id()[source]

Returns the widget ID.

Returns:ID
Return type:str
get_margin()[source]
Returns:Widget margin
Return type:tuple
get_menu()[source]

Return menu reference (if exists).

Returns:Menu reference
Return type:pygame_menu.Menu
get_rect()[source]

Return the Rect object, this forces the widget rendering

Returns:Widget rect
Return type:pygame.Rect
get_selected_time()[source]

Return time the widget has been selected in miliseconds. If the widget is not currently selected, return 0.

Returns:Time in ms
Return type:float
get_selection_effect()[source]
Returns:Selection effect
Return type:pygame_menu.widgets.core.Selection
get_title()[source]

Return the widget title.

Returns:Widget title
Return type:str
get_value()[source]

Return the value. If exception ValueError is raised, no value will be passed to the callbacks.

Returns:Value
Return type:Object
set_alignment(align)[source]

Set the alignment of the widget.

Parameters:align (str) – Widget align, see locals
Returns:None
set_attribute(key, value)[source]

Set widget attribute.

Parameters:
  • key – Key of the attribute
  • value – Value of the attribute
Returns:

None

set_background_color(color, inflate=(0, 0))[source]

Set widget background color.

Parameters:
Returns:

None

set_controls(joystick=True, mouse=True)[source]

Enable interfaces to control the widget.

Parameters:
  • joystick (bool) – Use joystick
  • mouse (bool) – Use mouse
Returns:

None

set_font(font, font_size, color, selected_color, background_color, antialias=True)[source]

Set the text font.

Parameters:
  • font (str, list) – Name or list of names for font (see pygame.font.match_font for precise format)
  • font_size (int) – Size of font in pixels
  • color (tuple) – Text color
  • selected_color (tuple) – Text color when widget is selected
  • background_color (tuple) – Font background color
  • antialias (bool) – Determines if antialias is applied to font (uses more processing power)
Returns:

None

set_margin(x, y)[source]

Set Widget margin.

Parameters:
Returns:

None

set_max_width(width)[source]

Set widget max width (column support) if force_fit_text is enabled.

Parameters:width (int, float, None) – Width in px, None if max width is disabled
Returns:None
set_menu(menu)[source]

Set menu reference.

Parameters:menu (pygame_menu.Menu) – Menu object
Returns:None
set_position(posx, posy)[source]

Set the position.

Parameters:
Returns:

None

set_selected(selected=True)[source]

Mark the widget as selected.

Parameters:selected (bool) – Set item as selected
Returns:None
set_selection_effect(selection)[source]

Set the selection effect handler.

Parameters:selection (pygame_menu.widgets.core.Selection) – Selection effect class
Returns:None
set_shadow(enabled=True, color=None, position=None, offset=None)[source]

Show text shadow.

Parameters:
  • enabled (bool) – Shadow is enabled or not
  • color (list, None) – Shadow color
  • position (str, None) – Shadow position
  • offset (int, float, None) – Shadow offset
Returns:

None

set_sound(sound)[source]

Set sound engine to the widget.

Parameters:sound (pygame_menu.sound.Sound) – Sound object
Returns:None
set_title(title)[source]

Update the widget title.

Parameters:title (str) – New title
Returns:None
set_value(value)[source]

Set the value.

Warning

This method does not fire the callbacks as it is called programmatically. This behavior is deliberately chosen to avoid infinite loops.

Parameters:value (Object) – Value to be set on the widget
Returns:None
surface_needs_update()[source]

Checks if the widget width/height has changed because events. If so, return true and set the status of the widget (menu widget position needs update) as false. This method is used by .update() from Menu class.

Returns:True if the widget position has changed by events after the rendering.
Return type:bool
update(events)[source]

Update internal variable according to the given events list and fire the callbacks.

Parameters:events (list[pygame.event.Event]) – List of pygame events
Returns:True if updated
Return type:bool