Create a Yes/No Message Box in Python using tkinter - GeeksforGeeks

26 Mar, 2020
Python offers a number Graphical User Interface(GUI) framework but Tk interface or tkinter is the most widely used framework. It is cross-platform which allows the same code to be run irrespective of the OS platform (Windows, Linux or macOS). Tkinter is lightweight, faster and simple to work with. Tkinter provides a variety of widgets that can be customized using standard attributes and geometry management methods. The Tkinter message box can be used to ask questions or display messages to the user.
Note: For more information, refer to Python GUI – tkinter
Steps to create a tkinter message box :
Import tkinter moduleimport tkinter as tk
from tkinter import *Note: Name of the module in Python 2.x is ‘Tkinter’ and in Python 3.x it is ‘tkinter’. Python 3.x is used here.Import tkinter messagebox widgetfrom tkinter import messagebox as mbCreate the method that is called to display the Yes/No Message Boxdef call():
    res=mb.askquestion('Exit Application', 'Do you really want to exit')
    if res == 'yes' :
        root.destroy()
    else :
        mb.showinfo('Return', 'Returning to main application')
Explanation:Syntax:askquestion(title=None, message=None, **options) Parametertitle: used to give a name which is displayed in as header of the dialog box.message: question for the user.Return value: Returns ‘yes’ when the yes option is clicked and ‘no’ when the no option is clicked.Syntax:showinfo(title=None, message=None, **options)Parametertitle: used to give a name which is displayed in as header of the dialog box.message: information for the user.Syntax:destroy() This method destroys a widget.Create the canvas for the button will be placedroot=tk.Tk()
canvas=tk.Canvas(root, width=200, height=200)
canvas.pack()
Explanation:Syntax:Tk(screenName=None,  baseName=None,  className=’Tk’,  useTk=1)Used to create the parent window. Tk class is instantiated without any arguments.The name of the parent window can be changed to desired one by changing the value of className argument. Here ‘root’ is the parent window.Syntax:Canvas(master, option=value)Parameter:master: used to represent the parent window.Here ‘root’ is the master.option: used to specify border, background color, height, width etc .Return Value: The method returns a string (.!canvas) .Syntax:pack(**options) Organizes the widgets in blocks before placing in the parent widget.The options can be used to expand, fill and specify side(left, right, top, bottom)Create the button and place it inside the canvasb=Button(root, text='Quit Application', command=call)
canvas.create_window(100, 100, window=b)
Explanation:Syntax:Button(master=None, options)Parameter:master: Here root is the parent window.options:There are a number of supported options. The options used in this case are text and command.text: button textcommand: the action or method that is to be invoked when the button is pressed.Return Value: The method returns a string (.!button) .Syntax:create_window(x, y, **options)Parameter:x, y: Specifies the position of the widget(button) within the canvas.options: There are a variety of options supported like anchor, height, width, state, tags, window. The option used here is window.window: window=b where b is the widget(button) to be placed on the canvas.Return Value: Returns the object ID for the window object.Call the mainloop() methodroot.mainloop()
Explanation:Syntax:mainloop()It is an infinite loop that is called when the program is ready to be run.It waits for an event(mouse clicks) to occur and as soon as the event is received the event is processed.The mainloop() runs as long as the parent window is not destroyed.
Import tkinter moduleimport tkinter as tk
from tkinter import *Note: Name of the module in Python 2.x is ‘Tkinter’ and in Python 3.x it is ‘tkinter’. Python 3.x is used here.
import tkinter as tk
from tkinter import *
Note: Name of the module in Python 2.x is ‘Tkinter’ and in Python 3.x it is ‘tkinter’. Python 3.x is used here.
Import tkinter messagebox widgetfrom tkinter import messagebox as mb
from tkinter import messagebox as mb
Create the method that is called to display the Yes/No Message Boxdef call():
    res=mb.askquestion('Exit Application', 'Do you really want to exit')
    if res == 'yes' :
        root.destroy()
    else :
        mb.showinfo('Return', 'Returning to main application')
Explanation:Syntax:askquestion(title=None, message=None, **options) Parametertitle: used to give a name which is displayed in as header of the dialog box.message: question for the user.Return value: Returns ‘yes’ when the yes option is clicked and ‘no’ when the no option is clicked.Syntax:showinfo(title=None, message=None, **options)Parametertitle: used to give a name which is displayed in as header of the dialog box.message: information for the user.Syntax:destroy() This method destroys a widget.
def call():
    res=mb.askquestion('Exit Application', 'Do you really want to exit')
    if res == 'yes' :
        root.destroy()
    else :
        mb.showinfo('Return', 'Returning to main application')

Explanation:Syntax:
askquestion(title=None, message=None, **options) 
Parameter
title: used to give a name which is displayed in as header of the dialog box.
message: question for the user.
Return value: Returns ‘yes’ when the yes option is clicked and ‘no’ when the no option is clicked.
Syntax:
showinfo(title=None, message=None, **options)
Parameter
title: used to give a name which is displayed in as header of the dialog box.
message: information for the user.
Syntax:
destroy() 
This method destroys a widget.
Create the canvas for the button will be placedroot=tk.Tk()
canvas=tk.Canvas(root, width=200, height=200)
canvas.pack()
Explanation:Syntax:Tk(screenName=None,  baseName=None,  className=’Tk’,  useTk=1)Used to create the parent window. Tk class is instantiated without any arguments.The name of the parent window can be changed to desired one by changing the value of className argument. Here ‘root’ is the parent window.Syntax:Canvas(master, option=value)Parameter:master: used to represent the parent window.Here ‘root’ is the master.option: used to specify border, background color, height, width etc .Return Value: The method returns a string (.!canvas) .Syntax:pack(**options) Organizes the widgets in blocks before placing in the parent widget.The options can be used to expand, fill and specify side(left, right, top, bottom)
root=tk.Tk()
canvas=tk.Canvas(root, width=200, height=200)
canvas.pack()

Explanation:Syntax:
Tk(screenName=None,  baseName=None,  className=’Tk’,  useTk=1)
Used to create the parent window. Tk class is instantiated without any arguments.The name of the parent window can be changed to desired one by changing the value of className argument. Here ‘root’ is the parent window.
Syntax:
Canvas(master, option=value)
Parameter:
master: used to represent the parent window.Here ‘root’ is the master.
option: used to specify border, background color, height, width etc .
Return Value: The method returns a string (.!canvas) .
Syntax:
pack(**options) 
Organizes the widgets in blocks before placing in the parent widget.The options can be used to expand, fill and specify side(left, right, top, bottom)
Create the button and place it inside the canvasb=Button(root, text='Quit Application', command=call)
canvas.create_window(100, 100, window=b)
Explanation:Syntax:Button(master=None, options)Parameter:master: Here root is the parent window.options:There are a number of supported options. The options used in this case are text and command.text: button textcommand: the action or method that is to be invoked when the button is pressed.Return Value: The method returns a string (.!button) .Syntax:create_window(x, y, **options)Parameter:x, y: Specifies the position of the widget(button) within the canvas.options: There are a variety of options supported like anchor, height, width, state, tags, window. The option used here is window.window: window=b where b is the widget(button) to be placed on the canvas.Return Value: Returns the object ID for the window object.
b=Button(root, text='Quit Application', command=call)
canvas.create_window(100, 100, window=b)

Explanation:Syntax:
Button(master=None, options)
Parameter:
master: Here root is the parent window.
options:There are a number of supported options. The options used in this case are text and command.text: button textcommand: the action or method that is to be invoked when the button is pressed.
text: button text
command: the action or method that is to be invoked when the button is pressed.
Return Value: The method returns a string (.!button) .
Syntax:
create_window(x, y, **options)
Parameter:
x, y: Specifies the position of the widget(button) within the canvas.
options: There are a variety of options supported like anchor, height, width, state, tags, window. The option used here is window.window: window=b where b is the widget(button) to be placed on the canvas.
window: window=b where b is the widget(button) to be placed on the canvas.
Return Value: Returns the object ID for the window object.
Call the mainloop() methodroot.mainloop()
Explanation:Syntax:mainloop()It is an infinite loop that is called when the program is ready to be run.It waits for an event(mouse clicks) to occur and as soon as the event is received the event is processed.The mainloop() runs as long as the parent window is not destroyed.
root.mainloop()

Explanation:Syntax:
mainloop()
It is an infinite loop that is called when the program is ready to be run.It waits for an event(mouse clicks) to occur and as soon as the event is received the event is processed.The mainloop() runs as long as the parent window is not destroyed.
The complete program is as follows:
# Python program to create # yes/no message box    import tkinter as tkfrom tkinter import * from tkinter import messagebox as mb    def call():    res = mb.askquestion('Exit Application',                          'Do you really want to exit')          if res == 'yes' :        root.destroy()              else :        mb.showinfo('Return', 'Returning to main application')  # Driver's coderoot = tk.Tk()canvas = tk.Canvas(root,                    width = 200,                    height = 200)  canvas.pack()b = Button(root,           text ='Quit Application',           command = call)  canvas.create_window(100, 100,                      window = b)  root.mainloop()
Output:
Python-tkinter
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
How To Convert Python Dictionary To JSON?
How to drop one or multiple columns in Pandas Dataframe
Check if element exists in list in Python
Selecting rows in pandas DataFrame based on conditions
Python | os.path.join() method
Defaultdict in Python
Python | Get unique values from a list
Create a directory in Python
Python | Pandas dataframe.groupby()