python Message

The Message widget in Tkinter is used to display text messages to the user. Unlike the Label widget, the Message widget is specifically designed to handle multi-line text and offers text wrapping and alignment features.

Syntax:

w = Message(parent, options)

Here, parent is the parent widget (such as a window or frame), and options are used to configure the appearance and behavior of the Message widget.

Options for Tkinter Message:

SN Option Description
1 anchor Determines the exact position of the text within the widget. Default is CENTER.
2 bg Background color of the widget.
3 bitmap Used to display a graphical object or image on the widget.
4 bd Border width of the widget. Default is 2 pixels.
5 cursor Mouse pointer type when it hovers over the widget (e.g., arrow, dot).
6 font Font type of the text displayed in the widget.
7 fg Font color of the text displayed in the widget.
8 height Vertical dimension of the widget.
9 image Static image to display on the widget.
10 justify Text alignment for multiple lines. Possible values: LEFT, CENTER (default), RIGHT.
11 padx Horizontal padding of the widget.
12 pady Vertical padding of the widget.
13 relief Type of border around the widget. Default is FLAT.
14 text Text to be displayed in the widget.
15 textvariable A StringVar control variable to dynamically control the text shown in the widget.
16 underline Specifies which letter of the text will be underlined. Default is -1 (no underline).
17 width Horizontal dimension of the widget, specified in number of characters (not pixels).
18 wraplength Number of characters at which the text should wrap.

Example: Using the Message Widget

Here's a simple example of how to use the Message widget in a Tkinter application:

from tkinter import *

# Create the main window
root = Tk()
root.title("Tkinter Message Widget Example")

# Create a Message widget
message = Message(root,
    text="This is a message widget. It can display multi-line text with automatic wrapping. You can adjust the width, padding, and text alignment.",
    width=300,
    font=("Arial", 12),
    bg="lightblue",
    fg="black",
    justify=LEFT,
    padx=10,
    pady=10,
    relief=RAISED)

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

# Run the application
root.mainloop()

Explanation of the Example:

text: Specifies the text to be displayed in the Message widget.

width: Sets the width of the widget in terms of characters.

font: Sets the font type and size for the text.

bg: Background color of the widget.

fg: Foreground color (text color) of the widget.

justify: Aligns the text within the widget. In this case, it is set to LEFT.

padx and pady: Adds padding around the text within the widget.

relief: Sets the border type. Here, RAISED is used to give the widget a 3D effect.

The Message widget is useful for displaying informational or instructional text in a user-friendly manner.