Create a Yes/No Message Box in Python using tkinter

Need to create a Yes/No message box in Python?

If so, I’ll show you the steps to create such a message box using tkinter.

What will be covered in this tutorial

In this tutorial, I’ll share the Python code to create a yes/no message box in Python. This includes:

  • A simple GUI with one button, called the ‘Exit Application’ button
  • When you press on that button, you’ll have a choice between:
    • Yes – to close the GUI
    • No – to return back to the main display

The code used to create a yes/no message box in Python

Here is the full code to create a yes/no message box in Python. In the next section, I’ll explain each component that was applied in the code.

import tkinter as tk
from tkinter import messagebox

root= tk.Tk() # create window

canvas1 = tk.Canvas(root, width = 300, height = 300)
canvas1.pack()

def ExitApplication():
    MsgBox = tk.messagebox.askquestion ('Exit Application','Are you sure you want to exit the application',icon = 'warning')
    if MsgBox == 'yes':
       root.destroy()
    else:
        tk.messagebox.showinfo('Return','You will now return to the application screen')
        
button1 = tk.Button (root, text='Exit Application',command=ExitApplication)
canvas1.create_window(150, 150, window=button1)
  
root.mainloop()

Background about the code

Let’s now review the components used in the code.

(1) Initially, you’ll need to import the tkinter package. This package is used to create Graphical User Interfaces (GUI) in Python:

import tkinter as tk
from tkinter import messagebox

(2) You’ll also need to create the Canvas, which will become your GUI screen. You can then place items, such as buttons, on the canvas.

Note that you can change the width and height of your GUI screen by modifying the numeric values for your canvas.

canvas1 = tk.Canvas(root, width = 300, height = 300)
canvas1.pack()

(3) Now you’ll need to create a function/command to be called by the Exit Application button (aka button1).

When you press on the button, a message box would appear on your screen. You can edit the text within the message box:

MsgBox = tk.messagebox.askquestion (‘Type here the title of your message box’,’type here the content that will be displayed within the message box’)

Then, you’ll need to use an If function:

  • If MsgBox == ‘yes’ then the GUI will be closed
  • else: another message box will appear on your screen to indicate that you’ll now return back to the application screen
def ExitApplication():
    MsgBox = tk.messagebox.askquestion ('Exit Application','Are you sure you want to exit the application',icon = 'warning')
    if MsgBox == 'yes':
       root.destroy()
    else:
        tk.messagebox.showinfo('Return','You will now return to the application screen')

(4) Finally, you’ll need to add a button (aka button1) to call the ‘ExitApplication’ function/command above.

The button should be placed on the GUI. You can control the position where your button will be displayed, by changing the numeric values (under the create_window syntax).

button1 = tk.Button (root, text='Exit Application',command=ExitApplication) 
canvas1.create_window(150, 150, window=button1)

Running the Python Code

(1) Run the Python code and you’ll see this simple GUI that has a single button to exit the application:

Create a Yes/No Message Box in Python using tkinter

(2) Press on the ‘Exit Application’ button.

You’ll see the following message box, where you’ll have two options to choose from. If you press ‘Yes’, you will exit the application.

Exit application Python

(3) However, if you press ‘No’, you’ll get this second message box:

Message Box in Python

Once you press ‘OK’ you’ll return back to the application screen.

You can check this link for a more comprehensive GUI that you can create using tkinter, or simply check the tkinter documentation.