The Message widget is a variant of the Label, designed to display multiline messages. The message widget can wrap text, and adjust its width to maintain a given aspect ratio.
When to use the Message Widget
The widget can be used to display short text messages, using a single font. You can often use a plain Label instead. If you need to display text in multiple fonts, use a Text widget.
Patterns #
To create a message, all you have to do is to pass in a text string. The widget will automatically break the lines, if necessary.
from tkinter import * master = Tk() w = Message(master, text="this is a message") w.pack() mainloop()
If you don’t specify anything else, the widget attepts to format the text to keep a given aspect ratio. If you don’t want that behaviour, you can specify a width:
w = Message(master, text="this is a relatively long message", width=50)
w.pack()
Reference #
- Message(master=None, **options) (class) [#]
-
A multi-line text message.
- master
- Parent widget.
- **options
- Widget options. See the description of the config method for a list of available options.
- config(**options) [#]
-
Modifies one or more widget options. If no options are given, the method returns a dictionary containing all current option values.
- **options
- Widget options.
- anchor=
- Where in the message widget the text should be placed. Use one of N, NE, E, SE, S, SW, W, NW, or CENTER. Default is CENTER. (the database name is anchor, the class is Anchor)
- aspect=
- Aspect ratio, given as the width/height relation in percent. The default is 150, which means that the message will be 50% wider than it is high. Note that if the width is explicitly set, this option is ignored. (aspect/Aspect)
- background=
- Message background color. The default value is system specific. (background/Background)
- bg=
- Same as background.
- borderwidth=
- Border width. Default value is 2. (borderWidth/BorderWidth)
- bd=
- Same as borderwidth.
- cursor=
- What cursor to show when the mouse is moved over the message widget. The default is to use the standard cursor. (cursor/Cursor)
- font=
- Message font. The default value is system specific. (font/Font)
- foreground=
- Text color. The default value is system specific. (foreground/Foreground)
- fg=
- Same as foreground.
- highlightbackground=
- Together with highlightcolor and highlightthickness, this option controls how to draw the highlight region. (highlightBackground/HighlightBackground)
- highlightcolor=
- See highlightbackground. (highlightColor/HighlightColor)
- highlightthickness=
- See highlightbackground. (highlightThickness/HighlightThickness)
- justify=
- Defines how to align multiple lines of text. Use LEFT, RIGHT, or CENTER. Note that to position the text inside the widget, use the anchor option. Default is LEFT. (justify/Justify)
- padx=
- Horizontal padding. Default is -1 (no padding). (padX/Pad)
- pady=
- Vertical padding. Default is -1 (no padding). (padY/Pad)
- relief=
- Border decoration. The default is FLAT. Other possible values are SUNKEN, RAISED, GROOVE, and RIDGE. (relief/Relief)
- takefocus=
- If true, the widget accepts input focus. The default is false. (takeFocus/TakeFocus)
- text=
- Message text. The widget inserts line breaks if necessary to get the requested aspect ratio. (text/Text)
- textvariable=
- Associates a tkinter variable (usually a StringVar) with the message. If the variable is changed, the message text is updated. (textVariable/Variable)
- width=
- Widget width, in character units. If omitted, the widget picks a suitable width based on the aspect setting. (width/Width)