Window¶
Usage¶
A window is the top-level container that the operating system uses to display widgets. On desktop platforms, an instance of Window will have a title bar, but will not have a menu or toolbar. On mobile, web and console platforms, Window is a bare container with no other decoration. Subclasses of Window (such as MainWindow) add other decorations.
When first created, a window is not visible. To display it, call the show() method. The title of the window will default to the formal name of the app.
The window has content, which will usually be a container widget of some kind. The content of the window can be changed by re-assigning its content attribute to a different widget.
import toga
window = toga.Window()
window.content = toga.Box(children=[...])
window.show()
# Change the window's content to something new
window.content = toga.Box(children=[...])
If the user attempts to close the window, Toga will call the on_close handler. This handler must return a bool confirming whether the close is permitted. This can be used to implement protections against closing a window with unsaved changes.
Once a window has been closed (either by user action, or programmatically with close()), it cannot be reused. The behavior of any method on a Window instance after it has been closed is undefined.
Notes¶
- The operating system may provide controls that allow the user to resize, reposition, minimize, maximize or close the window. However, the availability of these controls is entirely operating system dependent.
- While Toga provides methods for specifying the size and position of windows, these are ultimately at the discretion of the OS (or window manager). For example, on macOS, depending on a user's OS-level settings, new windows may open as tabs on the main window; on Linux, some window managers (e.g., tiling window managers) may not honor an app's size and position requests. You should avoid making UI design decisions that are dependent on specific size and placement of windows.
- A mobile application can only have a single window (the
main_window), and that window cannot be moved, resized, or hidden. Toga will raise an exception if you attempt to create a secondary window on a mobile platform. If you try to modify the size, position, or visibility of the main window, the request will be ignored. - On mobile platforms, a window's state cannot be
WindowState.MINIMIZEDorWindowState.MAXIMIZED. Any request to move to these states will be ignored. - On Linux, when using Wayland, a request to put a window into a
WindowState.MINIMIZEDstate, or to restore from theWindowState.MINIMIZEDstate, will be ignored, and any associated events likeon_hide()andon_show(), will not be triggered. This is due to limitations in window management features that Wayland allows apps to use.
Reference¶
Source code in core/src/toga/window.py
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 | |
app
property
writable
¶
The App that this window belongs to (read-only).
New windows are automatically associated with the currently active app.
closable
property
¶
Can the window be closed by the user?
closed
property
¶
Whether the window was closed.
content
property
writable
¶
Content of the window. On setting, the content is added to the same app as the window.
full_screen
property
writable
¶
DEPRECATED – Use Window.state.
id
property
¶
A unique identifier for the window (read-only).
minimizable
property
¶
Can the window be minimized by the user?
on_close
property
writable
¶
The handler to invoke if the user attempts to close the window.
on_gain_focus
property
writable
¶
The handler to invoke if the window gains input focus.
on_hide
property
writable
¶
The handler to invoke if the window is hidden from a visible state.
on_lose_focus
property
writable
¶
The handler to invoke if the window loses input focus.
on_resize
property
writable
¶
The handler to invoke when the window resizes.
on_show
property
writable
¶
The handler to invoke if the window is shown from a hidden state.
position
property
writable
¶
Absolute position of the window, in CSS pixels.
The origin is the top left corner of the primary screen.
:raises RuntimeError: If position change is requested while in
WindowState.FULLSCREEN or
WindowState.PRESENTATION.
resizable
property
¶
Can the window be resized by the user?
screen
property
writable
¶
Instance of the toga.Screen on which this window
is present.
screen_position
property
writable
¶
Position of the window with respect to current screen, in CSS pixels.
:raises RuntimeError: If position change is requested while in
WindowState.FULLSCREEN or
WindowState.PRESENTATION.
size
property
writable
¶
Size of the window, in CSS pixels.
:raises RuntimeError: If resize is requested while in
WindowState.FULLSCREEN or
WindowState.PRESENTATION.
state
property
writable
¶
The current state of the window.
When the window is in transition, then this will return the state it is transitioning towards, instead of the actual instantaneous state.
:raises RuntimeError: If state change is requested while the window is hidden.
:raises ValueError: If any state other than
WindowState.MINIMIZED
or WindowState.NORMAL
is requested on a non-resizable window.
title
property
writable
¶
Title of the window. If no title is provided, the title will default to "Toga".
visible
property
writable
¶
Is the window visible?
widgets
property
¶
The widgets contained in the window.
Can be used to look up widgets by ID (e.g., window.widgets["my_id"]).
__init__(id=None, title=None, position=None, size=(640, 480), resizable=True, closable=True, minimizable=True, on_close=None, on_gain_focus=None, on_lose_focus=None, on_show=None, on_hide=None, on_resize=None, content=None)
¶
Create a new Window.
:param id: A unique identifier for the window. If not provided, one will be
automatically generated.
:param title: Title for the window. Defaults to the formal name of the app.
:param position: Position of the window, as a toga.Position or tuple of
(x, y) coordinates, in CSS pixels.
:param size: Size of the window, as a toga.Size or tuple of (width,
height), in CSS pixels.
:param resizable: Can the window be resized by the user?
:param closable: Can the window be closed by the user?
:param minimizable: Can the window be minimized by the user?
:param on_close: The initial on_close handler.
:param content: The initial content for the window.
Source code in core/src/toga/window.py
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 | |
as_image(format=Image)
¶
Render the current contents of the window as an image.
:param format: Format to provide. Defaults to Image; also
supports [PIL.Image.Image][] if Pillow is installed, as well as any image
types defined by installed
image format plugins.
:returns: An image containing the window content, in the format requested.
Source code in core/src/toga/window.py
626 627 628 629 630 631 632 633 634 635 | |
close()
¶
Close the window.
This does not invoke the on_close handler. If the window being closed
is the app's main window, it will trigger on_exit handling; otherwise, the
window will be immediately and unconditionally closed.
Once a window has been closed, it cannot be reused. The behavior of any method
or property on a Window instance after it has been closed is
undefined, except for closed which can be used to check
if the window was closed.
:returns: True if the window was actually closed; False if closing the window
triggered on_exit handling.
Source code in core/src/toga/window.py
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 | |
confirm_dialog(title, message, on_result=None)
¶
DEPRECATED - await dialog() with a
ConfirmDialog
Source code in core/src/toga/window.py
762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 | |
dialog(dialog)
async
¶
Display a dialog to the user, modal to this window.
:param dialog: The toga.Window.dialog to display to the user. :returns: The result of the dialog.
Source code in core/src/toga/window.py
637 638 639 640 641 642 643 644 645 646 647 | |
error_dialog(title, message, on_result=None)
¶
DEPRECATED - await dialog() with an
ErrorDialog
Source code in core/src/toga/window.py
787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 | |
hide()
¶
Hide the window. If the window is already hidden, this method has no effect.
:raises ValueError: If the window is currently in a minimized, full screen or presentation state.
Source code in core/src/toga/window.py
552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 | |
info_dialog(title, message, on_result=None)
¶
DEPRECATED - await dialog() with an
InfoDialog
Source code in core/src/toga/window.py
715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 | |
open_file_dialog(title, initial_directory=None, file_types=None, multiple_select=False, on_result=None)
¶
DEPRECATED - await dialog() with an
OpenFileDialog
Source code in core/src/toga/window.py
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 | |
question_dialog(title, message, on_result=None)
¶
DEPRECATED - await dialog() with a
QuestionDialog
Source code in core/src/toga/window.py
737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 | |
save_file_dialog(title, suggested_filename, file_types=None, on_result=None)
¶
DEPRECATED - await dialog() with a
SaveFileDialog
Source code in core/src/toga/window.py
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 | |
select_folder_dialog(title, initial_directory=None, multiple_select=False, on_result=None)
¶
DEPRECATED - await dialog() with a
SelectFolderDialog
Source code in core/src/toga/window.py
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 | |
show()
¶
Show the window. If the window is already visible, this method has no effect.
:raises ValueError: If the window is currently in a minimized, full screen or presentation state.
Source code in core/src/toga/window.py
410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 | |
stack_trace_dialog(title, message, content, retry=False, on_result=None)
¶
DEPRECATED - await dialog() with a
StackTraceDialog
Source code in core/src/toga/window.py
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 | |
Bases: MutableSet[Window]
Source code in core/src/toga/window.py
1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 | |
__init__(app)
¶
A collection of windows managed by an app.
A window is automatically added to the app when it is created, and removed when
it is closed. Adding a window to an App's window set automatically sets the
Window.app property of the Window.
Source code in core/src/toga/window.py
1006 1007 1008 1009 1010 1011 1012 1013 1014 | |
Bases: AsyncResult
Source code in core/src/toga/window.py
193 194 195 196 197 198 199 | |
Bases: Protocol
Source code in core/src/toga/window.py
93 94 95 96 97 98 99 100 101 102 103 104 105 106 | |
__call__(window, **kwargs)
¶
A handler to invoke when a window is about to close.
The return value of this callback controls whether the window is allowed to close. This can be used to prevent a window closing with unsaved changes, etc.
:param window: The window instance that is closing.
:param kwargs: Ensures compatibility with arguments added in future versions.
:returns: True if the window is allowed to close; False if the window
is not allowed to close.
Source code in core/src/toga/window.py
94 95 96 97 98 99 100 101 102 103 104 105 106 | |
Bases: Protocol[_DialogResultT]
Source code in core/src/toga/window.py
183 184 185 186 187 188 189 190 | |
__call__(window, result, **kwargs)
¶
A handler to invoke when a dialog is closed.
:param window: The window that opened the dialog. :param result: The result returned by the dialog. :param kwargs: Ensures compatibility with arguments added in future versions.
Source code in core/src/toga/window.py
184 185 186 187 188 189 190 | |