w3resource

Python Tkinter message widget: Multi-line text with word wrapping made easy

Python tkinter widgets: Exercise-21 with Solution

Write a Python GUI program to create a Message widget for displaying multi-line text with word wrapping using tkinter module.

Sample Solution:

Python Code:

import tkinter as tk
parent = tk.Tk()
parent.title("Message Widget Example")
# Create a Message widget with word wrapping
message = tk.Message(parent, text="This is a multi-line text message widget. It automatically wraps text to fit within the specified width. Write a Python GUI program to create a Message widget for displaying multi-line text with word wrapping using tkinter module.", width=200)
message.pack(padx=20, pady=20)
parent.mainloop()

Explanation:

In the exercise above -

  • Import 'tkinter' as 'tk' for creating the GUI components.
  • Create the main window using tk.Tk() and set its title.
  • Create a Message widget named 'message' and specify the text content to display. The width parameter controls the Message widget width. If the text exceeds this width, it automatically wraps to the next line.
  • Pack the Message widget into the main window with padding (padx and pady) for spacing.
  • The main event loop, root.mainloop(), starts the Tkinter application.

Sample Output:

Tkinter: Multi-line text with word wrapping made easy. Part-1

Flowchart:

Flowchart: Multi-line text with word wrapping made easy.

Python Code Editor:


Previous: Custom range and step size made easy.
Next: Effortlessly choose and display colors.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/python-exercises/tkinter/python-tkinter-widgets-exercise-21.php