Skip to content
Toga
0.5.3.dev143+gabcc6cdb6

Dialogs

A short-lived window asking the user for input.

Availability (Key)

macOS GTK Windows iOS Android Web Terminal

Usage

A dialog is a short-lived window that requires the user to provide acknowledgement, respond to a question, or provide information to the application.

A dialog can be presented relative to a specific window (a window-modal dialog), and relative to the entire app (an app-modal dialog). When presented as a window-modal dialog, the user will not be able to interact with anything in the window until the dialog is dismissed. When presented as an app-modal dialog, the user will be restricted from interacting with the rest of the app (there are some platform-specific variations in this behavior; see the platform notes for details).

When a dialog is presented, the app's event loop will continue to run, and the content of the app windows will redraw if requested by the event loop. For this reason, dialogs are implemented asynchronously - that is, they must be await-ed in the context of an async method. The value returned by the await is the response of the dialog; the return type will vary depending on the type of dialog being displayed.

To display a dialog, create an instance of the dialog type you want to display, and await the dialog() method in the context that you want to display the dialog (either toga.Window.dialog() or toga.App.dialog()). In the following example, my_handler is an asynchronous method defined on an App subclass that would be installed as an event handler (e.g., as an on_press() handler on a Button). The dialog is displayed as window-modal against the app's main window; the dialog returns True or False depending on the user's response:

async def my_handler(self, widget, **kwargs):
    ask_a_question = toga.QuestionDialog("Hello!", "Is this OK!")

    if await self.main_window.dialog(ask_a_question):
        print("The user said yes!")
    else:
        print("The user said no.")

When this handler is triggered, the dialog will be displayed, but a print statement will not be executed until the user's response has been received. To convert this example into an app-modal dialog, you would use self.dialog(ask_a_question), instead of self.main_window.dialog(ask_a_question).

If you need to display a dialog in a synchronous context (i.e., in a normal, non-async event handler), you must create a asyncio.Task for the dialog, and install a callback that will be invoked when the dialog is dismissed:

def my_sync_handler(self, widget, **kwargs):
    ask_a_question = toga.QuestionDialog("Hello!", "Is this OK!")

    task = asyncio.create_task(self.main_window.dialog(ask_a_question))
    task.add_done_callback(self.dialog_dismissed)
    print("Dialog has been created")

def dialog_dismissed(self, task):
    if task.result():
        print("The user said yes!")
    else:
        print("The user said no.")

In this example, when my_sync_handler is triggered, a dialog will be created, the display of that dialog will be scheduled as an asynchronous task, and a message will be logged saying the dialog has been created. When the user responds, the dialog_dismissed callback will be invoked, with the dialog task provided as an argument. The result of the task can then be interrogated to handle the response.

Notes

  • On macOS, app-modal dialogs will not prevent the user from interacting with the rest of the app.

Reference

toga.InfoDialog

InfoDialog(title, message)

Bases: Dialog[None]

Ask the user to acknowledge some information.

Presents as a dialog with a single "OK" button to close the dialog.

Returns a response of None.

PARAMETER DESCRIPTION
title

The title of the dialog window.

TYPE: str

message

The message to display.

TYPE: str

toga.QuestionDialog

QuestionDialog(title, message)

Bases: Dialog[bool]

Ask the user a yes/no question.

Presents as a dialog with "Yes" and "No" buttons.

Returns a response of True when the "Yes" button is pressed, False when the "No" button is pressed.

PARAMETER DESCRIPTION
title

The title of the dialog window.

TYPE: str

message

The question to be answered.

TYPE: str

toga.ConfirmDialog

ConfirmDialog(title, message)

Bases: Dialog[bool]

Ask the user to confirm if they wish to proceed with an action.

Presents as a dialog with "Cancel" and "OK" buttons (or whatever labels are appropriate on the current platform).

Returns a response of True when the "OK" button is pressed, False when the "Cancel" button is pressed.

PARAMETER DESCRIPTION
title

The title of the dialog window.

TYPE: str

message

A message describing the action to be confirmed.

TYPE: str

toga.ErrorDialog

ErrorDialog(title, message)

Bases: Dialog[None]

Ask the user to acknowledge an error state.

Presents as an error dialog with an "OK" button to close the dialog.

Returns a response of None.

PARAMETER DESCRIPTION
title

The title of the dialog window.

TYPE: str

message

The error message to display.

TYPE: str

toga.StackTraceDialog

StackTraceDialog(
    title: str, message: str, content: str, retry: Literal[False] = ...
)
StackTraceDialog(title: str, message: str, content: str, retry: Literal[True])
StackTraceDialog(title, message, content, retry=False)

Bases: Dialog[DialogResultT]

Open a dialog to display a large block of text, such as a stack trace.

If retry is true, returns a response of True when the user selects "Retry", and False when they select "Quit".

If retry is False, returns a response of None.

PARAMETER DESCRIPTION
title

The title of the dialog window.

TYPE: str

message

Contextual information about the source of the stack trace.

TYPE: str

content

The stack trace, pre-formatted as a multi-line string.

TYPE: str

retry

If true, the user will be given options to "Retry" or "Quit"; if false, a single option to acknowledge the error will be displayed.

TYPE: bool DEFAULT: False

toga.SaveFileDialog

SaveFileDialog(title, suggested_filename, file_types=None)

Bases: Dialog[Path | None]

Prompt the user for a location to save a file.

This dialog is not currently supported on Android or iOS.

Returns a path object for the selected file location, or None if the user cancelled the save operation.

If the filename already exists, the user will be prompted to confirm they want to overwrite the existing file.

PARAMETER DESCRIPTION
title

The title of the dialog window

TYPE: str

suggested_filename

The initial suggested filename

TYPE: Path | str

file_types

The allowed filename extensions, without leading dots. If not provided, any extension will be allowed.

TYPE: list[str] | None DEFAULT: None

toga.OpenFileDialog

OpenFileDialog(
    title: str,
    initial_directory: Path | str | None = None,
    file_types: list[str] | None = None,
    multiple_select: Literal[False] = ...,
)
OpenFileDialog(
    title: str,
    initial_directory: Path | str | None = None,
    file_types: list[str] | None = None,
    *,
    multiple_select: Literal[True],
)
OpenFileDialog(
    title, initial_directory=None, file_types=None, multiple_select=False
)

Bases: Dialog[DialogResultT]

Prompt the user to select a file (or files) to open.

This dialog is not currently supported on Android or iOS.

If multiple_select is True, returns a list of Path objects.

If multiple_select is False, returns a single Path.

Returns None if the open operation is cancelled by the user.

PARAMETER DESCRIPTION
title

The title of the dialog window

TYPE: str

initial_directory

The initial folder in which to open the dialog. If None, use the default location provided by the operating system (which will often be the last used location)

TYPE: Path | str | None DEFAULT: None

file_types

The allowed filename extensions, without leading dots. If not provided, all files will be shown.

TYPE: list[str] | None DEFAULT: None

multiple_select

If True, the user will be able to select multiple files; if False, the selection will be restricted to a single file.

TYPE: bool DEFAULT: False

toga.SelectFolderDialog

SelectFolderDialog(
    title: str,
    initial_directory: Path | str | None = None,
    multiple_select: Literal[False] = ...,
)
SelectFolderDialog(
    title: str,
    initial_directory: Path | str | None = None,
    *,
    multiple_select: Literal[True],
)
SelectFolderDialog(title, initial_directory=None, multiple_select=False)

Bases: Dialog[DialogResultT]

Prompt the user to select a directory (or directories).

This dialog is not currently supported on Android or iOS.

If multiple_select is True, returns a list of Path objects.

If multiple_select is False, returns a single Path.

Returns None if the select operation is cancelled by the user.

PARAMETER DESCRIPTION
title

The title of the dialog window

TYPE: str

initial_directory

The initial folder in which to open the dialog. If None, use the default location provided by the operating system (which will often be "last used location")

TYPE: Path | str | None DEFAULT: None

multiple_select

If True, the user will be able to select multiple directories; if False, the selection will be restricted to a single directory. This option is not supported on WinForms.

TYPE: bool DEFAULT: False