Create a Yes/No Message Box in Python using tkinter

In this short guide, you’ll see how to create a Yes/No message box in Python using tkinter. This includes:

  • A simple GUI with one button, called the ‘Exit Application‘ button
  • When you click 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 the Yes/No message box in Python

Here is the full code to create the Yes/No message box in Python using tkinter.

In the next section, you’ll see an explanation about each component used in the code.

import tkinter as tk
from tkinter import messagebox

root = tk.Tk()

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


def exit_application():
    msg_box = tk.messagebox.askquestion(
        "Exit Application",
        "Are you sure you want to exit the application?",
        icon="warning",
    )
    if msg_box == "yes":
        root.destroy()
    else:
        tk.messagebox.showinfo(
            "Return", "You will now return to the application screen"
        )


button = tk.Button(
    root, text="Exit Application", command=exit_application, bg="brown", fg="white"
)
canvas.create_window(150, 150, window=button)

root.mainloop()

Background about the code

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

(1) First, import the tkinter package. This package is used to create a Graphical User Interface (GUI) in Python:

import tkinter as tk
from tkinter import messagebox

(2) Then, 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 display by modifying the numeric values below:

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

(3) Now, create a function to be called by the Exit Application button.

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

msg_box = tk.messagebox.askquestion (‘Type here the title for 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 msg_box == “yesthen the GUI will be closed
  • else: another message box would appear on your screen to indicate that you’ll now return back to the application screen
def exit_application():
    msg_box = tk.messagebox.askquestion(
        "Exit Application",
        "Are you sure you want to exit the application?",
        icon="warning",
    )
    if msg_box == "yes":
        root.destroy()
    else:
        tk.messagebox.showinfo(
            "Return", "You will now return to the application screen"
        )

(4) Finally, add a button to call the ‘exit_application‘ function above.

The button should be placed on the GUI. You can control the position of the button by changing the numeric values below:

button = tk.Button(
    root, text="Exit Application", command=exit_application, bg="brown", fg="white"
)
canvas.create_window(150, 150, window=button)

Running the Python Code

Run the Python code, and you’ll see a simple GUI that has a single button to exit the application.

Press on the ‘Exit Application‘ button, and you’ll get a message box, where you’ll have two options to choose from. If you you click on ‘Yes‘, you’ll exit the application.

Are you sure you want to exit the application?
Yes      No

However, if you click on ‘No‘, you’ll get this second message box:

You will now return to the application screen

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

You may check the following guide for a more comprehensive GUI that you can create using tkinter, or visit the tkinter documentation.

Leave a Comment