-
Notifications
You must be signed in to change notification settings - Fork 49
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cannot connect event callbacks to MagicFactory #155
Comments
@tlambert03 I'm confused... Why doesn't the namespace mangling you did for me in #117 work here? ie I would have expected this to work, since from magicgui import magic_factory
@magic_factory
def func(x: int):
func.x.changed.connect(lambda *args: print(args))
widget = func()
widget.show(run=True) Indeed this doesn't work but I don't understand why. |
that doesn't work because the function itself hasn't been called yet. from magicgui import magic_factory
@magic_factory
def func(x: int):
func.x.changed.connect(lambda *args: print(args))
widget = func()
widget() # this would make it work ... but now we're connecting it _every_ time
widget.show(run=True) |
for the sake of completeness, I'll mention something here that I mentioned on zulip: from magicgui.widgets import FunctionGui
def my_function(...):
...
class MyGui(FunctionGui):
def __init__(self):
param_options = {
...
}
super().__init__(my_function, call_button=True, layout='vertical', param_options=param_options)
# do your connections here with self. |
Maybe that's a stupid question, but what's the purpose of a dedicated from magicgui import magicgui, magic_factory
@magic_factory()
def func(x: int):
pass
def func_wrapper():
@magicgui()
def func(x: int):
pass
# func.x.changed.connect(lambda *args: print(args))
return func
print(func())
print(func_wrapper()) |
It's just a convenience function. Think of it like @magic_factory(call_button=True)
def my_factory(x: int):
...
widget = my_factory()
widget2 = my_factory(call_button=False, x={'widget_type': 'Slider'}) We originally added it for @jni's use case in affinder (where it actually was quite convenient because he didn't need to create event callbacks before the function itself was actually called). |
Keep in mind that magicgui is being developed with the hope/intention that it be useful beyond just napari. So it's really this special case with Outside of napari, you can definitely imagine someone using this factory, and then being able to connect events to the widget instances, in which case the factory is a convenient way to create multiple widgets based on a single template (the original function), which are possibly a bit different from each other (based on the arguments to the factory call itself) |
That makes sense, thanks for the explanation. |
yeah, I'm thinking that's a nice way to keep the functional interface. Will add it soon. Thanks for revealing the need! |
As noticed by @uschmidt83, the
@magic_factory
decorator is not compatible with the current callback connection approach (widget.param.changed.connect
). That's a big bummer and needs to be fixed ASAP. One possibility would be to allow for MagicFactory to accept awidget_init
function, that would be called with the widget instance each time a new one is created...The text was updated successfully, but these errors were encountered: