message box widget and its methods
Information Warning and Error message boxes are part of tkinter in Python
Tkinter provides many widgets for GUI in Python.
We can improve look and feel of the GUI using many customization options.
messagebox
Tkinter provides various dialog boxes under messagebox widget.
Syntax
Here is the simple syntax to create this widget −
messageBox.FunctionName(title, message [, options])
Parameters
FunctionName − This is the name of the appropriate message box function.
title − This is the text to be displayed in the title bar of a message box.
message − This is the text to be displayed as a message.
options − options are alternative choices that you may use to tailor a standard message box. Some of the options that you can use are default and parent. The default option is used to specify the default button, such as ABORT, RETRY, or IGNORE in the message box. The parent option is used to specify the window on top of which the message box is to be displayed.
Various methods of messagebox widget
Depending on application we can use functions like :
showinfo()
showwarning()
showerror()
askquestion()
askyesno()
askokcancel()
askretrycancel()
showinfo()
This method is used to show information with icon i and ok button on dialog box.
Example
""" Author : ITVoyagers (itvoyagers.in) Date : 7th April 2020 Description : Program to show use of showinfo() method of messagebox widget """ from tkinter import messagebox import tkinter top =tkinter.Tk() def information(): messagebox.showinfo("Hello", "This is ITVoyagers") B1 = tkinter.Button(top, text = "Inform us", command = information,fg="purple",bg="pink",font="verdana") B1.pack()
OUTPUT

showwarning()
This method is used to show warning with its icon and ok button on dialog box.
Example
""" Author : ITVoyagers (itvoyagers.in) Date : 7th April 2020 Description : Program to show use of showinfo() method of messagebox widget """ from tkinter import messagebox import tkinter top =tkinter.Tk() def warning(): messagebox.showwarning("Hello", "This is warning from ITVoyagers") B1 = tkinter.Button(top, text = "Warn us", command = warning,fg="red",bg="pink",font="verdana") B1.pack()
OUTPUT

showerror()
This method is used to show error message with its icon and ok button on dialog box.
Example
""" Author : ITVoyagers (itvoyagers.in) Date : 7th April 2020 Description : Program to show use of showerror() method of messagebox widget """ from tkinter import messagebox import tkinter top =tkinter.Tk() def error(): messagebox.showerror("Hello", "This is error message from ITVoyagers") B1 = tkinter.Button(top, text = "Error message for us", command = error,fg="yellow",bg="black",font="verdana") B1.pack()
OUTPUT

askquestion() and askyesno()
This methods are used to ask question with its icon and yes or no button on dialog box.
Example
""" Author : ITVoyagers (itvoyagers.in) Date : 7th April 2020 Description : Program to show use of askquestion() method of messagebox widget """ from tkinter import messagebox import tkinter top =tkinter.Tk() def asking_question(): messagebox.askquestion("Hello", "Do you like itvoyagers?") B1 = tkinter.Button(top, text = "Click to get a question", command = asking_question,fg="green",bg="white",font="verdana") B1.pack()
OUTPUT

askyesno()
Example
""" Author : ITVoyagers (itvoyagers.in) Date : 7th April 2020 Description : Program to show use of askyesno() method of messagebox widget """ from tkinter import messagebox import tkinter top =tkinter.Tk() def asking_yesno(): messagebox.askyesno("Hello", "Do you like itvoyagers?") B1 = tkinter.Button(top, text = "Click to get a yes no question", command = asking_yesno,fg="blue",bg="white",font="verdana") B1.pack()
OUTPUT

askokcancel()
This method is used to create a dialog box with ok and cancel button.
Example
""" Author : ITVoyagers (itvoyagers.in) Date : 7th April 2020 Description : Program to show use of askokcancel() method of messagebox widget """ from tkinter import messagebox import tkinter top =tkinter.Tk() def asking_okcancel(): messagebox.askokcancel("Hello", "ITVoyagers wishes happy learning") B1 = tkinter.Button(top, text = "Click here for ok cancel dialogbox", command = asking_okcancel,fg="magenta",bg="black",font="verdana") B1.pack()
OUTPUT

askretrycancel()
This method is used to create a dialog box with retry and cancel button.
Example
""" Author : ITVoyagers (itvoyagers.in) Date : 7th April 2020 Description : Program to show use of askretrycancel() method of messagebox widget """ from tkinter import messagebox import tkinter top =tkinter.Tk() def asking_retrycancel(): messagebox.askretrycancel("Hello", "ITVoyagers shows use of retry cancel") B1 = tkinter.Button(top, text = "Click here for retry cancel dialogbox", command = asking_retrycancel,fg="yellow",bg="dark green",font="verdana") B1.pack()
OUTPUT

You can also check following posts to know more about tkinter and GUI
- What is tkinter?
- Layout / Geometry Manager in Python
- GUI programming in Python and Python GUI Library
- Events and Bindings in Python along with Widget configuration and styling
- Fonts Names, Font Descriptors, System Fonts, Text formatting, Borders, Relief Styles in Python
- Canvas widget of tkinter module – Python
- Widgets in tkinter module – Python
- Label, Text, Entry & Message Widget in Python
- Best post on Menu and Menubutton Widget of tkinter
- Best post on Button, Checkbutton & Radiobutton Widget in Python
- Best post- Listbox and Scrollbar(Slider) Widget-Python
- Best post on Frame Widget of tkinter in Python
- Best post on Spinbox and Scale Widget in Python
- Best post:Dimensions, Anchors, Bitmaps & Cursors in Python