spyder.api.widgets.menus#

Spyder API menu widgets.

Module Attributes

MENU_SEPARATOR

Constant representing a separator line between groups in a menu.

T

Generic SpyderMenu type variable (TypeVar).

Classes

OptionsMenuSections()

Pseudo-enum listing sections in the pane options (hamburger) menu.

PluginMainWidgetMenus()

Pseudo-enum listing the menu types dockable plugin main widgets have.

PluginMainWidgetOptionsMenu([parent, ...])

Options menu for PluginMainWidget.

SpyderMenu([parent, menu_id, title, ...])

A QMenu subclass implementing additional functionality for Spyder.

SpyderMenuProxyStyle

Menu style adjustments that can only be done with a proxy style.

spyder.api.widgets.menus.MENU_SEPARATOR: None = None#

Constant representing a separator line between groups in a menu.

class spyder.api.widgets.menus.T#

Generic SpyderMenu type variable (TypeVar).

alias of TypeVar(‘T’, bound=SpyderMenu)

class spyder.api.widgets.menus.OptionsMenuSections[source]#

Bases: object

Pseudo-enum listing sections in the pane options (hamburger) menu.

Top: str = 'top_section'#

The top section of the options menu.

Bottom: str = 'bottom_section'#

The bottom section of the options menu.

class spyder.api.widgets.menus.PluginMainWidgetMenus[source]#

Bases: object

Pseudo-enum listing the menu types dockable plugin main widgets have.

Context: str = 'context_menu'#

The widget context menu.

Options: str = 'options_menu'#

The widget options (hamburger) menu.

class spyder.api.widgets.menus.SpyderMenuProxyStyle[source]#

Bases: QProxyStyle

Menu style adjustments that can only be done with a proxy style.

Deprecated since version 6.2: This class will be moved to the private _SpyderMenuProxyStyle in Spyder 6.2, while the current public name will become an alias raising a DeprecationWarning on use, and removed in 7.0.

It was never intended to be used directly by plugins, and its functionality is automatically inherited by using SpyderMenu.

pixelMetric(
metric: PixelMetric,
option: QStyleOption | None = None,
widget: QWidget | None = None,
) int[source]#

Calculate the value of the given pixel metric.

This is a callback intended to be called internally by Qt.

Parameters:
  • metric (PixelMetric) – The pixel metric to calculate.

  • option (QStyleOption | None, optional) – The current style options, or None (default).

  • widget (QWidget | None, optional) – The widget the pixel metric will be used for, or None (default).

Returns:

The resulting pixel metric value, used internally by Qt.

Return type:

int

class spyder.api.widgets.menus.SpyderMenu(
parent: QWidget | None = None,
menu_id: str | None = None,
title: str | None = None,
min_width: int | None = None,
reposition: bool = True,
)[source]#

Bases: QMenu, SpyderFontsMixin

A QMenu subclass implementing additional functionality for Spyder.

All menus in Spyder must inherit from this class.

MENUS: list[tuple[QWidget | None, str | None, SpyderMenu]] = []#

List of 3-tuples, one per menu, describing each menu.

Contains the menu’s parent widget object, its name as a string, and the SpyderMenu object itself, in that order.

APP_MENU: bool = False#

Whether this is a main application menu or a plugin menu.

Set this to True if this is an application menu; False otherwise.

HORIZONTAL_MARGIN_FOR_ITEMS: int = 6#

The QSS horizontal (left/right) margin for menu items, in pixels.

HORIZONTAL_PADDING_FOR_ITEMS: int = 9#

The QSS horizontal (left/right) padding for menu items, in pixels.

__init__(
parent: QWidget | None = None,
menu_id: str | None = None,
title: str | None = None,
min_width: int | None = None,
reposition: bool = True,
)[source]#

Create a menu for Spyder.

Parameters:
  • parent (QWidget | None, optional) – The menu’s parent widget, or None if no parent.

  • menu_id (str | None, optional) – Unique string identifier for the menu, or None if no ID.

  • title (str | None, optional) – Localized title for the menu, or None (default) if no title.

  • min_width (int or None, optional) – Minimum width for the menu, or None (default) for no min width.

  • reposition (bool, optional) – Whether to vertically reposition the menu due to its padding. True by default.

Return type:

None

menu_id: str | None#

The unique string identifier for the menu (or None if unset).

clear_actions() None[source]#

Remove actions from the menu (including custom references).

Return type:

None

add_action(
action: spyder.utils.qthelpers.SpyderAction | SpyderMenu,
section: str | None = None,
before: str | None = None,
before_section: str | None = None,
check_before: bool = True,
omit_id: bool = False,
) None[source]#

Add action to a given menu section.

Parameters:
  • action (spyder.utils.qthelpers.SpyderAction | SpyderMenu) – The action or menu object to add to the menu.

  • section (str | None, optional) – The section id in which to insert the action, or None (default) for no section.

  • before (str | None, optional) – Make the action appear before the action with the identifier before. If None (default), add it to the end.

  • before_section (str | None, optional) – Make the item section appear prior to before_section. If None (the default), add the section to the end.

  • check_before (bool, optional) – Check if the before action is already part of the menu before adding this one, and if so save it to be added later. This is necessary to avoid an infinite recursion when adding unintroduced actions with this method again. True by default.

  • omit_id (bool, optional) – If False, the default, then the menu will check if action.action_id exists and is set to a string, and raise an AttributeError if either is not the case. If True, it will add the action anyway.

Return type:

None

Raises:

AttributeError – If omit_id is False (the default) and action.action_id does not exist or is not set to a string.

remove_action(item_id: str) None[source]#

Remove the action with the given string identifier.

Parameters:

item_id (str) – The string identifier of the action to remove.

Return type:

None

get_title() str | None[source]#

Return the title for the menu.

Returns:

The menu’s title, or None if it doesn’t have one set.

Return type:

str | None

get_actions() list[SpyderMenu | spyder.utils.qthelpers.SpyderAction][source]#

Return a parsed list of menu items/actions.

Includes a MENU_SEPARATOR between each defined menu section.

Returns:

The list of menu items/actions for this menu.

Return type:

list[SpyderMenu | spyder.utils.qthelpers.SpyderAction]

get_sections() tuple[str, ...][source]#

Return a tuple of menu section names.

Returns:

An arbitrary-length tuple of menu section names.

Return type:

tuple[str, …]

render(force: bool = False) None[source]#

Create the menu prior to showing it. This takes into account sections and location of menus.

Parameters:

force (bool, optional) – Whether to force rendering the menu.

__str__() str[source]#

Output this menu’s class name and identifier as a string.

Returns:

The menu’s class name and string identifier, in the format :file:SpyderMenu({MENU_ID}).

Return type:

str

__repr__() str[source]#

Output this menu’s class name and identifier as a string.

Returns:

The menu’s class name and string identifier, in the format :file:SpyderMenu({MENU_ID}).

Return type:

str

showEvent(event: QShowEvent) None[source]#

Perform adjustments when the menu is going to be shown.

Parameters:

event (QShowEvent) – The event object showing the menu.

Return type:

None

class spyder.api.widgets.menus.PluginMainWidgetOptionsMenu(
parent: QWidget | None = None,
menu_id: str | None = None,
title: str | None = None,
min_width: int | None = None,
reposition: bool = True,
)[source]#

Bases: SpyderMenu

Options menu for PluginMainWidget.

render() None[source]#

Render the menu’s bottom section as expected.

Return type:

None