Message Box in Tk (tkinter)

A message box is a window with a title, a message, an icon, and one or more buttons. It is used to inform the user about something or encourage him to take an action.

The functions for creating message boxes in a Tcl/Tk application are defined within the tkinter.messagebox standard module (tkMessageBox in Python 2), and are the following:

  • showinfo()

  • showwarning()

  • showerror()

  • askquestion()

  • askyesno()

  • askokcancel()

  • askyesnocancel()

  • askretrycancel()

They all take the title and message arguments, which indicate the title of the window and its content. For example:

from tkinter import messagebox
messagebox.showinfo(message="Hello, world!", title="My Python Application")

This code generates the following window:

/images/message-box-in-tk-tkinter/message-box-showinfo.png

While the message box is open, other widgets inside the main window do not respond to any events (for example, clicking on a button), i. e. the user needs to answer the message to continue using the application. This behavior is known in the GUI jargon as modal.

The icon displayed by each function is provided by the operating system, although it is always related to the message that the dialog box intends to communicate to the user. For example, the showinfo() icon is usually blue or light blue and indicates information; that of showwarning() is yellow or orange denoting warning; the one for showerror() is red indicating error.

/images/message-box-in-tk-tkinter/message-boxes-error-warning.png

The same goes for the text of the buttons. In these three functions, in most operating systems the text is "Ok" or its proper translation. The return value is always the string "ok", regardless of whether the button was pressed or the window was closed.

The interrogation functions return True or False, depending on whether "Accept" or "Cancel", or "Yes" or "No" has been pressed. Examples:

from tkinter import messagebox
# These return True or False.
messagebox.askyesno(
    message="Do you want to continue?",
    title="My Python Application"
)
messagebox.askokcancel(
    message="Do you want to continue?",
    title="My Python Application"
)
messagebox.askretrycancel(
    message="Do you want to try again?",
    title="My Python Application"
)
/images/message-box-in-tk-tkinter/message-boxes-questions.png

You would usually do something like this after prompting one of these questions:

retry = messagebox.askretrycancel(
    message="Do you want to try again?",
    title="My Python Application"
)
if retry:
    do_retry()

In these three functions, closing the window is similar to canceling, so the result is in both cases False.

askquestion() is similar to askyesno(), but the result is "yes" or "no". These are the values ​​returned directly from the Tk library and there is no reason to use them instead of the first function.

# Returns "yes" or "no" (strings).
messagebox.askquestion(
    message="Do you want to continue?",
    title="My Python Application"
)

Finally, askyesnocancel() displays the "Yes", "No", and "Cancel" buttons, and possible return values ​​are True, False, and None, respectively.

# Return True, False, or None.
messagebox.askyesnocancel(
    message="Do you want to continue?",
    title="My Python Application"
)

In this case, closing the window returns None, since it is the value associated with the cancel button.