Source code for pygame_menu.widgets.widget.menulink


Similar to a Button that opens a Menu, MenuLink is a widget that contains a Menu
reference. This Menu can be opened with .open() method.

__all__ = [

import pygame_menu

from abc import ABC
from pygame_menu.widgets.core.widget import AbstractWidgetManager
from pygame_menu.widgets.widget.none import NoneWidget

from pygame_menu._types import Callable

# noinspection PyMissingOrEmptyDocstring

class MenuLinkManager(AbstractWidgetManager, ABC):
    MenuLink manager.

    def menu_link(
        menu: 'pygame_menu.Menu',
        link_id: str = ''
    ) -> 'pygame_menu.widgets.MenuLink':
        Adds a link to another Menu. The behaviour is similar to a button, but
        this widget is invisible, and cannot be selectable.

        Added menus can be opened using the ``.open()`` method. Opened menus change
        the state of the parent Menu (the current pointer).

        .. note::

            This is applied only to the base Menu (not the currently displayed,
            stored in ``_current`` pointer); for such behaviour apply to
            :py:meth:`` object.

        :param menu: Menu to be added as a link (the new submenu)
        :param link_id: ID of the menu link
        :return: Menu link widget
        :rtype: :py:class:`pygame_menu.widgets.MenuLink`
        if isinstance(menu, type(self._menu)):
            # Check for recursive
            if menu == self._menu or menu.in_submenu(self._menu, recursive=True):
                raise ValueError(
                    f'Menu "{menu.get_title()}" is already on submenu structure,'
                    f' recursive menus lead to unexpected behaviours. For '
                    f'returning to previous menu use '
                    f'event defining an optional back_count number of menus to '
                    f'return from, default is 1'

            raise ValueError('menu object is not a pygame_menu.Menu class')

        # noinspection PyProtectedMember
        widget = MenuLink(
        self._add_submenu(menu, widget)

        return widget