Tuesday, February 5, 2019

tkinter example - 11 (Message Widget)

The Message Widget used to display short text messages. It can wrap text, and adjust its width to maintain a given aspect ratio. The message widget is similar in its functionality to the Label widget, but it is more flexible in displaying text, e.g. the font can be changed while the Label widget can only display text in a single font. This means that we can choose arbitrarily a font for one widget, but the text of this widget will be rendered solely in this font. This means that we can't change the font within a widget. So it's not possible to have a text in more than one font. If you need to display text in multiple fonts, we suggest to use a Text widget. The Message widget is a variant of the Label, designed to display multi-line messages.

The following program creates a Message widget:

from tkinter import *

master = Tk()
master.title('Albert Einstein says:')

whatever_you_do = "Try not to become a man of success. Rather become a man of value."
msg = Message(master, text = whatever_you_do)
msg.config(bg='lightgreen', font=('times', 24, 'italic'))
msg.pack()

mainloop( )

The output of the program is shown below:




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:

Message(master, text = whatever_you_do,width=500)

See the modified program below:

from tkinter import *

master = Tk()
master.title('Albert Einstein says:')

whatever_you_do = "Try not to become a man of success. Rather become a man of value."
msg = Message(master, text = whatever_you_do,width=500)
msg.config(bg='lightgreen', font=('times', 24, 'italic'))
msg.pack()

mainloop( )

The output of the program is shown below:




Some more options which can be used with the Message() function are:

anchorThe position, where the text should be placed in the message widget: N, NE, E, SE, S, SW, W, NW, or CENTER. The Default is CENTER.
aspectAspect 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.
backgroundThe background color of the message widget. The default value is system specific.
bgShort for background.
borderwidthBorder width. Default value is 2.
bdShort for borderwidth.
cursorDefines the kind of cursor to show when the mouse is moved over the message widget. By default the standard cursor is used.
fontMessage font. The default value is system specific.
foregroundText color. The default value is system specific.
fgSame as foreground.
highlightbackgroundTogether with highlightcolor and highlightthickness, this option controls how to draw the highlight region.
highlightcolorSee highlightbackground.
highlightthicknessSee highlightbackground.
justifyDefines 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.
padxHorizontal padding. Default is -1 (no padding).
padyVertical padding. Default is -1 (no padding).
reliefBorder decoration. The default is FLAT. Other possible values are SUNKEN, RAISED, GROOVE, and RIDGE.
takefocusIf true, the widget accepts input focus. The default is false.
textMessage text. The widget inserts line breaks if necessary to get the requested aspect ratio. (text/Text)
textvariableAssociates a Tkinter variable with the message, which is usually a StringVar. If the variable is changed, the message text is updated.
widthWidget width given in character units. A suitable width based on the aspect setting is automatically chosen, if this option is not given.

This was a brief introduction about Message widget. Try to use the other options shown above in your programs to understand their functionality. I'll end this post now and till we meet next, keep learning and practicing Python as Python is easy to learn!
Share:

0 comments:

Post a Comment