Toga Python GUI Toolkit

repository·main·Indexed 26 days ago

https://github.com/beeware/toga

Toga is a Python GUI toolkit that provides native user interfaces by using OS-native widgets. It consists of a core library (toga-core) and multiple platform-specific backends, including toga-android, toga-cocoa (macOS), toga-gtk (Linux/FreeBSD), toga-winforms (Windows), toga-iOS, toga-web, toga-qt, and toga-textual. It also includes toga-positron for web-content GUIs and the Travertino library for color management.

Tokens
46.4K
Snippets
118
Records
278
Agent score
90%

What's inside Toga

  1. Understand Toga's Three-Layer Architecture

    main

    Toga widgets are composed of three distinct internal layers. Understanding these layers helps in understanding how Toga abstracts platform-specific complexities into a single, consistent API.

    1. Interface Layer: The public API defined in toga-core. This is what you use to build your application. It handles input validation, persistent value storage, and style/layout attributes.
    2. Implementation Layer: The platform-specific representation (e.g., toga-cocoa for macOS, toga-gtk for Linux). It provides a private utility API to the Interface layer. Every interface widget has exactly one implementation widget, though not all interface widgets are available on all platforms.
    3. Native Layer: The actual system widgets provided by the underlying toolkit (e.g., Gtk.Button). An implementation might use a single native widget or multiple native widgets combined into a single "primary" native widget to satisfy the Toga interface.
  2. Understand DrawingAction and Canvas State

    main

    In Toga, every drawing operation performed on a Canvas is represented by a DrawingAction object. Each drawing method (like line_to()) has a corresponding DrawingAction subclass (like LineTo).

    The drawing context is managed via state objects, which are subclasses of BaseState. The simplest is State. A canvas starts with a single initial state accessible via canvas.root_state. Each state maintains a drawing_actions list containing the operations performed within that state.

    Caution: While you can interact with these actions directly to retroactively alter or reorder them, doing so bypasses the canvas's internal management. Avoid mixing direct DrawingAction manipulation with the canvas's built-in context managers to prevent unintuitive results.

    import toga
    
    canvas = toga.Canvas()
    
    print(canvas.root_state)
    # State()
    print(canvas.root_state.drawing_actions)
    # []
    
    canvas.rect(0, 0, 10, 10)
    canvas.stroke()
    
    print(canvas.root_state.drawing_actions)
    # [Rect(x=0, y=0, width=10, height=10),
    #  Stroke(stroke_style=None, line_width=None, line_dash=None)]
  3. Understand Toga's widget roadmap and limitations

    main

    Toga is an evolving project. When designing your application, be aware that some common GUI widgets are currently partially implemented or planned for future release.

    Mobile Platform Workarounds

    On mobile platforms (iOS/Android), certain complex widgets do not have native equivalents. You may need to use alternative patterns:

    • Tables: Instead of a native Table, use DetailedList. You can treat the title and subtitle as your primary columns, and use row selection to navigate to a sub-page containing the full row details.
    • Trees: Similar to Tables, mobile platforms lack a native Tree widget. Consider using a list-based approach or a composite widget that combines table scrolling with tree functionality.

    Planned Widgets

    The following widgets are identified in the roadmap for future implementation:

    • Inputs: RadioButton, ComboBox, ColorInput, SearchInput, and DateTimeInput.
    • Views: VideoView and PDFView.
    • Containers: FormContainer (for key/value layouts) and NavigationContainer (for hierarchical navigation with 'back' button support).
  4. Use toga-android as a Toga backend

    main
    The toga-android package provides the Android backend implementation for the Toga widget toolkit. It cannot be used in isolation; it must be used in conjunction with the toga-core library to build Android applications using Toga.
  5. Use toga-winforms as a Toga backend

    main

    The toga-winforms package provides a Microsoft .NET backend for the Toga widget toolkit using the WinForms API.

    Note: This package is not a standalone library. To use it, you must install it alongside the core Toga library (toga-core).

    Before using this backend, ensure your system meets the Windows platform prerequisites defined in the Toga documentation.

  6. Use the Textual backend for Toga

    main

    The toga-textual package provides a Textual-based backend for the Toga widget toolkit. Note that this package cannot be used in isolation; it must be used in conjunction with the core Toga library (toga-core).

    Before using this backend, ensure your environment meets the requirements for the Terminal platform as specified in the Toga documentation.

  7. Use toga-iOS as a Toga backend

    main
    toga-iOS is an iOS-specific backend for the Toga widget toolkit. It is not intended to be used as a standalone package; it must be used in conjunction with the core toga-core library to enable Toga applications to run on iOS platforms.
  8. Use the Qt backend for Toga

    main

    The toga-qt package provides a Qt backend for the Toga widget toolkit. Note that toga-qt cannot be used in isolation; it must be used in conjunction with the core Toga library (toga-core).

    Before using this backend, ensure you meet the platform-specific requirements for Qt, which can be found in the Toga platform documentation.

  9. Use toga-cocoa as a Toga backend

    main
    The toga-cocoa package provides the Cocoa backend for the Toga widget toolkit. It is not intended to be used in isolation; it must be used in conjunction with the toga-core library to build macOS applications using Toga.
  10. Use toga-web as a Toga backend for web platforms

    main

    The toga-web package provides a backend for the Toga widget toolkit specifically for web platforms. It utilizes WebAwesome to provide web components.

    Note: This package is not a standalone library; it must be used in conjunction with toga-core to function.