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 if pygame_menu.menu.Menu.close() is called

  • A python callable object (a function, a method) that will be called without any arguments, or with the Menu instance.

  • A specific event of pygame_menu. The possible events are the following:

    Event

    Description

    pygame_menu.events.BACK

    Go back to the previous menu and disable the current

    pygame_menu.events.CLOSE

    Only disables the current menu

    pygame_menu.events.NONE

    The same as onclose=None

    pygame_menu.events.EXIT

    Exit the program (not only the menu)

    pygame_menu.events.RESET

    Go 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()