Professional Documents
Culture Documents
Availability (Key)
macOS GTK Windows iOS Android Web Terminal
○ ○ ○ ○ ○ ○ ○
The app is the main entry point and container for the Toga GUI.
Usage
The app class is used by instantiating with a name, namespace and callback to a startup delegate which
takes 1 argument of the app instance.
import toga
def build(app):
# build UI
pass
if __name__ == '__main__':
app = toga.App('First App', 'org.beeware.helloworld', startup=build)
app.main_loop()
Alternatively, you can subclass App and implement the startup method
import toga
class MyApp(toga.App):
def startup(self):
# build UI
pass
if __name__ == '__main__':
app = MyApp('First App', 'org.beeware.helloworld')
app.main_loop()
Reference
class toga.App(formal_name=None, app_id=None, app_name=None, id=None, icon=None,
author=None, version=None, home_page=None, description=None, startup=None,
windows=(), on_exit=None, factory=None)
Back to top
An App is the top level of any GUI program.
The App is the manager of all the other aspects of execution. An app will usually have a main
window; this window will hold the widgets with which the user will interact.
When you create an App you need to provide a name, an id for uniqueness (by convention, the
identifier is a reversed domain name) and an optional startup function which should run once the
App has initialized. The startup function constructs the initial user interface. If a startup function is
not provided as an argument, you must subclass the App class and define a startup() method.
If the name and app_id are not provided, the application will attempt to find application metadata.
This process will determine the module in which the App class is defined, and look for a .dist-info
file matching that name.
Once the app is created you should invoke the main_loop() method, which will start the event loop
of your App.
PARAMETERS:
• formal_name ( Optional [ str ]) – The formal name of the application. Will be derived from
packaging metadata if not provided.
• app_id ( Optional [ str ]) – The unique application identifier. This will usually be a reversed
domain name, e.g. org.beeware.myapp . Will be derived from packaging metadata if not provided.
• app_name ( Optional [ str ]) – The name of the Python module containing the app. Will be
derived from the module defining the instance of the App class if not provided.
• id ( Optional [ str ]) – The DOM identifier for the app (optional)
• icon ( UnionType [ Icon , str , None ]) – Identifier for the application’s icon.
• author ( Optional [ str ]) – The person or organization to be credited as the author of the
application. Will be derived from application metadata if not provided.
• version ( Optional [ str ]) – The version number of the app. Will be derived from packaging
metadata if not provided.
• home_page ( Optional [ str ]) – A URL for a home page for the app. Used in auto-generated
help menu items. Will be derived from packaging metadata if not provided.
• description ( Optional [ str ]) – A brief (one line) description of the app. Will be derived from
packaging metadata if not provided.
• startup ( Optional [ AppStartupMethod ]) – The callback method before starting the app, typically
to add the components. Must be a callable that expects a single argument of App .
• windows ( Iterable [ Window ]) – An iterable with objects of Window that will be the app’s
secondary windows.
about()
RETURN TYPE:
None
add_background_task(handler)
Schedules a coroutine or a generator to run in the background. Control will be returned to the
event loop during await or yield statements, respectively. Use this to run background tasks
without blocking the GUI. If a regular callable is passed, it will be called as is and will block the
GUI until the call returns.
PARAMETERS:
handler ( BackgroundTask ) – A coroutine, generator or callable.
RETURN TYPE:
None
app = None
property app_id: str
beep()
RETURN TYPE:
None
property current_window
exit()
RETURN TYPE:
None
exit_full_screen()
RETURN TYPE:
None
hide_cursor()
RETURN TYPE:
None
main_loop()
RETURN TYPE:
None
Some platforms do not allow arbitrary file access to any location on disk; even when arbitrary
file system access is allowed, there are “preferred” locations for some types of content.
The Paths object has a set of sub-properties that return pathlib.Path instances of platform-
appropriate paths on the file system.
set_full_screen(*windows)
Full screen is not the same as “maximized”; full screen mode is when all window borders and
other window decorations are no longer visible.
PARAMETERS:
windows ( Window ) – The list of windows to go full screen, in order of allocation to screens.
If the number of windows exceeds the number of available displays, those windows will not
be visible. If no windows are specified, the app will exit full screen mode.
RETURN TYPE:
None
show_cursor()
Show cursor.
RETURN TYPE:
None
startup()
RETURN TYPE:
None
visit_homepage()
RETURN TYPE:
None
Can be used to look up widgets by ID over the entire app (e.g., app.widgets["my_id"] ).
protocol toga.app.AppStartupMethod
typing.Protocol .
Back to top
Classes that implement this protocol must have the following methods / attributes:
__call__(app, **kwargs)
Called during app startup to set the initial main window content.
Note
PARAMETERS:
app ( App ) – The app instance that is starting.
RETURN TYPE:
Widget
RETURNS:
The widget to use as the main window content.
protocol toga.app.BackgroundTask
typing.Protocol .
Classes that implement this protocol must have the following methods / attributes:
__call__(app, **kwargs)
Note
RETURN TYPE:
None
protocol toga.app.OnExitHandler
typing.Protocol .
Classes that implement this protocol must have the following methods / attributes:
__call__(app, **kwargs)
The return value of this callback controls whether the app is allowed to exit. This can be used
to prevent the app exiting with unsaved changes, etc.
Note
PARAMETERS:
app ( App ) – The app instance that is exiting.
RETURN TYPE:
bool
RETURNS:
True if the app is allowed to exit; False if the app is not allowed to exit.