Skip to content
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

Disable call button while function is running #139

Merged
merged 1 commit into from
Jan 31, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion magicgui/widgets/_function_gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
Optional,
TypeVar,
Union,
cast,
)

from magicgui.application import AppRef
Expand Down Expand Up @@ -176,7 +177,19 @@ def __init__(
text = call_button if isinstance(call_button, str) else "Run"
self._call_button = PushButton(gui_only=True, text=text, name="call_button")
if not auto_call: # (otherwise it already gets called)
self._call_button.changed.connect(lambda e: self.__call__())

def _disable_button_and_call(val):
# disable the call button until the function has finished
self._call_button = cast(PushButton, self._call_button)
self._call_button.enabled = False
t, self._call_button.text = self._call_button.text, "Running..."
try:
self.__call__()
finally:
self._call_button.text = t
self._call_button.enabled = True

self._call_button.changed.connect(_disable_button_and_call)
self.append(self._call_button)

self._result_widget: Optional[LineEdit] = None
Expand Down