Tkinter Messagebox


Tkinter MessageBox module is used to display message boxes in your applications. This module provides a number of functions that you can use to display an appropriate message. According to its official documentation of messagebox, we have seven types of messagebox in Tkinter.

sample code

from tkinter import *
import  tkinter.messagebox
root = Tk()

tkinter.messagebox.showinfo('Window Title','This is the message')
tkinter.messagebox.showerror('Error Message', 'This is error')
tkinter.messagebox.showwarning('Warning Message', ' This is warning') 
root.mainloop()

when you run the above code, you receive the following dialog windows as output. We can run the above code without root= Tk( ). For more information about root= Tk and root.mainloop, please refer to Tkinter first Program.

Tkinter messagebox

The basic syntax of the messagebox is as follows

Information message box

tkinter.messagebox.showinfo(title=None, message=None, **options)

Warning message boxes

tkinter.messagebox.showwarning(title=None, message=None, **options)
tkinter.messagebox.showerror(title=None, message=None, **options)

Question message boxes

tkinter.messagebox.askquestion(title=None, message=None, **options)
tkinter.messagebox.askokcancel(title=None, message=None, **options)
tkinter.messagebox.askretrycancel(title=None, message=None, **options)
tkinter.messagebox.askyesno(title=None, message=None, **options)
tkinter.messagebox.askyesnocancel(title=None, message=None, **options)

Question message boxes not only display the desired message but also return values in the form of Yes, No, True, False.

from tkinter import *
import  tkinter.messagebox
root = Tk()
yesno  = tkinter.messagebox.askyesno('Question',"Are you sure to delete this record ?")

if yesno ==True :
    print("You selected to delete record") 

root.mainloop()


messagebox is a very useful widget that can help you a lot to show your information. Besides these messageboxes, the Simpledialog module has functions to receive inputs. In our Next Tutorial, we will explore more about simpledialog.

Print Friendly, PDF & Email

Related Posts

If you like CBSEToaday and would like to contribute, you can also write an article using submit article or mail your article to contribute@cbsetoday.com See your article appearing on the cbsetoday.com main page and help other students/teachers.