Escribe para buscar…

Basic

Un conjunto de herramientas GUI nativo de Python y del sistema operativo.

Esta página todavía no se ha traducido — se muestra en su idioma original:English

Introducció

Toga iss available on macOS, Windows, Linux (GTK), Android, iOS, for single-page web apps, and console apps.

Toga uses native system widgets, not themes. When you see a Toga app running, it doesn’t just look like a native app - it is a native app. Applying an operating system-inspired theme over the top of a generic widget set is an easy way for a developer to achieve a cross-platform goal, but it leaves the end user with the mess.

Native widgets are always faster than a themed generic widget. After all, you’re using native system capability that has been tuned and optimized, not a drawing engine that’s been layered on top of a generic widget. They also inherit all of the native platform’s accessibility affordances, such as support for screen readers and adaptive font sizes.

Toga has been designed from the ground up to be a Python native widget toolkit. This means the API is able to exploit language level features like generators and context managers

Your first Toga app

Crea un projecte toga-basic amb uv:

ps
uv init toga-basic
cd toga-basic
uv add toga

Write the app

Modifica el fitxer main.py:

python
main.py
import toga


def button_handler(widget):
    print("hello")


def build(app):
    box = toga.Box()

    button = toga.Button("Hello world", on_press=button_handler)
    button.style.margin = 50
    button.style.flex = 1
    box.add(button)

    return box


def main():
    app = toga.App("Basic App", "dev.xtec.toga.basic", startup=build)
    app.main_loop()

if __name__ == "__main__":
    main()

Let’s walk through this one line at a time.

The code starts with imports. First, we import toga:

python
import toga

Then we set up a handler, which is a wrapper around behavior that we want to activate when the button is pressed. A handler is just a function. The function takes the widget that was activated as the first argument; depending on the type of event that is being handled, other arguments may also be provided. In the case of a simple button press, however, there are no extra arguments:

python
def button_handler(widget):
    print("hello")

Estás leyendo una vista previa.

Inicia sesión para leer el artículo completo. Cualquier cuenta abre 4 artículos gratuitos al mes; el alumnado y el profesorado leen las páginas de su curso sin límite.

Iniciar sesión