Tkinter messagebox – Show Error

Tkinter messagebox – Show error

In Tkinter, the messagebox.showerror() method is used display or show an error message in a window or message box.

Tkinter messagebox.showerror() in Windows OS
Tkinter messagebox.showerror() in Windows OS

In this tutorial, you will learn how to display an error message to user using messagebox.showerror() method in Tkinter, how an error message box is displayed in different operating systems, and an example program.

Syntax of messagebox.showerror()

The syntax of showerror() method is

messagebox.showerror(title, message)

where

ParameterDescription
titleThis string is displayed as title of the messagebox. [How a title is displayed in message box depends on the Operating System. ]
messageThis string is displayed as an error message inside the message box.

How messagebox.showerror() is displayed in different Operating Systems

The following screenshots show how an error message box appears in different operating systems.

# Show an error message box
messagebox.showerror("Error", "This is an error message.")

In Windows

Given title is displayed as the title of message box. Inside the message box, an error icon (white x with red background in circular shape) is followed by the error message.

Tkinter messagebox.showerror() in Windows OS
Tkinter messagebox.showerror() in Windows OS

In MacOS

An icon (default or specific) is displayed, followed by the error message, and an OK button.

Tkinter messagebox – Show error - Example screenshot - MacOS
Tkinter messagebox.showerror() in MacOS

When you click on the OK button under the message, the message box disappears.

Examples

1. Show an error message to user

For example, consider that you have created a GUI application using Python Tkinter, and you would like to display an error message to the user about the action the user has taken.

You can display the error message in a messagebox, as shown in the following code.

Python Program

import tkinter as tk
from tkinter import messagebox

# Create the main window
window = tk.Tk()

# Show an error message box
messagebox.showerror("Error", "This is an error message.")

# Run the application
window.mainloop()

Once the message box is displayed, you can click on the OK button. Then the message box disappears.

Output

In Windows 10

Tkinter messagebox – Show Error

In MacOS

Tkinter messagebox – Show error - Example screenshot - MacOS
Tkinter messagebox.showerror() Example – MacOS

Summary

In this Python Tkinter tutorial, we learned how to display an error message in a message box in Tkinter, with examples.

Related Tutorials

Code copied to clipboard successfully 👍