Back to: Python Programming
The tkinter.messagebox module provides standard dialog boxes to display messages or ask user confirmation in a Tkinter application. These are modal windows, meaning they block other actions in the application until the user closes them.

To use it, you must import the module specifically:
from tkinter import *
from tkinter import messagebox # Imports the messagebox library
Common Message Box Types
The module provides different functions based on the purpose of the alert or the type of user response needed.
| Function | Purpose | Typical Return Value |
|---|---|---|
showinfo() | General information/success alerts. | "ok" |
showwarning() | Alerting user to potential issues. | "ok" |
showerror() | Reporting a failure or error. | "ok" |
askyesno() | Asking a simple Yes/No question. | True / False |
askokcancel() | Confirming an action. | True / False |
Utilising showinfo()

Here’s the code:
from tkinter import *
from tkinter import messagebox
def click():
messagebox.showinfo(title='This is the info messagebox title',
message='This is the info messagebox content')
window = Tk()
button = Button(window,command=click,text='click me')
button.pack()
window.mainloop()
Utilising showwarning()
from tkinter import *
from tkinter import messagebox
def click():
messagebox.showwarning(title='This is the warning messagebox title',
message='This is the warning messagebox content')
window = Tk()
button = Button(window,command=click,text='click me')
button.pack()
window.mainloop()

Utilising showerror()
from tkinter import *
from tkinter import messagebox
def click():
messagebox.showerror(title='This is the ERROR messagebox title',
message='This is the ERROR messagebox content')
window = Tk()
button = Button(window,command=click,text='click me')
button.pack()
window.mainloop()

Utilising askokcancel()
from tkinter import *
from tkinter import messagebox
def click():
if messagebox.askokcancel(title='Ask OK Cancel',message='Do you want to click OK?'):
print('You clicked OK')
else:
print('You chickened out!')
window = Tk()
button = Button(window,command=click,text='click me')
button.pack()
window.mainloop()

Utilising askyesno() – returns values “True” or “False”
from tkinter import *
from tkinter import messagebox
def click():
if messagebox.askyesno(title='Ask YES or NO',message='Do you like Python?'):
print('Me too! Python is amazing!!')
else:
print('Thanks for your feedback!')
window = Tk()
button = Button(window,command=click,text='click me')
button.pack()
window.mainloop()

Utilising askyesnocancel() – returns values “True”, “False” or “None”
from tkinter import *
from tkinter import messagebox
def click():
answer = messagebox.askyesnocancel(title='Ask YES or NO',message='Do you like Python?')
if answer == True:
print('Me too! Coding is cool!!')
elif answer == False:
print('You have come to the wrong place!!')
else:
print('You\'re dodging the question')
window = Tk()
button = Button(window,command=click,text='click me')
button.pack()
window.mainloop()

Example
Can you work out what this code does? Paste it into your IDE to find out!
from tkinter import *
from tkinter import messagebox
def on_closing():
# Asks a yes/no question and captures the boolean result
if messagebox.askyesno("Quit", "Do you really want to exit?"):
window.destroy()
window = Tk()
window.geometry("200x100")
btn = Button(window, text="Exit Application", command=on_closing)
btn.pack(pady=20)
window.mainloop()