Skip to content

Latest commit

 

History

History
140 lines (97 loc) · 4.44 KB

create_menu.rst

File metadata and controls

140 lines (97 loc) · 4.44 KB

pygame_menu.menu

Creating menus

Ready to go deeper into menu usage?

Configuring the menu

The :pypygame_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.

Widgets alignment

By default, the widgets are centered horizontally (widget_alignment=ALIGN_CENTER). All are included in a virtual rectangle positioned at 0 pixel below the title bar and 0 pixel from the left border (widget_offset=(0, 0)).

The widget alignment (str) can be changed with one of the following values:

Alignment Description
:pypygame_menu.locals.ALIGN_LEFT Left alignment
:pypygame_menu.locals.ALIGN_CENTER Center alignment
:pypygame_menu.locals.ALIGN_RIGHT Right alignment

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 :pypygame_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.

On-close callback

A callback can be defined using the onclose parameter, it will be called when the menu (end sub-menus) is closing. This parameter can take one of these two types of values:

  • a python callable object (a function, a method, a class, ...) that will be called without any arguments.
  • a specific event of :pypygame_menu. The possible events are the following:

    Event Description
    :pypygame_menu.events.BACK Go back to the previously opened menu
    :pypygame_menu.events.DISABLE_CLOSE The menu can not be closed
    :pypygame_menu.events.EXIT Exit the program (not only the menu)
    :pypygame_menu.events.RESET Go back to the first opened menu

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 :pypygame_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()

pygame_menu.Menu