Creating menus
Ready to go deeper into menu usage?
Configuring the menu
The pygame_menu.menu.Menu is the base class to draw the graphical items
on the screen. It offers many parameters to let you adapt the behavior and the visual
aspects of the menu.
The less trivial ones are explained here.
Widget position
By default, the widgets are centered horizontally by theme, included in a virtual
rectangle positioned at 0 pixel below the title bar and 0 pixel from the left border
(widget_offset=(0, 0)).
In the same way, an offset can be defined for the title using the parameter
title_offset.
The content of the menu can be centered vertically after all widgets have been
added by calling the method pygame_menu.menu.Menu.center_content():
menu = pygame_menu.Menu(...)
menu.add.text_input(...)
menu.add.selector(...)
menu.add.button(...)
menu.center_content()
Note
If the menu size is insufficient to show all of the widgets, horizontal and/or vertical scrollbar(s) will appear automatically.
Column and row
By default, the widgets are arranged in one unique column. But using the
columns and rows parameters, it is possible to arrange them in a grid.
The defined grid of columns x rows cells will be completed with the
widgets (in order of definition) column by column starting at the top-left
corner of the menu.
Also the width of each column can be set using column_max_width and
column_min_width Menu parameters.
On-close callback
A callback can be defined using the onclose parameter; it will be called when
the menu (end sub-menu) is closing. Closing the menu is the same as disabling
it, but with callback firing.
onclose parameter can take one of these three types of values:
None, the menu don’t disables ifpygame_menu.menu.Menu.close()is calledA python callable object (a function, a method) that will be called without any arguments, or with the
Menuinstance.A specific event of
pygame_menu. The possible events are the following:
Event
Description
pygame_menu.events.BACKGo back to the previous menu and disable the current
pygame_menu.events.CLOSEOnly disables the current menu
pygame_menu.events.NONEThe same as
onclose=None
pygame_menu.events.EXITExit the program (not only the menu)
pygame_menu.events.RESETGo back to the first opened menu and disable the current
Display a menu
The First steps chapter shows the way to display the menu, this method lets
pygame-menu managing the event loop by calling the
pygame_menu.menu.Menu.mainloop():
def draw_background():
...
mymenu = Menu(...)
mymenu.mainloop(surface, bgfun=draw_background)
There is a second way that gives more flexibility to the application because the events loop remains managed outside of the menu. In this case the application is in charge to update and draw the menu when it is necessary.
def draw_background():
...
mymenu = Menu(...)
while True:
draw_background()
events = pygame.event.get()
for event in events:
if event.type == pygame.QUIT:
exit()
if mymenu.is_enabled():
mymenu.update(events)
mymenu.draw(surface)
pygame.display.update()
Menu API
- class pygame_menu.menu.Menu(title, width, height, center_content=True, column_max_width=None, column_min_width=0, columns=1, enabled=True, joystick_enabled=True, keyboard_enabled=True, menu_id='', mouse_enabled=True, mouse_motion_selection=False, mouse_visible=True, onclose=None, onreset=None, overflow=(True, True), position=(50, 50, True), rows=None, screen_dimension=None, theme=<pygame_menu.themes.Theme object>, touchscreen=False, touchscreen_motion_selection=False)[source]
Menu object.
Menu can receive many callbacks; callbacks
oncloseandonresetare fired (if them are callable-type). They can only receive 1 argument maximum, if so, the Menu instance is providedonclose(menu) <or> onclose() onreset(menu) <or> onreset()
Note
Menu cannot be copied or deep-copied.
- Parameters
title (
str) – Title of the Menucenter_content (
bool) – Auto centers the Menu on the vertical position after a widget is added/deletedcolumn_max_width (
Union[int,float,Tuple[Union[int,float], …],List[Union[int,float]],None]) – List/Tuple representing the maximum width of each column in px,Noneequals no limit. For examplecolumn_max_width=500(each column width can be 500px max), orcolumn_max_width=(400,500)(first column 400px, second 500). If0uses the Menu width. This method does not resize the widgets, only determines the dynamic width of the column layoutcolumn_min_width (
Union[int,float,Tuple[Union[int,float], …],List[Union[int,float]]]) – List/Tuple representing the minimum width of each column in px. For examplecolumn_min_width=500(each column width is 500px min), orcolumn_max_width=(400,500)(first column 400px, second 500). Negative values are not acceptedcolumns (
int) – Number of columnsenabled (
bool) – Menu is enabled. IfFalsethe Menu cannot be drawn or updatedjoystick_enabled (
bool) – Enable/disable joystick events on the Menukeyboard_enabled (
bool) – Enable/disable keyboard events on the Menumenu_id (
str) – ID of the Menumouse_enabled (
bool) – Enable/disable mouse click inside the Menumouse_motion_selection (
bool) – Select widgets using mouse motion. IfTruemenu draws afocuson the selected widgetmouse_visible (
bool) – Set mouse visible on Menuonclose (
Union[MenuAction,Callable[[Menu],Any],Callable[[],Any],None]) – Event or function executed when closing the Menu. If notNonethe menu disables and executes the event or function it points to. If a function (callable) is provided it can be both non-argument or single argument (Menu instance)onreset (
Union[Callable[[Menu],Any],Callable[[],Any],None]) – Function executed when resetting the Menu. The function must be non-argument or single argument (Menu instance)overflow (
Union[Tuple[bool,bool],List[bool],bool]) – Enables overflow on x/y axes. IfFalsethen scrollbars will not work and the maximum width/height of the scrollarea is the same as the Menu container. Style: (overflow_x, overflow_y). IfFalseorTruethe value will be set on both axisposition (
Union[Tuple[Union[int,float],Union[int,float]],List[Union[int,float]],Tuple[Union[int,float],Union[int,float],bool]]) – Position on x-axis and y-axis. If the value is only 2 elements, the position is relative to the window width (thus, values must be 0-100%); else, the third element defines if the position is relative or not. If(x, y, False)the values of(x, y)are in pxrows (
Union[int,Tuple[int, …],List[int],None]) – Number of rows of each column, if there’s only 1 columnNonecan be used for no-limit. Also a tuple can be provided for defining different number of rows for each column, for examplerows=10(each column can have a maximum 10 widgets), orrows=[2, 3, 5](first column has 2 widgets, second 3, and third 5)screen_dimension (
Union[Tuple[int,int],List[int],None]) – List/Tuple representing the dimensions the Menu should reference for sizing/positioning (width, height), ifNonepygame is queried for the display mode. This value defines thewindow_sizeof the Menutheme (
Theme) – Menu themetouchscreen (
bool) – Enable/disable touch action inside the Menu. Only available on pygame 2touchscreen_motion_selection (
bool) – Select widgets using touchscreen motion. IfTruemenu draws afocuson the selected widget
- center_content()[source]
Centers the content of the Menu vertically. This action rewrites
widget_offset.Note
If the height of the widgets is greater than the height of the Menu, the drawing region will cover all Menu inner surface.
Note
This is applied only to the base Menu (not the currently displayed, stored in
_currentpointer); for such behaviour apply topygame_menu.menu.Menu.get_current()object.- Return type
- Returns
Self reference
- clear(reset=True)[source]
Clears all widgets.
Note
This is applied only to the base Menu (not the currently displayed, stored in
_currentpointer); for such behaviour apply topygame_menu.menu.Menu.get_current()object.
- close()[source]
Closes the current Menu firing
onclosecallback. Ifcallback=Nonethis method does nothing.Warning
This method should not be used along
pygame_menu.menu.Menu.get_current(), for example,menu.get_current().reset(...).- Return type
- Returns
Trueif the Menu has executed theonclosecallback
- collide(event)[source]
Check if user event collides the Menu.
Note
This is applied only to the base Menu (not the currently displayed, stored in
_currentpointer); for such behaviour apply topygame_menu.menu.Menu.get_current()object.- Parameters
event (
Event) – Pygame event- Return type
- Returns
Trueif collide
- disable()[source]
Disables the Menu (doesn’t check events and draw on the surface).
Note
This method does not fires
onclosecallback. UseMenu.close()instead.- Return type
- Returns
Self reference
- draw(surface, clear_surface=False)[source]
Draw the current Menu into the given surface.
Warning
This method should not be used along
pygame_menu.menu.Menu.get_current(), for example,menu.get_current().draw(...)
- enable()[source]
Enables Menu (can check events and draw).
Note
This is applied only to the base Menu (not the currently displayed, stored in
_currentpointer); for such behaviour apply topygame_menu.menu.Menu.get_current()object.- Return type
- Returns
Self reference
- force_surface_cache_update()[source]
Forces current Menu surface cache to update after next drawing call.
Note
This method only updates the surface cache, without forcing re-rendering of all Menu widgets.
Warning
This method should not be used along
pygame_menu.menu.Menu.get_current(), for example,menu.get_current().update(...).- Return type
- Returns
Self reference
- force_surface_update()[source]
Forces current Menu surface update after next rendering call.
Note
This method is expensive, as menu surface update forces re-rendering of all widgets (because them can change in size, position, etc…).
Warning
This method should not be used along
pygame_menu.menu.Menu.get_current(), for example,menu.get_current().update(...).- Return type
- Returns
Self reference
- full_reset()[source]
Reset the Menu back to the first opened Menu.
Note
This is applied only to the base Menu (not the currently displayed, stored in
_currentpointer); for such behaviour apply topygame_menu.menu.Menu.get_current()object.- Return type
- Returns
Self reference
- get_clock()[source]
Return the pygame Menu timer.
Note
This is applied only to the base Menu (not the currently displayed, stored in
_currentpointer); for such behaviour apply topygame_menu.menu.Menu.get_current()object.- Return type
- Returns
Pygame clock object
- get_current()[source]
Get the current active Menu. If the user has not opened any submenu the pointer object must be the same as the base. If not, this will return the opened Menu pointer.
- Return type
- Returns
Menu object (current)
- get_decorator()[source]
Return the Menu decorator API.
Note
prevmenu decorator may not draw becausepygame_menu.widgets.MenuBarandpygame_menu._scrollarea.ScrollAreaobjects draw over it. If it’s desired to draw a decorator behind widgets, use the ScrollArea decorator, for example:menu.get_scrollarea().get_decorator().The menu drawing order is:
Menu background color/image
Menu
prevdecoratorMenu ScrollArea
prevdecoratorMenu ScrollArea widgets
Menu ScrollArea
postdecoratorMenu title
Menu
postdecorator
Note
This is applied only to the base Menu (not the currently displayed, stored in
_currentpointer); for such behaviour apply topygame_menu.menu.Menu.get_current()object.- Return type
- Returns
Decorator API
- get_height(inner=False, widget=False)[source]
Get the Menu height.
Note
This is applied only to the base Menu (not the currently displayed, stored in
_currentpointer); for such behaviour apply topygame_menu.menu.Menu.get_current()object.
- get_index()[source]
Get selected widget index from the Menu.
Note
This is applied only to the base Menu (not the currently displayed, stored in
_currentpointer); for such behaviour apply topygame_menu.menu.Menu.get_current()object.- Return type
- Returns
Selected widget index
- get_input_data(recursive=False)[source]
Return input data from a Menu. The results are given as a dict object. The keys are the ID of each element.
With
recursive=Trueit collect also data inside the all sub-menus.Note
This is applied only to the base Menu (not the currently displayed), for such behaviour apply to
pygame_menu.menu.Menu.get_current()object.
- get_last_update_mode()[source]
Return the update mode.
Warning
This method should not be used along
pygame_menu.menu.Menu.get_current(), for example,menu.get_current().update(...).
- get_menubar()[source]
Return menubar widget.
Note
This is applied only to the base Menu (not the currently displayed, stored in
_currentpointer); for such behaviour apply topygame_menu.menu.Menu.get_current()object.- Return type
- Returns
MenuBar widget
- get_mouseover_widget(filter_appended=True)[source]
Return the mouseover widget on the Menu.
Note
This is applied only to the base Menu (not the currently displayed, stored in
_currentpointer); for such behaviour apply topygame_menu.menu.Menu.get_current()object.
- get_position()[source]
Return the menu position (constructor + translation).
Note
This is applied only to the base Menu (not the currently displayed, stored in
_currentpointer); for such behaviour apply topygame_menu.menu.Menu.get_current()object.
- get_rect()[source]
Return the
pygame.Rectobject of the Menu.Note
This is applied only to the base Menu (not the currently displayed, stored in
_currentpointer); for such behaviour apply topygame_menu.menu.Menu.get_current()object.- Return type
- Returns
Rect
- get_scrollarea()[source]
Return the Menu ScrollArea.
Note
This is applied only to the base Menu (not the currently displayed, stored in
_currentpointer); for such behaviour apply topygame_menu.menu.Menu.get_current()object.- Return type
- Returns
ScrollArea object
- get_selected_widget()[source]
Return the selected widget on the Menu.
Note
This is applied only to the base Menu (not the currently displayed, stored in
_currentpointer); for such behaviour apply topygame_menu.menu.Menu.get_current()object.
- get_size(inner=False, widget=False)[source]
Return the Menu size as a tuple of (width, height) in px.
Note
This is applied only to the base Menu (not the currently displayed, stored in
_currentpointer); for such behaviour apply topygame_menu.menu.Menu.get_current()object.
- get_submenus(recursive=False)[source]
Return the Menu submenus as a tuple.
Note
This is applied only to the base Menu (not the currently displayed, stored in
_currentpointer); for such behaviour apply topygame_menu.menu.Menu.get_current()object.
- get_theme()[source]
Return the Menu theme.
Note
This is applied only to the base Menu (not the currently displayed, stored in
_currentpointer); for such behaviour apply topygame_menu.menu.Menu.get_current()object.Warning
Use with caution, changing the theme may affect other menus or widgets if not properly copied.
- Return type
- Returns
Menu theme
- get_title()[source]
Return the title of the Menu.
Note
This is applied only to the base Menu (not the currently displayed, stored in
_currentpointer); for such behaviour apply topygame_menu.menu.Menu.get_current()object.- Return type
- Returns
Menu title
- get_translate()[source]
Get Menu translate on x-axis and y-axis (x, y) in px.
Note
This is applied only to the base Menu (not the currently displayed, stored in
_currentpointer); for such behaviour apply topygame_menu.menu.Menu.get_current()object.
- get_widget(widget_id, recursive=False)[source]
Return a widget by a given ID from the Menu.
With
recursive=Trueit looks for a widget in the Menu and all sub-menus.Note
This is applied only to the base Menu (not the currently displayed, stored in
_currentpointer); for such behaviour apply topygame_menu.menu.Menu.get_current()object.Note
Noneis returned if no widget is found.
- get_widgets(ids=None)[source]
Return the Menu widgets as a tuple.
Note
This is applied only to the base Menu (not the currently displayed, stored in
_currentpointer); for such behaviour apply topygame_menu.menu.Menu.get_current()object.
- get_width(inner=False, widget=False)[source]
Get the Menu width.
Note
This is applied only to the base Menu (not the currently displayed, stored in
_currentpointer); for such behaviour apply topygame_menu.menu.Menu.get_current()object.
- get_window_size()[source]
Return the window size as a tuple of (width, height).
Note
This is applied only to the base Menu (not the currently displayed, stored in
_currentpointer); for such behaviour apply topygame_menu.menu.Menu.get_current()object.
- in_submenu(menu, recursive=False)[source]
Return
Trueifmenuis a submenu of the Menu.Note
This is applied only to the base Menu (not the currently displayed, stored in
_currentpointer); for such behaviour apply topygame_menu.menu.Menu.get_current()object.
- is_enabled()[source]
Return
Trueif the Menu is enabled.Note
This is applied only to the base Menu (not the currently displayed, stored in
_currentpointer); for such behaviour apply topygame_menu.menu.Menu.get_current()object.- Return type
- Returns
Menu enabled status
- mainloop(surface, bgfun=None, **kwargs)[source]
Main loop of the current Menu. In this function, the Menu handle exceptions and draw. The Menu pauses the application and checks
pygameevents itself.This method returns until the Menu is updated (a widget status has changed).
The execution of the mainloop is at the current Menu level.
menu = pygame_menu.Menu(...) menu.mainloop(surface)
The
bgfuncallable (if not None) can receive 1 argument maximum, if so, the Menu instance is provided:draw(...): bgfun(menu) <or> bgfun()
Finally, mainloop can be disabled externally if menu.disable() is called.
- kwargs (Optional)
clear_surface(bool) – IfTruesurface is cleared usingtheme.surface_clear_colordisable_loop(bool) – IfTruethe mainloop only runs once. Use for running draw and update in a single callfps_limit(int) – Maximum FPS of the loop. Default equals totheme.fps. If0there’s no limitwait_for_event(bool) – Holds the loop until an event is provided, useful to save CPU power
Warning
This method should not be used along
pygame_menu.menu.Menu.get_current(), for example,menu.get_current().mainloop(...).
- move_widget_index(widget, index=None, render=True, **kwargs)[source]
Move a given widget to a certain index.
indexcan be another widget, a numerical position, orNone; ifNonethe widget is pushed to the last widget list position.Note
This is applied only to the base Menu (not the currently displayed, stored in
_currentpointer); for such behaviour apply topygame_menu.menu.Menu.get_current()object.- Parameters
widget (
Optional[Widget]) – Widget to move. IfNonethe widgets are flipped or reversed and returnsNoneindex (
Union[Widget,int,None]) – Target index. It can be a widget, a numerical index, orNone; ifNonethe widget is pushed to the last positionrender (
bool) – Force menu rendering after updatekwargs – Optional keyword arguments
- Return type
- Returns
The new indices of the widget and the previous index element
- remove_widget(widget)[source]
Remove the
widgetfrom the Menu. If widget not exists on Menu this method raises aValueErrorexception.Note
This is applied only to the base Menu (not the currently displayed, stored in
_currentpointer); for such behaviour apply topygame_menu.menu.Menu.get_current()object.
- render()[source]
Force the current Menu to render. Useful to force widget update.
Note
This method should not be called if the Menu is being drawn as this method is called by
pygame_menu.menu.Menu.draw()Warning
This method should not be used along
pygame_menu.menu.Menu.get_current(), for example,menu.get_current().render(...)- Return type
- Returns
Self reference (current)
- reset(total)[source]
Go back in Menu history a certain number of times from the current Menu. This method operates through the current Menu pointer.
Warning
This method should not be used along
pygame_menu.menu.Menu.get_current(), for example,menu.get_current().reset(...).
- reset_value(recursive=False)[source]
Reset all widget values to default.
Note
This is applied only to the base Menu (not the currently displayed, stored in
_currentpointer); for such behaviour apply topygame_menu.menu.Menu.get_current()object.
- resize(width, height, screen_dimension=None, position=None)[source]
Resize the menu to another width/height
- Parameters
screen_dimension (
Union[Tuple[int,int],List[int],None]) – List/Tuple representing the dimensions the Menu should reference for sizing/positioning (width, height), ifNonepygame is queried for the display mode. This value defines thewindow_sizeof the Menuposition (
Union[Tuple[Union[int,float],Union[int,float]],List[Union[int,float]],Tuple[Union[int,float],Union[int,float],bool],None]) – Position on x-axis and y-axis. If the value is only 2 elements, the position is relative to the window width (thus, values must be 0-100%); else, the third element defines if the position is relative or not. If(x, y, False)the values of(x, y)are in px. IfNoneuse the default from the menu constructor
- Return type
- scroll_to_widget(widget, scroll_parent=True)[source]
Scroll the Menu to the given widget.
Note
This is applied only to the base Menu (not the currently displayed, stored in
_currentpointer); for such behaviour apply topygame_menu.menu.Menu.get_current()object.
- select_widget(widget)[source]
Select a widget from the Menu. If
Noneunselect the current one.Note
This is applied only to the base Menu (not the currently displayed, stored in
_currentpointer); for such behaviour apply topygame_menu.menu.Menu.get_current()object.
- set_absolute_position(position_x, position_y)[source]
Set the absolute Menu position.
Note
This is applied only to the base Menu (not the currently displayed, stored in
_currentpointer); for such behaviour apply topygame_menu.menu.Menu.get_current()object.
- set_onbeforeopen(onbeforeopen)[source]
Set
onbeforeopencallback. Callback is executed before opening the Menu, it receives the current Menu and the next Menu:onbeforeopen(current Menu <from>, next Menu <to>)
Note
This is applied only to the base Menu (not the currently displayed, stored in
_currentpointer); for such behaviour apply topygame_menu.menu.Menu.get_current()object.
- set_onclose(onclose)[source]
Set
onclosecallback. Callback can only receive 1 argument maximum (if notNone), if so, the Menu instance is provided:onclose(menu) <or> onclose()
Note
This is applied only to the base Menu (not the currently displayed, stored in
_currentpointer); for such behaviour apply topygame_menu.menu.Menu.get_current()object.
- set_onmouseleave(onmouseleave)[source]
Set
onmouseleavecallback. This method is executed inpygame_menu.menu.Menu.update()method. The callback function receives the following arguments:onmouseleave(menu, event) <or> onmouseleave()
- set_onmouseover(onmouseover)[source]
Set
onmouseovercallback. This method is executed inpygame_menu.menu.Menu.update()method. The callback function receives the following arguments:onmouseover(menu, event) <or> onmouseover()
- set_onreset(onreset)[source]
Set
onresetcallback. Callback can only receive 1 argument maximum (if notNone), if so, the Menu instance is provided:onreset(menu) <or> onreset()
Note
This is applied only to the base Menu (not the currently displayed, stored in
_currentpointer); for such behaviour apply topygame_menu.menu.Menu.get_current()object.
- set_onupdate(onupdate)[source]
Set
onupdatecallback. Callback is executed before updating the Menu, it receives the event list and the Menu reference; also,onupdatecan receive zero arguments:onupdate(event_list, menu) <or> onupdate()
Note
This is applied only to the base Menu (not the currently displayed, stored in
_currentpointer); for such behaviour apply topygame_menu.menu.Menu.get_current()object.
- set_onwidgetchange(onwidgetchange)[source]
Set
onwidgetchangecallback. This method is executed if any appended widget changes its value. The callback function receives the following arguments:onwidgetchange(menu, widget)
- set_onwindowmouseleave(onwindowmouseleave)[source]
Set
onwindowmouseleavecallback. This method is executed inpygame_menu.menu.Menu.update()method. The callback function receives the following arguments:onwindowmouseleave(menu) <or> onwindowmouseleave()
- set_onwindowmouseover(onwindowmouseover)[source]
Set
onwindowmouseovercallback. This method is executed inpygame_menu.menu.Menu.update()method. The callback function receives the following arguments:onwindowmouseover(menu) <or> onwindowmouseover()
- set_relative_position(position_x, position_y)[source]
Set the Menu position relative to the window.
Note
Menu left position (x) must be between
0and100, if0the margin is at the left of the window, if100the Menu is at the right of the window.Menu top position (y) must be between
0and100, if0the margin is at the top of the window, if100the margin is at the bottom of the window.
Note
This is applied only to the base Menu (not the currently displayed, stored in
_currentpointer); for such behaviour apply topygame_menu.menu.Menu.get_current()object.
- set_sound(sound, recursive=False)[source]
Add a sound engine to the Menu. If
recursive=True, the sound is applied to all submenus.Note
This is applied only to the base Menu (not the currently displayed, stored in
_currentpointer); for such behaviour apply topygame_menu.menu.Menu.get_current()object.
- set_title(title, offset=None)[source]
Set the title of the Menu.
Note
This is applied only to the base Menu (not the currently displayed, stored in
_currentpointer); for such behaviour apply topygame_menu.menu.Menu.get_current()object.
- toggle()[source]
Switch between enable/disable Menu.
Note
This is applied only to the base Menu (not the currently displayed, stored in
_currentpointer); for such behaviour apply topygame_menu.menu.Menu.get_current()object.- Return type
- Returns
Self reference
- translate(x, y)[source]
Translate to (+x, +y) according to the default position.
Note
To revert changes, only set to
(0, 0).Note
This is applied only to the base Menu (not the currently displayed, stored in
_currentpointer); for such behaviour apply topygame_menu.menu.Menu.get_current()object.
- unselect_widget()[source]
Unselects the current widget.
Note
This is applied only to the base Menu (not the currently displayed, stored in
_currentpointer); for such behaviour apply topygame_menu.menu.Menu.get_current()object.- Return type
- Returns
Self reference
- update(events)[source]
Update the status of the Menu using external events. The update event is applied only on the current Menu.
Warning
This method should not be used along
pygame_menu.menu.Menu.get_current(), for example,menu.get_current().update(...).