magicgui.widgets.bases
#
The magicgui.widgets.bases
module contains the base classes for all widgets.
While most users will never instantiate these classes directly, the methods and properties of these classes are inherited by all widgets, and define the common API for all widgets. Therefore, it is worth being aware of the type of widget you are working with.
Summary#
Widget | Description |
---|---|
Widget |
Basic Widget, wrapping a class that implements WidgetProtocol. |
ButtonWidget |
Widget with a value, Wraps a widget implementing the ButtonWidgetProtocol. |
CategoricalWidget |
Widget with a value and choices. Wraps CategoricalWidgetProtocol. |
ContainerWidget |
Widget that can contain other widgets. |
DialogWidget |
Modal Container. |
MainWindowWidget |
Top level Application widget that can contain other widgets. |
RangedWidget |
Widget with a constrained value. Wraps RangedWidgetProtocol. |
SliderWidget |
Widget with a constrained value and orientation. Wraps SliderWidgetProtocol. |
ValueWidget |
Widget with a value, Wraps ValueWidgetProtocol. |
Class Hierarchy#
In visual form, the widget class hierarchy looks like this:
classDiagram
Widget <|-- ValueWidget
Widget <|-- ContainerWidget
BackendWidget ..|> WidgetProtocol : implements a
ValueWidget <|-- RangedWidget
ValueWidget <|-- ButtonWidget
ValueWidget <|-- CategoricalWidget
RangedWidget <|-- SliderWidget
Widget --* WidgetProtocol : controls a
<<Interface>> WidgetProtocol
class WidgetProtocol {
_mgui_get_X()
_mgui_set_X()
}
class Widget{
name: str
annotation: Any
label: str
tooltip: str
visible: bool
enabled: bool
native: Any
height: int
width: int
hide()
show()
close()
render()
}
class ValueWidget{
value: Any
changed: SignalInstance
bind(value, call) Any
unbind()
}
class RangedWidget{
value: float | tuple
min: float
max: float
step: float
adaptive_step: bool
range: tuple[float, float]
}
class SliderWidget{
orientation: str
}
class ButtonWidget{
value: bool
clicked: SignalInstance
text: str
}
class CategoricalWidget{
choices: List[Any]
}
class ContainerWidget{
widgets: List[Widget]
labels: bool
layout: str
margins: tuple[int, int, int, int]
reset_choices()
asdict() Dict[str, Any]
update(mapping)
}
click Widget href "#magicgui.widgets.bases.Widget"
click ValueWidget href "#magicgui.widgets.bases.ValueWidget"
click RangedWidget href "#magicgui.widgets.bases.RangedWidget"
click SliderWidget href "#magicgui.widgets.bases.SliderWidget"
click ButtonWidget href "#magicgui.widgets.bases.ButtonWidget"
click CategoricalWidget href "#magicgui.widgets.bases.CategoricalWidget"
click ContainerWidget href "#magicgui.widgets.bases.ContainerWidget"
Base Widget Classes#
magicgui.widgets.bases.Widget
#
Basic Widget, wrapping a class that implements WidgetProtocol.
Parameters:
-
widget_type
(type[WidgetProtocol]
) –A class implementing a widget protocol. Will be instantiated during init.
-
name
(str
, default:''
) –The name of the parameter represented by this widget. by default ""
-
annotation
(Any
, default:None
) –The type annotation for the parameter represented by the widget, by default
None
-
label
(str
, default:None
) –A string to use for an associated Label widget (if this widget is being shown in a
Container
widget, and labels are on). By default,name
will be used. Note:name
refers the name of the parameter, as might be used in a signature, whereas label is just the label for that widget in the GUI. -
tooltip
(str
, default:None
) –A tooltip to display when hovering over the widget.
-
visible
(bool
, default:None
) –Whether the widget is visible, by default
True
. -
enabled
(bool
, default:True
) –Whether the widget is enabled, by default
True
. -
gui_only
(bool
, default:False
) –If
True
, widget is excluded from any function signature representation. by defaultFalse
. (This will likely be deprecated.) -
parent
(Any
, default:None
) –Optional parent widget of this widget. CAREFUL: if a parent is set, and subsequently deleted, this widget will likely be deleted as well (depending on the backend), and will no longer be usable.
-
backend_kwargs
(dict
, default:None
) –keyword argument to pass to the backend widget constructor.
annotation: Any
property
writable
#
Return type annotation for the parameter represented by the widget.
ForwardRefs will be resolve when setting the annotation.
enabled: bool
property
writable
#
Whether widget is enabled (editable).
height: int
property
writable
#
Return the current height of the widget.
label: str
property
writable
#
Return a label to use for this widget when present in Containers.
max_height: int
property
writable
#
Get the maximum height of the widget.
max_width: int
property
writable
#
Get the maximum width of the widget.
min_height: int
property
writable
#
Get the minimum height of the widget.
min_width: int
property
writable
#
Get the minimum width of the widget.
native: Any
property
#
Return native backend widget.
Note this is the widget that contains the layout, and not any parent widgets of this (e.g. a parent widget that is used to enable scroll bars)
options: dict
property
#
Return options currently being used in this widget.
param_kind: inspect._ParameterKind
property
writable
#
Return :attr:inspect.Parameter.kind
represented by this widget.
Used in building signatures from multiple widgets, by default
:attr:~inspect.Parameter.POSITIONAL_OR_KEYWORD
parent: Widget
property
writable
#
Return the parent widget.
parent_changed: SignalInstance
property
#
Signal emitted when the parent of the widget changes.
root_native_widget: Any
property
#
Return the root native backend widget.
This can be different from the .native
widget if the layout
is a child of some other widget, e.g. a widget used to enable
scroll bars.
tooltip: str | None
property
writable
#
Get the tooltip for this widget.
visible: bool
property
writable
#
Return whether widget is visible.
widget_type: str
property
#
Return type of widget.
width: int
property
writable
#
Return the current width of the widget.
__repr__() -> str
#
Return representation of widget of instance.
close() -> None
#
Close widget.
hide() -> None
#
Hide widget.
alias for widget.visible = False
render() -> np.ndarray
#
Return an RGBA (MxNx4) numpy array bitmap of the rendered widget.
show(run: bool = False) -> Widget
#
Show widget.
alias for widget.visible = True
Parameters:
-
run
(bool
, default:False
) –Whether to start the application event loop, by default False
shown() -> Iterator[Application]
#
Context manager to show the widget.
magicgui.widgets.bases.ButtonWidget
#
Bases: ValueWidget[bool]
Widget with a value, Wraps a widget implementing the ButtonWidgetProtocol.
see ButtonWidgetProtocol.
Parameters:
-
value
(bool
, default:Undefined
) –The starting state of the widget.
-
text
(str
, default:None
) –The text to display on the button. If not provided, will use
name
. -
bind
(Callable[[ValueWidget], Any] | Any
, default:Undefined
) –A value or callback to bind this widget. If provided, whenever
widget.value
is accessed, the value provided here will be returned instead.bind
may be a callable, in which casebind(self)
will be returned (i.e. your bound callback must accept a single parameter, which is this widget instance). -
nullable
(bool
, default:False
) –If
True
, the widget will acceptsNone
as a valid value, by defaultFalse
. -
**base_widget_kwargs
(Any
, default:{}
) –All additional keyword arguments are passed to the base
magicgui.widgets.Widget
constructor.
magicgui.widgets.bases.CategoricalWidget
#
Bases: ValueWidget[T]
Widget with a value and choices. Wraps CategoricalWidgetProtocol.
Parameters:
-
value
(Any
, default:Undefined
) –The initially selected choice.
-
choices
(Enum, Iterable, or Callable
, default:()
) –Available choices displayed in the combo box.
-
bind
(Callable[[ValueWidget], Any] | Any
, default:Undefined
) –A value or callback to bind this widget. If provided, whenever
widget.value
is accessed, the value provided here will be returned instead.bind
may be a callable, in which casebind(self)
will be returned (i.e. your bound callback must accept a single parameter, which is this widget instance). -
nullable
(bool
, default:False
) –If
True
, the widget will acceptsNone
as a valid value, by defaultFalse
. -
**base_widget_kwargs
(Any
, default:{}
) –All additional keyword arguments are passed to the base
magicgui.widgets.Widget
constructor.
choices: tuple[T | None, ...]
property
writable
#
Available value choices for this widget.
current_choice: str
property
#
Return the text of the currently selected choice.
options: dict
property
#
Return options currently being used in this widget.
value: T
property
writable
#
Return current value of the widget.
__len__() -> int
#
Return the number of choices.
del_choice(choice_name: str) -> None
#
Delete the provided choice_name
and associated data.
get_choice(choice_name: str) -> T
#
Get data for the provided choice_name
.
reset_choices(*_: Any) -> None
#
Reset choices to the default state.
If self._default_choices is a callable, this may NOT be the exact same set of choices as when the widget was instantiated, if the callable relies on external state.
set_choice(choice_name: str, data: Any | None = None) -> None
#
Set data for the provided choice_name
.
magicgui.widgets.bases.ContainerWidget
#
Bases: Widget
, _OrientationMixin
, MutableSequence[WidgetVar]
Widget that can contain other widgets.
Wraps a widget that implements
ContainerProtocol
.
A ContainerWidget
behaves like a python list of Widget
objects. Subwidgets can be accessed using integer or slice-based indexing
(container[0]
), as well as by widget name (container.<widget_name>
). Widgets can
be added with append
or insert
, and removed with del
or pop
, etc...
There is a tight connection between a ContainerWidget
and an
inspect.Signature object,
just as there is a tight connection between individual Widgetobjects an
an :class:
inspect.Parameter object.
The signature representation of a ContainerWidget
(with the current settings as default values) is accessible with
the :meth:~ContainerWidget.__signature__
method (or by using
:func:inspect.signature
from the standard library)
For a ContainerWidget
subclass that is tightly coupled to a specific function
signature (as in the "classic" magicgui decorator), see
magicgui.widgets.FunctionGui.
Parameters:
-
widgets
(Sequence[Widget]
, default:()
) –A sequence of widgets with which to initialize the container, by default
None
. -
layout
(str
, default:'vertical'
) –The layout for the container. must be one of
{'horizontal', 'vertical'}
. by default "vertical" -
scrollable
(bool
, default:False
) –Whether to enable scroll bars or not. If enabled, scroll bars will only appear along the layout direction, not in both directions.
-
labels
(bool
, default:True
) –Whether each widget should be shown with a corresponding Label widget to the left, by default
True
. Note: the text for each widget defaults towidget.name
, but can be overridden by settingwidget.label
. -
**base_widget_kwargs
(Any
, default:{}
) –All additional keyword arguments are passed to the base
magicgui.widgets.Widget
constructor.
__signature__: MagicSignature
property
#
Return a MagicSignature object representing the current state of the gui.
labels: bool
property
writable
#
Whether widgets are presented with labels.
layout: str
property
writable
#
Return the layout of the widget.
margins: tuple[int, int, int, int]
property
writable
#
Return margin between the content and edges of the container.
__delattr__(name: str) -> None
#
Delete a widget by name.
__delitem__(key: int | slice) -> None
#
Delete a widget by integer or slice index.
__dir__() -> list[str]
#
Add subwidget names to the dir() call for this widget.
__getattr__(name: str) -> WidgetVar
#
Return attribute name
. Will return a widget if present.
__getitem__(key: int | str | slice) -> WidgetVar | MutableSequence[WidgetVar]
#
Get item by integer, str, or slice.
__len__() -> int
#
Return the count of widgets.
__repr__() -> str
#
Return a repr.
__setattr__(name: str, value: Any) -> None
#
Set attribute name
. Prevents changing widget if present, (use del).
__setitem__(key: Any, value: Any) -> NoReturn
#
Prevent assignment by index.
asdict() -> dict[str, Any]
#
Return state of widget as dict.
from_callable(obj: Callable, gui_options: dict | None = None, **kwargs: Unpack[ContainerKwargs]) -> Container
classmethod
#
Create a Container widget from a callable object.
In most cases, it will be preferable to create a FunctionGui
instead.
from_signature(sig: inspect.Signature, **kwargs: Unpack[ContainerKwargs]) -> Container
classmethod
#
Create a Container widget from an inspect.Signature object.
index(value: Any, start: int = 0, stop: int = 9223372036854775807) -> int
#
Return index of a specific widget instance (or widget name).
insert(key: int, widget: WidgetVar) -> None
#
Insert widget at key
.
remove(value: Widget | str) -> None
#
Remove a widget instance (may also be string name of widget).
reset_choices(*_: Any) -> None
#
Reset choices for all Categorical subWidgets to the default state.
If widget._default_choices is a callable, this may NOT be the exact same set of choices as when the widget was instantiated, if the callable relies on external state.
update(mapping: Mapping | Iterable[tuple[str, Any]] | None = None, **kwargs: Any) -> None
#
Update the parameters in the widget from a mapping, iterable, or kwargs.
magicgui.widgets.bases.DialogWidget
#
Bases: ContainerWidget
Modal Container.
exec() -> bool
#
Show the dialog, and block.
Return True if the dialog was accepted, False if rejected.
magicgui.widgets.bases.MainWindowWidget
#
Bases: ContainerWidget
Top level Application widget that can contain other widgets.
create_menu_item(menu_name: str, item_name: str, callback: Callable | None = None, shortcut: str | None = None) -> None
#
Create a menu item item_name
under menu menu_name
.
menu_name
will be created if it does not already exist.
magicgui.widgets.bases.RangedWidget
#
Bases: ValueWidget[T]
Widget with a constrained value. Wraps RangedWidgetProtocol.
Parameters:
-
value
(Any
, default:Undefined
) –The starting value for the widget.
-
min
(float
, default:Undefined
) –The minimum allowable value, by default 0 (or
value
ifvalue
is less than 0) -
max
(float
, default:Undefined
) –The maximum allowable value, by default 999 (or
value
ifvalue
is greater than 999) -
step
(float
, default:Undefined
) –The step size for incrementing the value, by default adaptive step is used
-
bind
(Callable[[ValueWidget], Any] | Any
, default:Undefined
) –A value or callback to bind this widget. If provided, whenever
widget.value
is accessed, the value provided here will be returned instead.bind
may be a callable, in which casebind(self)
will be returned (i.e. your bound callback must accept a single parameter, which is this widget instance). -
nullable
(bool
, default:False
) –If
True
, the widget will acceptsNone
as a valid value, by defaultFalse
. -
**base_widget_kwargs
(Any
, default:{}
) –All additional keyword arguments are passed to the base
magicgui.widgets.Widget
constructor.
adaptive_step: bool
property
writable
#
Whether the step size is adaptive.
Adaptive decimal step means that the step size will continuously be adjusted to one power of ten below the current value. So when the value is 1100, the step is set to 100, so stepping up once increases it to 1200. For 1200 stepping up takes it to 1300. For negative values, stepping down from -1100 goes to -1200.
max: float
property
writable
#
Maximum allowable value for the widget.
min: float
property
writable
#
Minimum allowable value for the widget.
options: dict
property
#
Return options currently being used in this widget.
range: tuple[float, float]
property
writable
#
Range of allowable values for the widget.
step: float | None
property
writable
#
Step size for widget values (None if adaptive step is turned on).
value(value: T) -> None
#
Set widget value, will raise Value error if not within min/max.
magicgui.widgets.bases.SliderWidget
#
Bases: RangedWidget[T]
, _OrientationMixin
Widget with a constrained value and orientation. Wraps SliderWidgetProtocol.
Parameters:
-
value
(Any
, default:Undefined
) –The starting value for the widget.
-
min
(float
, default:Undefined
) –The minimum allowable value, by default 0 (or
value
ifvalue
is less than 0) -
max
(float
, default:Undefined
) –The maximum allowable value, by default 999 (or
value
ifvalue
is greater than 999) -
step
(float
, default:Undefined
) –The step size for incrementing the value, by default adaptive step is used
-
orientation
((str, {'horizontal', 'vertical'})
, default:'horizontal'
) –The orientation for the slider, by default "horizontal"
-
readout
(bool
, default:True
) –Whether to show the editable spinbox next to the slider
-
tracking
(bool
, default:True
) –If tracking is enabled (the default), the slider emits the
changed
signal while the slider is being dragged. If tracking is disabled, the slider emits thechanged
signal only after the user releases the slider. -
bind
(Callable[[ValueWidget], Any] | Any
, default:Undefined
) –A value or callback to bind this widget. If provided, whenever
widget.value
is accessed, the value provided here will be returned instead.bind
may be a callable, in which casebind(self)
will be returned (i.e. your bound callback must accept a single parameter, which is this widget instance). -
nullable
(bool
, default:False
) –If
True
, the widget will acceptsNone
as a valid value, by defaultFalse
. -
**base_widget_kwargs
(Any
, default:{}
) –All additional keyword arguments are passed to the base
magicgui.widgets.Widget
constructor.
options: dict
property
#
Return options currently being used in this widget.
readout: bool
property
writable
#
Get visibility state of readout widget.
tracking: bool
property
writable
#
Return whether slider tracking is enabled.
If tracking is enabled (the default), the slider emits the changed() signal while the slider is being dragged. If tracking is disabled, the slider emits the changed() signal only when the user releases the slider.
magicgui.widgets.bases.ValueWidget
#
Widget with a value, Wraps ValueWidgetProtocol.
Parameters:
-
value
(Any
, default:Undefined
) –The starting value for the widget.
-
bind
(Callable[[ValueWidget], Any] | Any
, default:Undefined
) –A value or callback to bind this widget. If provided, whenever
widget.value
is accessed, the value provided here will be returned instead.bind
may be a callable, in which casebind(self)
will be returned (i.e. your bound callback must accept a single parameter, which is this widget instance). -
nullable
(bool
, default:False
) –If
True
, the widget will acceptsNone
as a valid value, by defaultFalse
. -
**base_widget_kwargs
(Any
, default:{}
) –All additional keyword arguments are passed to the base
magicgui.widgets.Widget
constructor.
annotation: Any
property
writable
#
Return type annotation for the parameter represented by the widget.
ForwardRefs will be resolve when setting the annotation. If the widget is nullable (had a type annototation of Optional[Type]), annotation will return the first argument in the Optional clause.
value: T
property
writable
#
Return current value of the widget. This may be interpreted by backends.
__repr__() -> str
#
Return representation of widget of instance.
bind(value: T | Callable[[ValueWidget], T], call: bool = True) -> None
#
Binds value
to self.value.
If a value is bound to this widget, then whenever widget.value
is accessed,
the value provided here will be returned. value
can be a callable, in which
case value(self)
will be returned (i.e. your callback must accept a single
parameter, which is this widget instance.).
If you provide a callable and you don't want it to be called (but rather just
returned as a callable object, then use call=False
when binding your value.
Note: if you need to access the "original" widget.value
within your
callback, please use widget.get_value()
instead of the widget.value
property, in order to avoid a RuntimeError.
Parameters:
-
value
(Any
) –The value (or callback) to return when accessing this widget's value.
-
call
(bool
, default:True
) –If
value
is a callable andcall
isTrue
, the callback will be called ascallback(self)
when accessingself.value
. IfFalse
, the callback will simply be returned as a callable object, by default,True
.
get_value() -> T
#
Callable version of self.value
.
The main API is to use self.value
, however, this is here in order to provide
an escape hatch if trying to access the widget's value inside of a callback
bound to self._bound_value.
unbind() -> None
#
Unbinds any bound values. (see ValueWidget.bind
).