MenuBar¶
-
class
pygame_menu.widgets.
MenuBar
(title, width, background_color, back_box=False, mode=1000, offsetx=0.0, offsety=0.0, onreturn=None, *args, **kwargs)[source]¶ Bases:
pygame_menu.widgets.core.widget.Widget
MenuBar widget.
Parameters: - title (str) – Title of the menubar
- width (int, float) – Width of the widget, generally width of the Menu
- background_color (tuple, list) – Background color
- back_box (bool) – Draw a back-box button on header
- mode (int) – Mode of drawing the bar
- offsetx (int, float) – Offset x-position of title (px)
- offsety (int, float) – Offset y-position of title (px)
- onreturn (callable, None) – Callback when pressing the back-box button
- args (any) – Optional arguments for callbacks
- kwargs (dict) – Optional keyword arguments for callbacks
-
add_draw_callback
(func)¶ Adds a function to the widget to be executed each time the widget is drawn.
The function that this method receives receives two objects: the widget itself and the menu reference.
import math def draw_update_function(widget, menu): t = widget.get_attribute('t', 0) t += menu.get_clock().get_time() widget.set_padding(10*(1 + math.sin(t)))) # Oscillating padding button = menu.add_button('This button updates its padding', None) button.set_draw_callback(draw_update_function)
After creating a new callback, this functions returns the ID of the call. It can be removed anytime using widget.remove_draw_callback(id).
Parameters: func (callable) – Function Returns: Call ID Return type: str
-
add_update_callback
(func)¶ Adds a function to the widget to be executed each time the widget is updated.
The function that this method receives receives two objects: the widget itself and the menu reference. It is similar to
add_draw_callback
.After creating a new callback, this functions returns the ID of the call. It can be removed anytime using widget.remove_update_callback(id).
Note
Not all widgets are updated, so the provided function may never be executed.
Parameters: func (callable) – Function Returns: Call ID Return type: str
-
apply
(*args)¶ 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 byget_value()
)args
given to this methodargs
of the widgetkwargs
of the widget
Parameters: - args – Extra arguments passed to the callback
- args – any
Returns: None
-
apply_draw_callbacks
()¶ Apply callbacks on widget draw.
Returns: None
-
apply_update_callbacks
()¶ Apply callbacks on widget update.
Returns: None
-
change
(*args)¶ 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 byget_value()
)args
given to this methodargs
of the widgetkwargs
of the widget
Parameters: - args – Extra arguments passed to the callback
- args – any
Returns: None
-
draw
(surface)[source]¶ Draw the widget shape.
Parameters: surface ( pygame.Surface
) – Surface to drawReturns: None
-
draw_selection
(surface)¶ Draw selection effect on widget.
Parameters: surface ( pygame.Surface
) – Surface to drawReturns: None
-
get_attribute
(key, default=None)¶ Get attribute value.
Parameters: - key (str) – Key of the attribute
- default (any) – Value if does not exists
Returns: Attribute data
Return type: any
-
get_font_info
()¶ Return a dict with the information of the widget font.
Dict values:
antialias
Font antialias (bool)background_color
Background color (tuple)color
Font color (tuple)name
Name of the font (str)selected_color
Selected color (tuple)size
Size of the font (int)
Returns: Dict Return type: dict
-
get_menu
()¶ Return the menu reference (if exists).
Returns: Menu reference Return type: pygame_menu.Menu
, None
-
get_padding
()¶ Return the widget padding.
Returns: Widget padding (top, right, bottom, left) Return type: tuple
-
get_rect
(inflate=None)¶ Return the Rect object, this forces the widget rendering.
Note
This is the only method that returns the rect with the padding applied. If widget._rect is used, the padding has not been applied.
Parameters: inflate (None, tuple, list) – Inflate rect (x,y) in px Returns: Widget rect Return type: pygame.Rect
-
get_selected_time
()¶ 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
()¶ Return the selection effect.
Returns: Selection effect Return type: pygame_menu.widgets.core.Selection
-
get_value
()¶ Return the value. If exception
ValueError
is raised, no value will be passed to the callbacks.Returns: Widget data value Return type: Object
-
has_attribute
(key)¶ Returns true if widget has the given attribute.
Parameters: key (str) – Key of the attribute Returns: True if exists Return type: bool
-
hide
()¶ Hides widget.
Returns: None
-
remove_attribute
(key)¶ Removes the given attribute from the widget. Throws
IndexError
if given key does not exist.Parameters: key (str) – Key of the attribute Returns: None
-
remove_draw_callback
(callid)¶ Removes draw callback from ID.
Parameters: callid (str) – Callback ID Returns: None
-
remove_update_callback
(callid)¶ Removes update callback from ID.
Parameters: callid (str) – Callback ID Returns: None
-
set_alignment
(align)¶ Set the alignment of the widget.
Parameters: align (str) – Widget align, see locals Returns: None
-
set_attribute
(key, value)¶ Set widget attribute.
Parameters: - key (str) – Key of the attribute
- value (any) – Value of the attribute
Returns: None
-
set_background_color
(color, inflate=(0, 0))¶ Set widget background color.
Parameters: - color (tuple, list,
pygame_menu.baseimage.BaseImage
, None) – Widget background color - inflate (tuple, list, None) – Inflate background in x,y. If None, the widget value is not updated
Returns: None
- color (tuple, list,
-
set_controls
(joystick=True, mouse=True, touchscreen=True)¶ Enable interfaces to control the widget.
Parameters: Returns: None
-
set_font
(font, font_size, color, selected_color, background_color, antialias=True)¶ Set the text font.
Parameters: - font (str) – Font name (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
- font (str) – Font name (see
-
set_margin
(x, y)¶ Set Widget margin (left, top).
Parameters: Returns: None
-
set_max_width
(width)¶ 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)¶ Set the menu reference.
Parameters: menu ( pygame_menu.Menu
) – Menu objectReturns: None
-
set_padding
(padding)[source]¶ Set the Widget padding according to CSS rules.
- If an integer or float is provided: top, right, bottom and left values will be the same
- If 2-item tuple is provided: top and bottom takes the first value, left and right the second
- If 3-item tuple is provided: top will take the first value, left and right the second, and bottom the third
- If 4-item tuple is provided: padding will be (top, right, bottom, left)
Note
See CSS W3Schools for more info about padding.
Parameters: padding (int, float, tuple, list) – Can be a single number, or a tuple of 2, 3 or 4 elements following CSS style Returns: None
-
set_position
(posx, posy)¶ Set the position.
Parameters: Returns: None
-
set_selected
(selected=True)¶ Mark the widget as selected.
Parameters: selected (bool) – Set item as selected Returns: None
-
set_selection_effect
(selection)¶ Set the selection effect handler.
Parameters: selection ( pygame_menu.widgets.core.Selection
) – Selection effect classReturns: None
-
set_shadow
(enabled=True, color=None, position=None, offset=None)¶ Show text shadow.
Parameters: Returns: None
-
set_sound
(sound)¶ Set sound engine to the widget.
Parameters: sound ( pygame_menu.sound.Sound
) – Sound objectReturns: None
-
set_value
(value)¶ 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
-
show
()¶ Set widget visible.
Returns: None
-
surface_needs_update
()¶ 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 eventsReturns: True if updated Return type: bool
-
update_font
(style)¶ Updates font. This method receives a style dict (non empty) containing the following keys:
antialias
Font antialias (bool)background_color
Background color (tuple)color
Font color (tuple)name
Name of the font (str)selected_color
Selected color (tuple)size
Size of the font (int)
Note
If a key is not defined it will be rewritten using current font style from
.get_font_info()
method.Parameters: style (dict) – Font style dict Returns: None