Skip to content

MainWindow

Usage

A toga.MainWindow is a toga.Window that can serve as the main interface to an application. A toga.MainWindow may optionally have a toolbar. The presentation of toga.MainWindow is platform dependent:

  • On desktop platforms that place menus inside windows (e.g., Windows, and most Linux window managers), a toga.MainWindow instance will display a menu bar that contains the defined app commands.
  • On desktop platforms that use an app-level menu bar (e.g., macOS, and some Linux window managers), the window will not have a menu bar; all menu items will be displayed in the app bar.
  • On mobile, web and console platforms, a toga.MainWindow will include a title bar that can contain both menus and toolbar items.

Toolbar items can be added by adding them to toolbar; any command added to the toolbar will be automatically added to the App's commands as well.

import toga

main_window = toga.MainWindow(title='My Application')

self.toga.App.main_window = main_window
main_window.show()

Reference

Bases: Window

Source code in core/src/toga/window.py
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
class MainWindow(Window):
    _WINDOW_CLASS = "MainWindow"

    def __init__(self, *args, **kwargs):
        """Create a new Main Window.

        Accepts the same arguments as [`Window`][toga.Window].
        """
        super().__init__(*args, **kwargs)

        # Create a toolbar that is linked to the app.
        self._toolbar = CommandSet(app=self.app)

        # If the window has been created during startup(), we don't want to
        # install a change listener yet, as the startup process may install
        # additional commands - we want to wait until startup is complete,
        # create the initial state of the menus and toolbars, and then add a
        # change listener. However, if startup *has* completed, we can install a
        # change listener immediately, and trigger the creation of menus and
        # toolbars.
        if self.app.commands.on_change:
            self._toolbar.on_change = self._impl.create_toolbar

            self._impl.create_menus()
            self._impl.create_toolbar()

    @property
    def toolbar(self) -> CommandSet:
        """Toolbar for the window."""
        return self._toolbar

toolbar property

Toolbar for the window.

__init__(*args, **kwargs)

Create a new Main Window.

Accepts the same arguments as Window.

Source code in core/src/toga/window.py
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
def __init__(self, *args, **kwargs):
    """Create a new Main Window.

    Accepts the same arguments as [`Window`][toga.Window].
    """
    super().__init__(*args, **kwargs)

    # Create a toolbar that is linked to the app.
    self._toolbar = CommandSet(app=self.app)

    # If the window has been created during startup(), we don't want to
    # install a change listener yet, as the startup process may install
    # additional commands - we want to wait until startup is complete,
    # create the initial state of the menus and toolbars, and then add a
    # change listener. However, if startup *has* completed, we can install a
    # change listener immediately, and trigger the creation of menus and
    # toolbars.
    if self.app.commands.on_change:
        self._toolbar.on_change = self._impl.create_toolbar

        self._impl.create_menus()
        self._impl.create_toolbar()