MagicGUI

0.10.2 · active · verified Fri Apr 17

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

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to create two simple GUI elements using the `@magicgui` decorator: one for calculation and another for message display. It then combines them into a `Container` and shows the window. It also includes the necessary `QApplication` setup to make the GUI runnable as a standalone script, which is a common requirement.

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_())

view raw JSON →