Hit enter after type your search item

Python tkinter message box

How to create tkinter messagebox

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

It enables you to send a confirmation message to the user, a warning, a simple information message, and so on.

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 easily creating the 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:


Output:

tkinter-messagebox-info

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

An example of 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:


Output:

tkinter-messagebox-erro

Display warning message box example

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

Code:


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:


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:


Output:

tkinter-message-question

Writing the code based on clicked button behavior

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

In the example below, we used askyesno message box (method) and write the code 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 notify the user that you clicked “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:


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 Yes/Cancel buttons with a question icon. For that, use the askokcancel method:

Code:


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:


Output:

This div height required for enabling the sticky sidebar