1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
|
"""# Basic widgets demo
Widget demonstration with magicgui.
This code demonstrates a few of the widget types that magicgui can create
based on the parameter types in your function.
"""
import datetime
from enum import Enum
from pathlib import Path
from magicgui import magicgui
class Medium(Enum):
"""Enum for various media and their refractive indices."""
Glass = 1.520
Oil = 1.515
Water = 1.333
Air = 1.0003
@magicgui(
main_window=True,
call_button="Calculate",
layout="vertical",
result_widget=True,
slider_float={"widget_type": "FloatSlider", "max": 100},
slider_int={"widget_type": "Slider", "readout": False},
radio_option={
"widget_type": "RadioButtons",
"orientation": "horizontal",
"choices": [("first option", 1), ("second option", 2)],
},
filename={"label": "Pick a file:"},
)
def widget_demo(
boolean=True,
integer=1,
spin_float=3.14159,
slider_float=43.5,
slider_int=550,
string="Text goes here",
dropdown=Medium.Glass,
radio_option=2,
date=datetime.date(1999, 12, 31),
time=datetime.time(1, 30, 20),
datetime=datetime.datetime.now(),
filename=Path.home(),
):
"""We can use numpy docstrings to provide tooltips.
Parameters
----------
boolean : bool, optional
A checkbox for booleans, by default True
integer : int, optional
Some integer, by default 1
spin_float : float, optional
This one is a float, by default "pi"
slider_float : float, optional
Hey look! I'm a slider, by default 43.5
slider_int : float, optional
I only take integers, and I've hidden my readout, by default 550
string : str, optional
We'll use this string carefully, by default "Text goes here"
dropdown : Enum, optional
Pick a medium, by default Medium.Glass
radio_option : int
A set of radio buttons.
date : datetime.date, optional
Your birthday, by default datetime.date(1999, 12, 31)
time : datetime.time, optional
Some time, by default datetime.time(1, 30, 20)
datetime : datetime.datetime, optional
A very specific time and date, by default ``datetime.datetime.now()``
filename : str, optional
Pick a path, by default Path.home()
"""
return locals().values()
widget_demo.changed.connect(print)
widget_demo.show(run=True)
|