MainWindow

The main window of the application.

../../_images/mainwindow-cocoa.png

Usage

The main window of an application is a normal toga.Window, with one exception - when the main window is closed, the application exits.

import toga

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

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

As the main window is closely bound to the App, a main window cannot define an on_close handler. Instead, if you want to prevent the main window from exiting, you should use an on_exit handler on the toga.App that the main window is associated with.

Reference

class toga.MainWindow(id=None, title=None, position=Position(100, 100), size=Size(640, 480), resizable=True, minimizable=True, content=None, resizeable=None, closeable=None)

Bases: Window

Create a new main window.

Parameters:
  • id (str | None) – A unique identifier for the window. If not provided, one will be automatically generated.

  • title (str | None) – Title for the window. Defaults to the formal name of the app.

  • position (PositionT) – Position of the window, as a toga.Position or tuple of (x, y) coordinates, in CSS pixels.

  • size (SizeT) – Size of the window, as a toga.Size or tuple of (width, height), in CSS pixels.

  • resizable (bool) – Can the window be resized by the user?

  • minimizable (bool) – Can the window be minimized by the user?

  • content (Widget | None) – The initial content for the window.

  • resizeable (None) – DEPRECATED - Use resizable.

  • closeable (None) – DEPRECATED - Use closable.

property on_close: None

The handler to invoke before the window is closed in response to a user action.

Always returns None. Main windows should use toga.App.on_exit(), rather than on_close.

Raises:

ValueError – if an attempt is made to set the on_close handler.