Tkinter messagebox - Show Warning


Tkinter messagebox – Show warning

In Tkinter, the messagebox.showwarning() method is used display or show a warning message in a window or message box.

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

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

Syntax of messagebox.showwarning()

The syntax of showwarning() method is

messagebox.showwarning(title, message)

where

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

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

The following screenshots show how a warning message box appears in different operating systems.

messagebox.showwarning("Warning", "This is a sample warning message. (PythonExamples.org)")

In Windows

Given title is displayed as the title of message box. Inside the message box, an error icon (! mark with yellow background in triangular shape) is followed by the error message.

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

In MacOS

A warning icon is displayed, followed by the warning message, and an OK button.

Tkinter messagebox.showwarning() in MacOS

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

Examples

1. Show a warning message to user

For example, consider that you have created a GUI application using Python Tkinter, and you would like to display a warning message to the user about the action the user is going to take.

You can display the warning message in a message box, as shown in the following code.

Python Program

import tkinter as tk
from tkinter import messagebox

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

# Show a warning message box
messagebox.showwarning("Warning", "This is a sample warning message. (PythonExamples.org)")

# 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

In MacOS

Tkinter messagebox – Show warning - Example screenshot - MacOS
Tkinter messagebox.showwarning() Example - MacOS

Summary

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