MagicGUI
magicgui is a Python library that automatically generates graphical user interfaces (GUIs) from functions, primarily leveraging type hints for widget inference. It aims to make GUI creation simple and declarative, supporting various GUI backends (like Qt, wxPython, GTK, or custom) via `qtpy` and `app-model`. The current stable version is 0.10.2, with active development and frequent releases.
Common errors
-
RuntimeError: No Qt backend found, please install qtpy and a Qt binding.
cause magicgui could not find any installed Qt binding (PyQt5, PySide2, PyQt6, PySide6) or `qtpy` (which abstracts them).fixInstall `magicgui` with the `qt` extra: `pip install magicgui[qt]` or manually install `qtpy` and a specific binding: `pip install qtpy pyqt6`. -
AttributeError: 'FunctionGui' object has no attribute 'show_dialog'
cause The `show_dialog` method was deprecated and removed in favor of `show()` in recent versions of magicgui (0.8.0+).fixReplace calls to `function_gui_instance.show_dialog()` with `function_gui_instance.show()`. -
TypeError: magicgui arguments must be hashable and bound to a function... (or similar TypeError related to decorator arguments)
cause This often occurs when arguments passed to the `@magicgui` decorator are incorrect, or when attempting to use the decorator on a method without proper binding or `self` handling.fixEnsure all arguments passed to `@magicgui(...)` are valid keyword arguments as per the decorator's API. If decorating a method, consider using `bound=True` if you're not explicitly passing the `self` instance when calling the function. Review the `magicgui` decorator signature in the documentation. -
My magicgui window appears but immediately closes / is unresponsive.
cause The GUI event loop (e.g., QApplication.exec_() for Qt) is not running or not properly engaged to keep the window alive and interactive.fixFor standalone scripts, make sure you explicitly start the event loop after creating and showing your magicgui window(s). For example, `app = QApplication(sys.argv); my_gui.show(); sys.exit(app.exec_())`.
Warnings
- gotcha magicgui requires a GUI backend (like Qt/PyQt/PySide) and `qtpy` to function. Without at least one installed, you'll encounter a `RuntimeError: No Qt backend found...`.
- gotcha magicgui relies heavily on Python type hints to infer appropriate widgets. Missing, incorrect, or ambiguous type hints will result in default widgets (e.g., `QLineEdit` for strings without an explicit annotation for `str` type), or unexpected UI behavior.
- gotcha If your magicgui window appears and immediately closes, or is unresponsive, it's likely that the GUI event loop for your chosen backend (e.g., `QApplication.exec_()` for Qt) is not running or not properly integrated.
- breaking Significant API changes occurred in versions >= 0.8.0, particularly around `app-model` integration. The `bind` decorator for argument injection was removed, `show_dialog` was deprecated, and direct `FunctionGui` instantiation from `magicgui.widgets` became less common/recommended.
Install
-
pip install magicgui -
pip install magicgui[qt]
Imports
- magicgui
from magicgui import magicgui
- Container
from magicgui.widgets import Container
- FunctionGui (direct instantiation)
from magicgui.widgets import FunctionGui
from magicgui.function_gui import FunctionGui
Quickstart
import os
import sys
from magicgui import magicgui
from magicgui.widgets import Container
# Ensure a Qt application is running for the GUI to display
try:
from qtpy.QtWidgets import QApplication
app = QApplication.instance() # Use existing app if any
if app is None:
app = QApplication(sys.argv)
except ImportError:
print("Install `pip install magicgui[qt]` for a complete experience.")
sys.exit(1)
@magicgui(auto_call=True, layout="horizontal", result_widget=True)
def calculate_sum(a: int = 1, b: int = 2) -> int:
"""Calculates the sum of two numbers."""
return a + b
@magicgui(call_button="Display Message")
def show_message(text: str = "Hello, magicgui!"):
"""Displays a message in the console."""
print(f"User message: {text}")
# Create a container to hold multiple magicgui functions
main_container = Container(widgets=[calculate_sum, show_message], labels=False)
main_container.show()
# Start the Qt event loop if running as a standalone script
if app is not None and app.exec_ is not None:
sys.exit(app.exec_())