Python Message - How can i create simple Message Widget with Tkinter

Last updated Dec 17, 2021

In your application, you may want to display some essential texts for your users to read. The Tkinter message widget lets you accomplish that. This widget allows you to show brief text messages. You can indeed opt for a Tkinter label to display text rather than a Tkinter message, but the two are extremely different. In the Tkinter messages, you can edit the font of the text shown, whereas you cannot do that on the label. You may also span numerous lines of text in your message and justify your content, which is restricted by the Tkinter label.

This implies that if you'd like to show numerous textual lines with various fonts, you should opt for the Tkinter message rather than the Tkinter font.

How to create a Tkinter Message

The syntax for creating a Tkinter message to show your text is as follows:

w = message (master, option, and so on).

The master represents the parent window, while options represent the numerous choices you may use in your widget.

Options for Tkinter Messages

Let's have a look at some Tkinter message options for displaying text in your application:

  • Bg-Specifies the color of the messages' backdrop.

  • Borderwidth- defines the message's border width.

  • Bd- indicates the size of the message's border.

  • Cursor- defines how the cursor behaves when it hovers over the messages widget.

  • Typeface-Specifies the message's font.

  • Position- The text message is positioned on the widget using an anchor. The default setting is CENTER.

  • Aspect- specifies the aspect ratio, expressed as a width/height ratio in percent. The default setting is 150.

  • Foreground -The color of the text is specified.

  • Fg- denotes the foreground.

  • Justify- Justifies the use of numerous lines of text.

  • Padx- indicates the horizontal padding.

  • Pady- defines the vertical padding.

  • Text- contains the message texts.

  • Takefocus- If the value is true, the widget will allow the input focus.

  • Textvariable-associates the Tkinter variable with the message that constitutes the StringVar

  • Width- defines the width of the specified character units.

  • Underline- highlights the chosen value.

  • Wraplength- wraps the text in the number of lines specified.

Example of the Tkinter Message 

This Tkinter message example displays the message, “I am  the a coding  master”.

from tkinter import *

top = Tk()

top.geometry("100x100")

var = StringVar()

msg = Message(top, text="I am  the a coding  master", bg="green",  justify="center")

msg.pack()

top.mainloop()

 

 

Output

Python Message box Tkinter

 

Download Source code

 

Article Contributed By :
https://www.rrtutors.com/site_assets/profile/assets/img/avataaars.svg

271 Views