Note
Click here to download the full example code
Deocrate class methods with magicgui#
Demonstrates decorating a class method with magicgui.
Once the class is instantiated, instance.method_name
will return a FunctionGui
in which the instance will always be provided as the first argument (i.e. "self") when
the FunctionGui or method is called.
Out:
instance: a, counter: 0.0, sigma: 0.0
instance: b, counter: 0.0, sigma: 0.0
from magicgui import event_loop, magicgui
from magicgui.widgets import Container
class MyObject:
"""Example object class."""
def __init__(self, name):
self.name = name
self.counter = 0.0
@magicgui(auto_call=True)
def method(self, sigma: float = 0):
"""Example class method."""
print(f"instance: {self.name}, counter: {self.counter}, sigma: {sigma}")
self.counter = self.counter + sigma
return self.name
with event_loop():
a = MyObject("a")
b = MyObject("b")
container = Container(widgets=[a.method, b.method])
container.show()
assert a.method() == "a"
assert b.method() == "b"
Total running time of the script: ( 0 minutes 0.028 seconds)
Download Python source code: class_method.py