Different messages in Tkinter - Python

Tkinter is Python's built-in GUI toolkit that provides various message display options for user interactions and program state changes. The Message widget displays multi-line text with customizable formatting, while the messagebox module offers standard dialog boxes like confirmations, errors, and warnings.

Custom Message Widget

The Message widget displays text with customizable appearance ?

import tkinter as tk

main = tk.Tk()

key = "The key to success is to focus on goals and not on obstacles"

message = tk.Message(main, text=key)
message.config(bg='lightblue', font=('Arial', 14, 'italic'), width=300)

message.pack(padx=20, pady=20)
main.mainloop()

This creates a customizable text display with background color, font styling, and width control.

Standard Message Boxes

The messagebox module provides pre-built dialog boxes for common user interactions.

Question Dialog

Use askquestion() to get yes/no responses ?

from tkinter.messagebox import askquestion

result = askquestion("Question", "Do you want to proceed?")
print(f"User clicked: {result}")
User clicked: yes

Retry/Cancel Dialog

Use askretrycancel() for retry operations ?

from tkinter.messagebox import askretrycancel

result = askretrycancel("Retry", "Operation failed. Try again?")
print(f"User choice: {result}")
User choice: True

Error Message

Use showerror() to display error notifications ?

from tkinter.messagebox import showerror

showerror("Error", "File not found!")
print("Error dialog displayed")
Error dialog displayed

Warning Message

Use showwarning() for warning notifications ?

from tkinter.messagebox import showwarning

showwarning("Warning", "This action cannot be undone!")
print("Warning dialog displayed")
Warning dialog displayed

Common Message Box Functions

Function Purpose Return Values
askquestion() Yes/No question 'yes' or 'no'
askretrycancel() Retry operation True or False
showerror() Error notification 'ok'
showwarning() Warning notification 'ok'
showinfo() Information display 'ok'

Complete Example

Here's a comprehensive example showing multiple message types ?

import tkinter as tk
from tkinter import messagebox

def show_info():
    messagebox.showinfo("Info", "Operation completed successfully!")

def show_warning():
    messagebox.showwarning("Warning", "Please save your work!")

def ask_question():
    result = messagebox.askquestion("Question", "Exit application?")
    if result == 'yes':
        root.destroy()

root = tk.Tk()
root.title("Message Box Demo")
root.geometry("300x200")

tk.Button(root, text="Show Info", command=show_info).pack(pady=10)
tk.Button(root, text="Show Warning", command=show_warning).pack(pady=10)
tk.Button(root, text="Ask Question", command=ask_question).pack(pady=10)

root.mainloop()

Conclusion

Tkinter provides both customizable Message widgets for formatted text display and standard messagebox dialogs for user interactions. Use message boxes for consistent user experience and the Message widget for custom text presentations.

Updated on: 2026-03-15T17:12:09+05:30

522 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements