-
Notifications
You must be signed in to change notification settings - Fork 53
Open
Description
Hi,
I wanted my widget to execute when I hit instead of clicking on the Run
button with my mouse. Asking ChatGPT, I solved the problem with the following code:
from magicgui import magicgui
from qtpy.QtWidgets import QLineEdit, QPushButton
@magicgui(call_button="Execute")
def my_widget(input: int = 0) -> int:
# This is where you define your plugin's functionality
print(f"Executed with input: {input}")
return input + 1
# Get access to the underlying QWidget
widget = my_widget.native
# Find the input field and button from the widget
input_field = widget.findChild(QLineEdit)
execute_button = widget.findChild(QPushButton)
# Save the original keyPressEvent to call it later
original_key_press_event = input_field.keyPressEvent
# Define a new keyPressEvent handler
def on_key_press(event):
if event.key() == 16777220: # Qt.Key_Enter or Qt.Key_Return
execute_button.click() # Trigger button click on Enter
else:
# Call the original keyPressEvent for other keys (letters, numbers, etc.)
original_key_press_event(event)
# Connect the keyPressEvent to the input field
input_field.keyPressEvent = on_key_press
# Add the widget to napari viewer (you must have napari viewer running)
viewer = napari.Viewer()
viewer.window.add_dock_widget(my_widget)
napari.run()
This works. But it seems overly complicated. Is there a way to accomplish the same by just setting a correct keyword argument? If not, can this be integrated as new feature?
Best,
Niklas
Metadata
Metadata
Assignees
Labels
No labels