Python Tkinter messagebox – Displaying Prompts

From the Python Tkinter Library, messagebox is used to display a variety of prompts to the user. Messagebox is useful due to it’s numerous types of prompts and the user friendly way it displays them to the user.

Keep in mind, that Python messagebox goes hand in hand with the GUI library tktinter. A tkinter gui window is required for prompt to spawn. Hence, even if you don’t declare one yourself python will automatically create one during execution. If you only want the prompt to be showing however, we will explore some ways you may do so.


Importing messagebox

The messagebox module comes bundled with the tkinter library, so if you have the tkinter library installed, you’re all set. Just use the following import statement to bring the messagebox module into your code.

from tkinter import messagebox

Types of Message Boxes

The messagebox module introduces several types of prompts that we may present, such as confirmation messages, warning messages and information messages.

messagebox.showinfo()

A very simple type of prompt that simply displays text to the user. Comes with an “i” for information sign embedded on the prompt.

from tkinter import messagebox

prompt = messagebox.showinfo(title = "Info", 
                             message = "Certain information")

messagebox.showwarning()

Identical to the messagebox.showinfo() function but it comes with a warning sign embedded onto the prompt. Used to give the user a sense of urgency regarding the information in the message.

from tkinter import messagebox

prompt = messagebox.showwarning(title = "Warning!",
                                message = "Warning, errors may occur")