Best post: message box widget and its methods

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

Output of showinfo() in messagebox widget. (itvoyagers.in)

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

Output of showwarning() of messagebox widget (itvoyagers.in)

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

Output of showerror()  in messagebox widget (itvoyagers.in)



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

Output of askquestion() in messagebox widget. (itvoyagers.in)

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

Output of askyesno() in messagebox widget (itvoyagers.in)

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

Output of askokcancel() in messagebox widget (itvoyagers.in)

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

Output of retrycancel() in messagebox widget (itvoyagers.in)

You can also check following posts to know more about tkinter and GUI

Leave a Comment