New Tutorials:   TKINTER    KOTLIN    JAVASCRIPT    SASS/SCSS    PL/SQL    Matplotlib    C++ Programs
See the Tutorial List

Python Tkinter MessageBox

In this tutorial, we will cover how to create and use Tkinter MessageBox while developing desktop applications.

In order to display message boxes in a desktop application, we use the MessageBox module in Tkinter.

  • There are various functions present in this module which helps to provide an appropriate type of message according to the requirement.

  • With the help of this module, we can create pop-up message boxes to take user input.

  • The functions of the MessageBox module are as follows: showError(), askretrycancel(), showwarning(), etc., all of which are used to create a messagebox.

Tkinter MessageBox

To use the messagebox module, we first need to import it in our python script:

from tkinter import messagebox

Then following is the basic syntax to use the messagebox:

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

In the above syntax, we have used the following:

  • function_name

    This is used to indicate the name of the appropriate MessageBox Function.

  • title

    This is used to indicate the text to be displayed in the title bar of the appropriate message box.

  • message

    This is used to indicate the text to be displayed as a message in the message box.

  • options

    It is used to indicate various options in order to configure the MessageBox. There are two values of it and these are default and parent.

    • default: It is used to specify a default button such as ABORT, RETRY, IGNORE.

    • parent: It is used to specify a window on the top of which we will display the MessageBox.

The functions present in the MessageBox module uses the same syntax but the functionalities of each function are different.

Let us see a few functions of the Tkinter MessageBox module.

Tkinter MessageBox - showwarning()

This method is used to display any warning to the user in a Python application.

Here is a code for the same:

from tkinter import *  
  
from tkinter import messagebox  
  
win = Tk()  
win.geometry("200x200")  
messagebox.showwarning("warning","This is a Warning")  
  
win.mainloop()  


Tkinter messagebox module example

Tkinter MessageBox - askquestion()

This method in MessageBox is mainly used to ask some question to the user which can be answered either in Yes or No.

The code for this method is as follows:

from tkinter import *  
from tkinter import messagebox  
  
win = Tk()  
win.geometry("100x100")  
messagebox.askquestion("Confirm","Are you sure about it?")  
win.mainloop()  


Tkinter messagebox module example

Tkinter MessageBox - askretrycancel()

If you want to ask a user to do any particular task or not then this method will be used.

Let us see the code for the same:

from tkinter import *  
from tkinter import messagebox  
  
win= Tk()  
win.geometry("100x100")  
messagebox.askretrycancel("Application"," wanna try again?")  
  
win.mainloop()  


Tkinter messagebox module example

Tkinter MessageBox - showerror()

To display an error message this method will be used.

Let us see the code snippet given below:

from tkinter import *  
from tkinter import messagebox  
  
top = Tk()  
top.geometry("100x100")  
messagebox.showerror("errorWindow","oops!!!Error")  
top.mainloop()  


Tkinter messagebox module example

Summary:

In this tutorial, we learned about the different types of message boxes that we can create to show information to user or to take input from users, like to confirm any action, etc. The message boxes are like pop-ups in which we can show errors, warnings, etc. to the user.