Python tkinter Message Box

How to create tkinter message box

While working with GUI for building applications, the message box is an important component.

It enables you to display a confirmation message, a warning, or a simple information message to the user.

For example, after entering data in the textboxes, you press the “Save” button. After saving the information, it will display the confirmation message that the record is saved successfully.

Similarly, you want to delete a record or file and before that, a warning message box appears that alerts the user for critical action before proceeding.

Python Tkinter library enables us to easily create a message box with various options.

Syntax

To create a message box of any available type, the following syntax is used:

messagebox.Message(title, message [, options])

Following categories/message box types can be used:

  • Information Message box:
  • Warning Message Boxes
  • Question Message Boxes

See the examples below with various functions (for simple, warning, error, etc.) message boxes.

An example of the information message box

In order to display a message box with text information and an OK button, you may use the showinfo function/message as shown in the example below:

Code:

from tkinter import messagebox 

messagebox.showinfo("Hello World!","This is simple information message box")

Output:

tkinter-messagebox-info

As you execute this program, it will simply display a message box with the title “Hello World”.

An example of the error message box

To display a message box with an error sound and icon, you may use the “showerror” method. See an example below:

Code:

from tkinter import messagebox 

messagebox.showerror("Error Message!","File cannot be deleted!")

Output:

tkinter-messagebox-erro

Display warning message box example

Similarly, for the warning error message, use the showwarning method.

Code:

from tkinter import messagebox 


messagebox.showwarning("Warning Box!","A warning message here!")

Output:

tkinter-message-warning

Triggering a message box upon clicking a button example

Before showing you examples of Question message boxes, let us show you an example where we will trigger a message box as you click on the button.

After running the program, click on the “Trigger Info Messagebox” button:

Code:

#Example of button command / message box

from tkinter import *

from tkinter import messagebox

top = Tk()

#Setting window size containing button

top.geometry("250x150")

#Function to call as button is clicked/pressed

def msgbox():

    messagebox.showinfo("Welcome", "A demo of button with message box!")



#Specify button option / command with a function call

btn = Button(top,text = "Trigger Info Messagebox", command = msgbox)


btn.pack()

top.mainloop()

Output:

tkinter-message-button

Asking a question in the message box example

For creating a message box with a question and Yes/No buttons, you may use the askquestion method.

Code:

from tkinter import messagebox 


messagebox.askquestion("Asking Question!","Do you really want to exit?")

Output:

tkinter-message-question

Writing the code based on clicked button behavior

So which button is clicked/pressed by the user and what to performe based on it?

In the example below, we used askyesno message box (method) and wrote the code to check if “Yes” or “No” button is clicked.

  • If the user presses the Yes button, it executes yes_msg() custom function.
  • In this function, we just triggered another info message box that notified the user that you clicked the “Yes” button.
  • Similarly, if the No button is pressed, it executes, no_msg() function.
  • In that function, we displayed another info message box telling the user that the No button is pressed.

Code:

from tkinter import messagebox 

#Functions to deal with User Reponse

def yes_msg():

    messagebox.showinfo("Yes Response", "You clicked Yes button") 


def no_msg():

    messagebox.showinfo("No Response", "You clicked No button")

#Logic to catch whether user pressed Yes or No button 

if messagebox.askyesno("Asking Question!","Your choice: Yes/No") == True:

    yes_msg()

else:

    no_msg()

Output when Yes is pressed:

tkinter-msg-yes-no-logi

An example of OK/Cancel message box

Rather than displaying the Yes/No buttons, you may show the OK/Cancel buttons with a question icon. For that, use the askokcancel method:

Code:

from tkinter import messagebox 


messagebox.askokcancel("Asking Question!","Press Ok to proceed or Cancel to get back!")

Output:

tkinter-message-q-cancel

Yes/No/Cancel message box example

A message box with the question icon can also be displayed with Yes/No and Cancel options.

Use the askyesnocancel method for that purpose.

Code:

from tkinter import messagebox 



messagebox.askyesnocancel("Asking Question!","Your choice: Yes/No/Cancel")

Output:

Author - Atiq Zia

Atiq is the writer at jquery-az.com, an online tutorial website started in 2014. With a passion for coding and solutions, I navigate through various languages and frameworks. Follow along as we solve the mysteries of coding together!