App¶
The main application object.
Screenshot not available
Screenshot not available
Screenshot not available
Screenshot not available
Screenshot not available
Screenshot not available
Screenshot not available
Screenshot not available
Usage¶
The App class is the top level representation of all application activity. It is a singleton object - any given process can only have a single App. That application may manage multiple windows, but it will generally have at least one window (called the main_window).
The application is started by calling main_loop(). This will invoke the startup() method of the app.
import toga
app = toga.App("Simplest App", "com.example.simplest")
app.main_loop()
You can populate an app's main window by passing a callable as the startup argument to the toga.App constructor. This startup method must return the content that will be added to the main window of the app.
import toga
def create_content(app):
return toga.Box(children=[toga.Label("Hello!")])
app = toga.App("Simple App", "com.example.simple", startup=create_content)
app.main_loop()
This approach to app construction is most useful with simple apps. For most complex apps, you should subclass toga.App, and provide an implementation of startup(). This implementation must assign a value to main_window for the app. The possible values are discussed below; most apps will assign an instance of toga.MainWindow:
import toga
class MyApp(toga.App):
def startup(self):
self.main_window = toga.MainWindow()
self.main_window.content = toga.Box(children=[`toga.Label("Hello!")])
self.main_window.show()
if __name__ == '__main__':
app = MyApp("Realistic App", "org.beeware.realistic")
app.main_loop()
Every app must have a formal name (a human readable name), and an app ID (a machine-readable identifier - usually a reversed domain name). In the examples above, these are provided as constructor arguments. However, you can also provide these details, along with many of the other constructor arguments, as packaging metadata in a format compatible with [importlib.metadata][]. If you deploy your app with Briefcase, this will be done automatically.
A Toga app will install a number of default commands to reflect core application functionality (such as the Quit/Exit menu item, and the About menu item). The IDs for these commands are defined as constants on the Command class. These commands are automatically installed before startup() is invoked. If you wish to customize the menu items exposed by your app, you can add or remove commands in your startup() implementation.
As part of application startup, apps will also ensure that the locale has been set to match the language settings of the operating system.
Assigning a main window¶
An app must assign main_window as part of the startup process. However, the value that is assigned as the main window will affect the behavior of the app.
toga.Window¶
Most apps will assign an instance of toga.Window (or a subclass, such as toga.MainWindow) as the main window. This window will control the life cycle of the app. When the window assigned as the main window is closed, the app will exit.
If you create an App by passing a startup argument to the constructor, a MainWindow will be automatically created and assigned to main_window.
None¶
If your app doesn't have a single "main" window, but instead has multiple windows that are equally important (e.g., a document editor, or a web browser), you can assign a value of None to main_window. The resulting behavior is slightly different on each platform, reflecting platform differences.
On macOS, the app is allowed to continue running without having any open windows. The app can open and close windows as required; the app will keep running until explicitly exited. If you give the app focus when it has no open windows, a file dialog will be displayed prompting you to select a file to open. If the file is already open, the existing representation for the document will be given focus.
On Linux and Windows, when an app closes the last window it is managing, the app will automatically exit. Attempting to close the last window will trigger any app-level on_exit() handling in addition to any window-specific on_close() handling.
Mobile, web and console platforms must define a main window.
toga.App.BACKGROUND¶
Assigning a value of toga.App.BACKGROUND as the main window will allow your app to persist even if it doesn't have any open windows. It will also hide any app-level icon from your taskbar.
Background apps are not supported on mobile, web and console platforms.
Life cycle of an app¶
Regardless of what an application does, every application goes through the same life cycle of starting, running, and shutting down.
Application startup is handled by the startup() method described above. startup() cannot be an asynchronous method, as it runs before the App's event loop is started.
All other events in the life cycle of the app can be managed with event handlers. toga.App defines the following event handlers:
on_running()occurs as soon as the app's event loop has started.on_exit()occurs when the user tries to exit. The handler for this event must return a Boolean value:Trueif the app is allowed to exit;Falseotherwise. This allows an app to abort the exit process (for example, to prevent exit if there are unsaved changes).
Event handlers can be defined by subclassing toga.App and overriding the event handler method, by assigning a value to the event handler when the app instance is constructed, or by assigning the event handler attribute on an existing app instance. When the event handler is set by assigning a value to the event handler, the handler method must accept an app argument. This argument is not required when subclassing, as the app instance can be implied. Regardless of how they are defined, event handlers can be defined as async methods.
Managing documents¶
When you create an App instance, you can declare the type of documents that your app is able to manage by providing a value for document_types. When an app declares that it can manage document types, the app will automatically create file management menu items (such as New, Open and Save), and the app will process command line arguments, creating a toga.Document instance for each argument matching a registered document type.
For details on how to define and register document types, refer to the documentation on document handling.
Notes¶
- On macOS, menus are tied to the app, not the window; and a menu is mandatory. Therefore, a macOS app will always have a menu with the default menu items, regardless of the window being used as the main window.
- Apps executed under Wayland on Linux environment may not show the app's formal name correctly. Wayland considers many aspects of app operation to be the domain of the windowing environment, not the app; as a result, some API requests will be ignored under a Wayland environment. Correctly displaying the app's formal name requires the use of a desktop metadata that Wayland can read. Packaging your app with Briefcase is one way to produce this metadata.
Reference¶
Source code in core/src/toga/app.py
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 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 1003 1004 1005 | |
BACKGROUND = 'background app'
class-attribute
instance-attribute
¶
A constant that can be used as the main window to indicate that an app will run in the background without a main window.
app = None
class-attribute
instance-attribute
¶
The currently running App. Since there can only be one running
Toga app in a process, this is available as a class property via
toga.App.app. If no app has been created yet, this is set to None.
app_id
property
¶
The unique application identifier (read-only). This will usually be a
reversed domain name, e.g. org.beeware.myapp.
app_name
property
¶
The name of the distribution used to load metadata with
[importlib.metadata][] (read-only).
author
property
¶
The person or organization to be credited as the author of the app (read-only).
camera
property
¶
A representation of the device's camera (or cameras).
commands
property
¶
The commands available in the app.
current_window
property
writable
¶
Return the currently active window.
dark_mode
property
¶
Whether the user has dark mode enabled in their environment (read-only).
:returns: A Boolean describing if the app is in dark mode; None if Toga
cannot determine if the app is in dark mode.
description
property
¶
A brief (one line) description of the app (read-only).
documents
property
¶
The list of documents associated with this app.
formal_name
property
¶
The human-readable name of the app (read-only).
home_page
property
¶
The URL of a web page for the app (read-only). Used in auto-generated help menu items.
icon
property
writable
¶
The Icon for the app.
Can be specified as any valid icon content.
in_presentation_mode
property
¶
Is the app currently in presentation mode?
is_bundled
property
¶
Has the app been bundled as a standalone binary, or is it running as a Python script?
is_full_screen
property
¶
DEPRECATED – Use
App.in_presentation_mode.
location
property
¶
A representation of the device's location service.
loop
property
¶
The event loop of the app's main thread (read-only).
main_window
property
writable
¶
The main window for the app.
See the documentation on assigning a main window for values that can be used for this attribute.
paths
property
¶
Paths for platform-appropriate locations on the user's file system.
Some platforms do not allow access to any file system location other than these paths. Even when arbitrary file access is allowed, there are preferred locations for each type of content.
screens
property
¶
Returns a list of available screens.
status_icons
property
¶
The status icons displayed by the app.
version
property
¶
The version number of the app (read-only).
widgets
property
¶
The widgets managed by the app, over all windows.
Can be used to look up widgets by ID over the entire app (e.g.,
app.widgets["my_id"]).
Only returns widgets that are currently part of a layout. A widget that has been created, but not assigned as part of window content will not be returned by widget lookup.
windows
property
¶
The windows managed by the app. Windows are automatically added to the app when they are created, and removed when they are closed.
__init__(formal_name=None, app_id=None, app_name=None, *, icon=None, author=None, version=None, home_page=None, description=None, startup=None, document_types=None, on_running=None, on_exit=None)
¶
Create a new App instance.
Once the app has been created, you should invoke the
App.main_loop() method, which will start the event loop
of your App.
:param formal_name: The human-readable name of the app. If not provided, the
metadata key Formal-Name must be present.
:param app_id: The unique application identifier. This will usually be a
reversed domain name, e.g. org.beeware.myapp. If not provided, the
metadata key App-ID must be present.
:param app_name: The name of the distribution used to load metadata with
[importlib.metadata][]. If app_name is not provided, the last segment of
the app_id argument will be used as a default value (e.g., my-app if an
app ID of com.example.my-app is provided). If app_id is not provided,
and the __main__ module for the app is contained in a package, that
package's name will be used. As a last resort, the name toga will be used.
:param icon: The icon for the app. Defaults to
toga.Icon.APP_ICON.
:param author: The person or organization to be credited as the author of the
app. If not provided, the metadata key Author will be used.
:param version: The version number of the app. If not provided, the metadata
key Version will be used.
:param home_page: The URL of a web page for the app. Used in auto-generated help
menu items. If not provided, the metadata key Home-page will be used.
:param description: A brief (one line) description of the app. If not provided,
the metadata key Summary will be used.
:param startup: A callable to run before starting the app.
:param on_running: The initial on_running handler.
:param on_exit: The initial on_exit handler.
:param document_types: A list of Document classes that this
app can manage.
Source code in core/src/toga/app.py
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 | |
about()
¶
Display the About dialog for the app.
Default implementation shows a platform-appropriate about dialog using app metadata. Override if you want to display a custom About dialog.
Source code in core/src/toga/app.py
787 788 789 790 791 792 793 | |
add_background_task(handler)
¶
DEPRECATED – Use [asyncio.create_task][], or override/assign
App.on_running().
Source code in core/src/toga/app.py
945 946 947 948 949 950 951 952 953 954 955 956 957 | |
beep()
¶
Play the default system notification sound.
Source code in core/src/toga/app.py
795 796 797 | |
dialog(dialog)
async
¶
Display a dialog to the user in the app context.
:param dialog: The dialog to display to the user. :returns: The result of the dialog.
Source code in core/src/toga/app.py
799 800 801 802 803 804 805 806 | |
enter_presentation_mode(windows)
¶
Enter into presentation mode with one or more windows on different screens.
Presentation mode is not the same as "Full Screen" mode; presentation mode is when window borders, other window decorations, app menu and toolbars are no longer visible.
:param windows: A list of windows, or a dictionary mapping screens to windows, to go into presentation, in order of allocation to screens. If the number of windows exceeds the number of available displays, those windows will not be visible. The windows must have a content set on them.
:raises ValueError: If the presentation layout supplied is not a list of windows or a dict mapping windows to screens, or if any window does not have content.
Source code in core/src/toga/app.py
857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 | |
exit()
¶
Unconditionally exit the application.
This does not invoke the on_exit handler; the app will be immediately
and unconditionally closed.
Source code in core/src/toga/app.py
458 459 460 461 462 463 464 | |
exit_full_screen()
¶
DEPRECATED – Use
App.exit_presentation_mode().
Source code in core/src/toga/app.py
963 964 965 966 967 968 969 970 971 972 973 974 975 | |
exit_presentation_mode()
¶
Exit presentation mode.
Source code in core/src/toga/app.py
900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 | |
hide_cursor()
¶
Hide cursor from view.
Source code in core/src/toga/app.py
823 824 825 | |
main_loop()
¶
Start the application.
On desktop platforms, this method will block until the application has exited. On mobile and web platforms, it returns immediately.
Source code in core/src/toga/app.py
472 473 474 475 476 477 478 479 480 481 | |
on_exit()
¶
The event handler that will be invoked when the app is about to exit.
The return value of this method controls whether the app is allowed to exit. This can be used to prevent the app exiting with unsaved changes, etc.
If necessary, the overridden method can be defined as an async coroutine.
:returns: True if the app is allowed to exit; False if the app is not
allowed to exit.
Source code in core/src/toga/app.py
920 921 922 923 924 925 926 927 928 929 930 931 932 | |
on_running()
¶
The event handler that will be invoked when the app's event loop starts running.
If necessary, the overridden method can be defined as an async coroutine.
Source code in core/src/toga/app.py
934 935 936 937 938 939 | |
request_exit()
¶
Request an exit from the application.
This method will call the App.on_exit() handler to confirm
if the app should be allowed to exit; if that handler confirms the action,
the app will exit.
Source code in core/src/toga/app.py
442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 | |
set_full_screen(*windows)
¶
DEPRECATED – Use
App.enter_presentation_mode() and
App.exit_presentation_mode().
Source code in core/src/toga/app.py
991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 | |
show_cursor()
¶
Make the cursor visible.
Source code in core/src/toga/app.py
827 828 829 | |
startup()
¶
Create and show the main window for the application.
Subclasses can override this method to define customized startup behavior;
however, any override must ensure the
main_window has
been assigned before it returns.
Source code in core/src/toga/app.py
680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 | |
visit_homepage()
¶
Open the application's home_page in the default
browser.
This method is invoked as a handler by the "Visit homepage" default menu item.
If the home_page is None, this is a no-op, and the
default menu item will be disabled.
Source code in core/src/toga/app.py
808 809 810 811 812 813 814 815 816 817 | |
Bases: Protocol
Source code in core/src/toga/app.py
37 38 39 40 41 42 43 44 45 46 47 | |
__call__(app, **kwargs)
¶
The startup method of the app.
Called during app startup to set the initial main window content.
:param app: The app instance that is starting. :param kwargs: Ensures compatibility with additional arguments introduced in future versions. :returns: The widget to use as the main window content.
Source code in core/src/toga/app.py
38 39 40 41 42 43 44 45 46 47 | |
Bases: Protocol
Source code in core/src/toga/app.py
75 76 77 78 79 80 81 82 | |
__call__(app, **kwargs)
¶
Code that should be executed as a background task.
:param app: The app that is handling the background task. :param kwargs: Ensures compatibility with additional arguments introduced in future versions.
Source code in core/src/toga/app.py
76 77 78 79 80 81 82 | |
Bases: Protocol
Source code in core/src/toga/app.py
50 51 52 53 54 55 56 57 | |
__call__(app, **kwargs)
¶
A handler to invoke when the app event loop is running.
:param app: The app instance that is running. :param kwargs: Ensures compatibility with additional arguments introduced in future versions.
Source code in core/src/toga/app.py
51 52 53 54 55 56 57 | |
Bases: Protocol
Source code in core/src/toga/app.py
60 61 62 63 64 65 66 67 68 69 70 71 72 | |
__call__(app, **kwargs)
¶
A handler to invoke when the app is about to exit.
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.
:param app: The app instance that is exiting.
:param kwargs: Ensures compatibility with additional arguments introduced in
future versions.
:returns: True if the app is allowed to exit; False if the app is not
allowed to exit.
Source code in core/src/toga/app.py
61 62 63 64 65 66 67 68 69 70 71 72 | |