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=(100, 100), size=(640, 480), resizable=True, minimizable=True, 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 (tuple[int, int]) – Position of the window, as a tuple of (x, y) coordinates, in CSS pixels.

  • size (tuple[int, int]) – Size of the window, as a 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?

  • resizeableDEPRECATED - Use resizable.

  • closeableDEPRECATED - 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.