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.
Note
This widget does not accept scale/resize transformation.
- Parameters
title (str) – Title of the menubar
width (int, float) – Width of the widget, generally width of the Menu
back_box (bool) – Draw a back-box button on header
mode (int) – Mode of drawing the bar
onreturn (callable, None) – Callback when pressing the back-box button
args (any) – Optional arguments for callbacks
kwargs (dict, any) – Optional keyword arguments for callbacks
-
add_draw_callback
(draw_callback)¶ 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
draw_callback (callable) – Function
- Returns
Callback ID
- Return type
-
add_update_callback
(update_callback)¶ 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
update_callback (callable) – Function
- Returns
Callback ID
- Return type
-
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
Callback return value
- Return type
any
-
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
Callback return value
- Return type
any
-
draw
(surface)[source]¶ Draw the widget shape.
- Parameters
surface (
pygame.Surface
) – Surface to draw- Returns
None
-
draw_selection
(surface)¶ Draw selection effect on widget.
- Parameters
surface (
pygame.Surface
) – Surface to draw- Returns
None
-
expand_background_inflate_to_selection_effect
()¶ Expand background inflate to match the selection effect (the widget don’t require to be selected).
This is a permanent change; for dynamic purposes, depending if the widget is selected or not, setting
widget.selection_expand_background
toTrue
may help.Note
This method may have unexpected results with certain selection effects.
- Returns
None
-
flip
(x, y)¶ This method can flip the widget either vertically, horizontally, or both. Flipping a widget is non-destructive and does not change the dimensions.
Note
Flip is only applied after widget rendering. Thus, the changes are not immediate.
-
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
-
get_height
(apply_padding=True, apply_selection=False)¶ Return the widget height.
Warning
If the widget is not rendered, this method will return
0
.
-
get_menu
()¶ Return the menu reference (if exists).
Warning
Use with caution.
- Returns
Menu reference
- Return type
pygame_menu.Menu
, None
-
get_padding
()¶ Return the widget padding.
- Returns
Widget padding (top,right,bottom,left)
- Return type
-
get_rect
(apply_padding=True, 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
- Returns
Widget rect
- Return type
-
get_selected_time
()¶ Return time the widget has been selected in milliseconds. If the widget is not currently selected, return 0.
- Returns
Time in ms
- Return type
-
get_selection_effect
()¶ Return the selection effect.
Warning
Use with caution.
- Returns
Selection effect
- Return type
-
get_size
(apply_padding=True, apply_selection=False)¶ Return the widget size.
Warning
If the widget is not rendered this method might return
(0,0)
.
-
get_surface
()¶ Return widget surface.
Warning
Use with caution.
- Returns
Widget surface
- Return type
-
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
-
get_width
(apply_padding=True, apply_selection=False)¶ Return the widget width.
Warning
If the widget is not rendered, this method will return
0
.
-
has_attribute
(key)¶ Returns true if widget has the given attribute.
-
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
(callback_id)¶ Removes draw callback from ID.
- Parameters
callback_id (str) – Callback ID
- Returns
None
-
remove_update_callback
(callback_id)¶ Removes update callback from ID.
- Parameters
callback_id (str) – Callback ID
- Returns
None
-
resize
(width, height, smooth=False)¶ Set the widget size to another size.
Note
This method calls
widget.scale
method; thus, some widgets may not support this transformation.Note
Resize is only applied after widget rendering. Thus, the changes are not immediate.
-
rotate
(angle)¶ Unfiltered counterclockwise rotation. The angle argument represents degrees and can be any floating point value. Negative angle amounts will rotate clockwise.
Note
Not all widgets accepts rotation. Also this rotation only affects the text or images, the selection or background is not rotated.
Note
Rotation is only applied after widget rendering. Thus, the changes are not immediate.
-
scale
(width, height, smooth=True)[source]¶ Scale the widget to a desired width and height factor.
Note
Not all widgets are affected by scale.
Note
Scale is only applied after widget rendering. Thus, the changes are not immediate.
-
set_alignment
(align)¶ Set the alignment of the widget.
Note
Alignment is only applied when updating the widget position, done by Menu when rendering the surface. Thus, the alignment change is not immediate.
- 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_backbox_border_width
(width)[source]¶ Set backbox border width in px.
- Parameters
width (int) – Width in px
- Returns
None
-
set_background_color
(color, inflate=(0, 0))¶ Set widget background color.
- Parameters
color (tuple, list,
pygame_menu.baseimage.BaseImage
, None) – Widget background colorinflate (tuple, list, None) – Inflate background in (x,y). If
None
, the widget value is not updated
- Returns
None
-
set_controls
(joystick=True, mouse=True, touchscreen=True)¶ Enable interfaces to control the widget.
-
set_font
(font, font_size, color, selected_color, background_color, antialias=True)¶ Set the text font.
- Parameters
- Returns
None
-
set_margin
(x, y)¶ Set Widget margin (left, bottom).
-
set_max_width
(width)¶ Set widget max width (column support) if
force_fit_text
is enabled.
-
set_menu
(menu)¶ Set the menu reference.
- Parameters
menu (
pygame_menu.Menu
, None) – Menu object- Returns
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.
-
set_position
(posx, posy)¶ Set the widget position.
-
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 class- Returns
None
-
set_shadow
(enabled=True, color=None, position=None, offset=2)¶ Show text shadow.
-
set_sound
(sound)¶ Set sound engine to the widget.
- Parameters
sound (
pygame_menu.sound.Sound
) – Sound object- Returns
None
-
set_value
(value)¶ Set the widget 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
Menu.update()
.- Returns
True if the widget position has changed by events after the rendering.
- Return type
-
translate
(x, y)¶ Translate to (+x,+y) according to default position.
Note
To revert changes, only set to (0,0).
Note
Translate is only applied when updating the widget position (calling
pygame_menu.widgets.core.Widget.set_position()
. This is done by Menu when rendering the surface. Thus, the position change is not immediate.
-
update
(events)[source]¶ Update internal variable according to the given events list and fire the callbacks.
- Parameters
events (list[
pygame.event.Event
], tuple[pygame.event.Event
]) – List/Tuple of pygame events- Returns
True if updated
- Return type
-
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
Widget.get_font_info()
method.- Parameters
style (dict) – Font style dict
- Returns
None