Direct Widget API

While the primary API is to use the @magicgui decorator, one can also instantiate widgets directly using any of the widgets in magicgui.widgets.

Generally, one will construct a Container object that acts as a layout for all of the sub-widgets.

from magicgui.widgets import SpinBox, FileEdit, Slider, Label, Container
from pathlib import Path

# make some widgets
spin_box = SpinBox(value=10, name='spin', label='Value:', max=100)
file_picker = FileEdit(value='some/path')
slider = Slider(value=30, min=20, max=40)
label = Label(value=slider.value)

# set up callbacks
@slider.changed.connect
def set_label(value: str):
    label.value = value


# create a container to hold the widgets:
container = Container(widgets=[spin_box, file_picker, slider, label])
container.show()
../_images/direct_api_1_0.png

Note, Container widgets are subclassed from python’s collections.abc.MutableSequence, so they behave like a basic python list. You can append, instert, pop widgets as you would with a regular list:

container.remove(file_picker)
container.pop(0)
container.insert(2, Label(value='a new label'))
container.show()