The messagebox module in Tkinter is used to display various types of message dialogs. These dialogs can show information, warnings, errors, or ask for user confirmation. Here’s how you can use different messagebox functions:
messagebox.function_name(title, message [, options])
• function_name: The function to call (e.g., showinfo, showwarning).
• title: The title of the message box.
• message: The message to be displayed in the message box.
• options: Optional parameters such as default and parent.
1. default: Specifies the default button (ABORT, RETRY, or IGNORE).
2. parent: Specifies the parent window on top of which the message box is displayed.
from tkinter import messagebox
messagebox.showinfo("Information", "This is an info message.")
from tkinter import messagebox
messagebox.showwarning("Warning", "This is a warning message.")
from tkinter import messagebox
messagebox.showerror("Error", "This is an error message.")
from tkinter import messagebox
response = messagebox.askquestion("Question", "Do you want to continue?")
if response == 'yes':
print("User chose Yes.")
else:
print("User chose No.")
from tkinter import messagebox
response = messagebox.askokcancel("Confirmation", "Do you want to save changes?")
if response:
print("User chose OK.")
else:
print("User chose Cancel.")
from tkinter import messagebox
response = messagebox.askyesno("Confirm", "Are you sure you want to exit?")
if response:
print("User chose Yes.")
else:
print("User chose No.")
from tkinter import messagebox
response = messagebox.askretrycancel("Retry", "Failed to connect. Retry?")
if response:
print("User chose Retry.")
else:
print("User chose Cancel.")
import tkinter as tk
from tkinter import messagebox
def show_info():
messagebox.showinfo("Information", "This is an info message.")
def show_warning():
messagebox.showwarning("Warning", "This is a warning message.")
def show_error():
messagebox.showerror("Error", "This is an error message.")
def ask_question():
response = messagebox.askquestion("Question", "Do you want to continue?")
if response == 'yes':
print("User chose Yes.")
else:
print("User chose No.")
def ask_ok_cancel():
response = messagebox.askokcancel("Confirmation", "Do you want to save changes?")
if response:
print("User chose OK.")
else:
print("User chose Cancel.")
def ask_yes_no():
response = messagebox.askyesno("Confirm", "Are you sure you want to exit?")
if response:
print("User chose Yes.")
else:
print("User chose No.")
def ask_retry_cancel():
response = messagebox.askretrycancel("Retry", "Failed to connect. Retry?")
if response:
print("User chose Retry.")
else:
print("User chose Cancel.")
# Create main window
root = tk.Tk()
root.title("MessageBox Example")
# Create buttons to trigger message boxes
tk.Button(root, text="Show Info", command=show_info).pack(pady=5)
tk.Button(root, text="Show Warning", command=show_warning).pack(pady=5)
tk.Button(root, text="Show Error", command=show_error).pack(pady=5)
tk.Button(root, text="Ask Question", command=ask_question).pack(pady=5)
tk.Button(root, text="Ask OK/Cancel", command=ask_ok_cancel).pack(pady=5)
tk.Button(root, text="Ask Yes/No", command=ask_yes_no).pack(pady=5)
tk.Button(root, text="Ask Retry/Cancel", command=ask_retry_cancel).pack(pady=5)
# Run the Tkinter event loop
root.mainloop()