spyder.api.widgets.main_widget#
Main plugin widget.
SpyderDockablePlugin plugins must provide a WIDGET_CLASS attribute that is a subclass of PluginMainWidget.
Classes
|
Spyder plugin main widget class. |
- class spyder.api.widgets.main_widget.PluginMainWidget(
- name: str,
- plugin: SpyderPluginV2,
- parent: spyder.app.mainwindow.MainWindow | None = None,
Bases:
QWidget,SpyderWidgetMixinSpyder plugin main widget class.
This class handles both a dockwidget pane and a floating window widget (undocked pane).
Note
All
SpyderDockablePlugins define a main widget that must be a subclass of this class.Notes
This widget is a subclass of
QWidgetthat consists of a single central widget and a set of toolbars stacked above it.The toolbars are not movable nor floatable and must occupy the entire horizontal space available for the plugin. This mean that toolbars must be stacked vertically and cannot be placed horizontally next to each other.
- ENABLE_SPINNER: bool = False#
Enable/disable showing a progress spinner on the top right of the toolbar.
If
True, an extra space will be added to the toolbar (even if the spinner is not moving) to avoid items jumping to the left/right when the spinner appears. IfFalseno extra space will be added.The spinner is shown to the left of the Options (hamburger) menu.
Plugins that provide actions that take time should make this
Trueand use thestart_spinner()/stop_spinner()methods accordingly.Examples
The Find in Files plugin (
spyder.plugins.findinfilesis an example of a core plugin that uses it.
- CONTEXT_NAME: str | None = None#
The name under which to store actions, toolbars, toolbuttons and menus.
This optional attribute defines the context name under which actions, toolbars, toolbuttons and menus should be registered in the Spyder global registry.
If those elements belong to the global scope of the plugin, then this attribute should have a
Nonevalue, which will use the plugin’s name as the context scope.
- SHOW_MESSAGE_WHEN_EMPTY: bool = False#
Enable or (by default) disable showing a message when the widget is empty.
Note
If
True, you need to set at least theMESSAGE_WHEN_EMPTYattribute as well.Examples
The Find in Files plugin is an example of a core plugin that uses it.
- MESSAGE_WHEN_EMPTY: str | None = None#
The main message, as a string, that will be shown when the widget is empty.
Must be set to a string when
SHOW_MESSAGE_WHEN_EMPTYisTrue, and has no effect if that attribute isFalse.Examples
The Find in Files plugin is an example of a core plugin that uses it.
- IMAGE_WHEN_EMPTY: str | None = None#
Name of or path to an SVG image to show when the widget is empty.
If
None(the default), no image is shown.Only shown when
SHOW_MESSAGE_WHEN_EMPTYis set toTrue.Note
This needs to be an SVG file so that it can be rendered correctly on high-resolution screens.
Examples
The Find in Files plugin is an example of a core plugin that uses it.
- DESCRIPTION_WHEN_EMPTY: str | None = None#
Additional text shown below the main message when the widget is empty.
If
None(the default), no additional text is shown.Only shown when
SHOW_MESSAGE_WHEN_EMPTYis set toTrue, and shown belowMESSAGE_WHEN_EMPTY.Examples
The Find in Files plugin is an example of a core plugin that uses it.
- SET_LAYOUT_WHEN_EMPTY: bool = True#
Use a vertical layout for the stack holding the empty and content widgets.
Set this to
Falseif you need to use a more complex layout in your widget;Trueis the default behavior.Examples
The Debugger plugin is an example of a core plugin that uses it.
- sig_free_memory_requested: Signal#
Signal to request the main application garbage-collect deleted objects.
- sig_restart_requested: Signal#
Signal to request the main Spyder application quit and restart itself.
- sig_redirect_stdio_requested(bool): Signal#
Request the main app redirect standard out/error within file pickers.
This will redirect
stdin,stdout, andstderrwhen using Open, Save, and Browse dialogs within a plugin’s widgets.- Parameters:
enable (bool) – Enable (
True) or disable (False) standard input/output redirection.
- sig_exception_occurred(PyQt_PyObject): Signal#
Signal to report an exception from a plugin.
- Parameters:
error_data (dict[str, str | bool]) –
The dictionary containing error data. The expected keys are:
error_data = { "text": str, "is_traceback": bool, "repo": str, "title": str, "label": str, "steps": str, }
The
is_tracebackkey indicates iftextcontains plain text or a Python error traceback.The
titleandrepokeys indicate how the error data should customize the report dialog and GitHub error submission.The
labelandstepskeys allow customizing the content of the error dialog.
- sig_toggle_view_changed(bool): Signal#
Signal to report that visibility of a dockable plugin has changed.
This is triggered by checking/unchecking the entry for a pane in the menu.
- Parameters:
visible (bool) – Whether the widget has been shown (
True) or hidden (False).
- sig_update_ancestor_requested: Signal#
Notify the main window that a child widget needs its ancestor updated.
- sig_unmaximize_plugin_requested(), (PyQt_PyObject): Signal#
Request the main window unmaximize the currently maximized plugin, if any.
If emitted without arguments, it’ll unmaximize any plugin.
- Parameters:
plugin_instance (spyder.api.plugins.SpyderDockablePlugin) – Unmaximize current plugin only if it is not
plugin_instance.
- sig_focus_status_changed(bool): Signal#
Signal to report a change in the focus state of this widget.
- Parameters:
status (bool) –
Trueif the widget is now focused;Falseif it is not.
- __init__(
- name: str,
- plugin: SpyderPluginV2,
- parent: spyder.app.mainwindow.MainWindow | None = None,
Create a new main widget for a plugin.
The widget is created automatically by Spyder, and is not intended to be instantiated manually.
- Parameters:
name (str) – The name of the plugin, i.e. the
SpyderPluginV2.NAME.plugin (SpyderPluginV2) – The plugin object this is to be the container class of.
parent (spyder.app.mainwindow.MainWindow | None, optional) – The container’s parent widget, normally the Spyder main window. By default (
None), no parent widget (used for testing).
- Return type:
None
- Raises:
SpyderAPIError – If
SHOW_MESSAGE_WHEN_EMPTYis set toTruebutMESSAGE_WHEN_EMPTYis not set to a non-empty string.
- closeEvent(event: QCloseEvent) None[source]#
Handle closing the widget.
- Parameters:
event (QCloseEvent) – The event object closing this widget.
- Return type:
None
- focusInEvent(event: QFocusEvent) None[source]#
Handle the widget gaining focus.
- Parameters:
event (QFocusEvent) – The focus event object.
- Return type:
None
- focusOutEvent(event: QFocusEvent) None[source]#
Handle the widget losing focus.
- Parameters:
event (QFocusEvent) – The focus event object.
- Return type:
None
- get_plugin() SpyderPluginV2[source]#
Return the parent plugin of this widget.
- Returns:
The parent plugin of this widget.
- Return type:
- get_action( ) spyder.utils.qthelpers.SpyderAction[source]#
Return an action by name, context and plugin.
- Parameters:
name (str) – Identifier of the action to retrieve.
context (str | None, optional) – Context identifier under which the action was stored. If
None, the default, then theCONTEXT_NAMEattribute is used instead.plugin (str | None, optional) – Identifier of the plugin in which the action was defined. If
None, the default, then thePLUGIN_NAMEattribute is used instead.
- Returns:
- The corresponding action stored under the given
name, contextandplugin.
- The corresponding action stored under the given
- Return type:
spyder.utils.qthelpers.SpyderAction
- Raises:
KeyError – If the combination of
name,contextandpluginkeys does not exist in the action registry.
- add_corner_widget(
- action_or_widget: spyder.utils.qthelpers.SpyderAction | QWidget,
- before: spyder.utils.qthelpers.SpyderAction | QWidget | None = None,
Add a widget to the corner toolbar.
By default, widgets are added to the left of the last toolbar item. Corner widgets provide an options menu button and a spinner so any additional widgets will be placed the left of the spinner, if visible (unless
beforeis set).- Parameters:
widget (spyder.utils.qthelpers.SpyderAction | QWidget) – The action or widget to add to the toolbar.
before (spyder.utils.qthelpers.SpyderAction | QWidget | None, optional) – The action or widget to add
widgetbefore (to the right of). IfNone(the default), the widget will be added to the left of the left-most widget.
- Return type:
None
- Raises:
SpyderAPIError – If either
widgetorbeforelacks anameattribute; a widget with the samenameaswidgetwas already added; a widget withbefore.namehas not been added previously; or the first widget added is not the options (hamburger) menu widget.
- get_corner_widget(
- name: str,
Return a widget by its unique ID (i.e. its
nameattribute).
- create_toolbar(
- toolbar_id: str,
Create and add an auxiliary toolbar to the top of the plugin.
- Parameters:
toolbar_id (str) – The unique identifier name of this toolbar.
- Returns:
The auxiliary toolbar object that was created.
- Return type:
Return the options (“hamburger”) menu for this widget.
- Returns:
The options (“hamburger”) menu widget.
- Return type:
Return the options menu button for this widget.
- Returns:
The button widget for the plugin options (“hamburger”) menu.
- Return type:
- get_main_toolbar() MainWidgetToolbar[source]#
Return the main toolbar of this widget.
- Returns:
The main toolbar of the widget that contains the options button.
- Return type:
- get_auxiliary_toolbars() OrderedDict[MainWidgetToolbar][source]#
Return the auxiliary toolbars of this widget.
- Returns:
A dictionary with toolbar IDs as keys and their corresponding auxiliary toolbar widgets as values.
- Return type:
OrderedDict[MainWidgetToolbar]
- set_icon_size(icon_size: int) None[source]#
Set the icon size of this widget’s toolbars.
- Parameters:
iconsize (int) – An integer corresponding to the size in pixels to which the icons of the plugin’s toolbars need to be set.
- show_status_message(message: str, timeout: int) None[source]#
Show a message in the Spyder status bar.
- get_focus_widget() PluginMainWidget[source]#
Get the widget to give focus to.
This is called when this widget’s dockwidget is raised to the top.
- Returns:
The widget to give focus to.
- Return type:
- update_margins(margin=None)[source]#
Update the margins of this widget’s central widget.
- Parameters:
margin (int | None) – The margins to use for the central widget, or
Nonefor the default margins.- Return type:
None
- set_name(name: str) None[source]#
Set this widget’s name.
Note
Normally, this is set to the same as the plugin’s name,
SpyderPluginV2.NAME.- Parameters:
name (str) – The name to set.
- Return type:
None
- get_name() str[source]#
Return this widget’s name.
By default, the same as the plugin’s name,
SpyderPluginV2.NAME.- Returns:
The name of the widget, and normally the plugin as well.
- Return type:
- set_icon(icon: QIcon) None[source]#
Set this widget’s icon.
- Parameters:
icon (QIcon) – The icon object to set as the widget’s icon.
- Return type:
None
- render_toolbars() None[source]#
Render all toolbars of this widget.
Caution
This action can only be performed once.
- Return type:
None
- set_content_widget( ) None[source]#
When there is an empty message, set the widget for actual content,
- show_content_widget() None[source]#
Show the widget that displays actual content instead of the empty one.
- Return type:
None
- dock_window() None[source]#
Dock an undocked window with this widget back to the main window.
- Return type:
None
- close_window() None[source]#
Close undocked window when clicking on the close window button.
This can either dock or hide the window, depending on whether the user hid the window before:
The default behavior is to dock the window, so that new users can experiment with the dock/undock functionality without surprises.
If the user closes the window by clicking on the Close action in the widget’s options (“hamburger”) menu or by going to the menu, then we will hide it when they click on the close button again. That gives users the ability to show/hide panes without docking/undocking them first.
- Return type:
None
- change_visibility( ) None[source]#
Raise this widget to the foreground, and/or grab its focus.
- Parameters:
state (bool) – Whether the widget is being raised to the foreground (
True) or set as not in the foreground (False). The latter does not actually send it to the background, but does configure it for not being actively shown (e.g. it disables its empty pane widget, if any).force_focus (bool | None, optional) – If
True, always give the widget keyboard focus when raising or un-raising it with this method. IfNone, only give it focus when showing, not hiding (settingstatetoTrue), and only ifSpyderDockablePlugin.RAISE_AND_FOCUSisTrue. IfFalse, the default, don’t give it focus regardless.
- Return type:
None
- toggle_view(checked: bool) None[source]#
Show or hide this widget in the Spyder interface.
Used to show or hide it from the from the menu.
- Parameters:
value (bool) – Whether to show (
True) or hide (False) this widget.- Return type:
None
Notes
If you need to attach some functionality when this changes, use
sig_toggle_view_changed. For an example, please seespyder.plugins.onlinehelp.widgets.
- create_dockwidget(
- mainwindow: spyder.app.mainwindow.MainWindow,
Add this widget to the parent Spyder main window as a dock widget.
- Parameters:
mainwindow (spyder.app.mainwindow.MainWindow) – The main window to set as the dockwidget’s parent.
- Returns:
spyder.widgets.dock.SpyderDockWidget – The newly created dock widget.
Qt.DockWidgetArea – The area of the window the dockwidget is placed in.
- lock_unlock_position() None[source]#
Show/hide title bar to move/lock this widget’s position.
- Return type:
None
- get_maximized_state() bool[source]#
Get this widget’s maximized state.
- Returns:
Trueif the widget is maximized,Falseotherwise.- Return type:
- set_maximized_state(state: bool) None[source]#
Set the attribute that holds this widget’s maximized state.
- Parameters:
state (bool) –
Trueto set the widget as maximized,Falseset it as not maximized.- Return type:
None
- get_title() str[source]#
Return the title that will be displayed on dockwidgets or windows.
- Returns:
This dockwidget’s tab/window title.
- Return type:
- Raises:
NotImplementedError – If the main widget subclass doesn’t define a
get_titlemethod.
- set_ancestor(ancestor: QWidget) None[source]#
Update the ancestor/parent of child widgets when undocking.
- Parameters:
ancestor (QWidget) – The window widget to set as a parent of this one.
- Return type:
None
- setup() None[source]#
Create widget actions, add to menus and perform other setup steps.
- Return type:
None
- Raises:
NotImplementedError – If the main widget subclass doesn’t define a
setupmethod.
- update_actions() None[source]#
Update the state of exposed actions.
Exposed actions are actions created by the
create_action()method.- Return type:
None
- Raises:
NotImplementedError – If the subclass doesn’t define an
update_actionsmethod.
- on_close() None[source]#
Perform actions before the widget is closed.
Does nothing by default; intended to be overridden for widgets that need to perform actions on close.
Warning
This method must only operate on local attributes.
- Return type:
None