Create a Yes/No Message Box in Python using tkinter

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

If that’s the case, 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 include the Python code to create a yes/no message box in Python, where:

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

To start, I’ll share the Python code to create the yes/no message box. I’ll then provide some explanations about the code. And finally, I’ll present the outcome of running the code.

The code 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 of the code.

import tkinter as tk
from tkinter import messagebox

root= tk.Tk() # create window

canvas1 = tk.Canvas(root, width = 800, height = 350)
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(97, 270, window=button1)
  
  
root.mainloop()

Background about the code

I’ll now break the code into few main parts to explain each component:

(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 = 800, height = 350)
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 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 the 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 screen. You can control the position where your button will be displayed, by changing the numeric values (under the create_window portion).

button1 = tk.Button (root, text='Exit Application',command=ExitApplication) 
canvas1.create_window(97, 270, 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 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) But if you end up pressing ‘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.