How to word-wrap text in Tkinter Text?


Word Wrapping plays a significant role in any textual information. It is an important feature for any text editor which breaks the section of a particular text to fit into multiple sections of lines where possible. It is used to fit the content in the width of a text document. In Tkinter, we can wrap the words or chars in the text widget using the wrap property. The default values for the wrap properties are – WORD, CHARS, or NONE.

Example

In this example, we will wrap all the words of a text widget using the wrap property.

#Import tkinter library
from tkinter import *
from tkinter import ttk
#Create an instance of Tkinter frame or window
win= Tk()
#Set the geometry of tkinter frame
win.geometry("750x250")
#Create a text widget and wrap by words
text= Text(win,wrap=WORD)
text.insert(INSERT,"Python is an interpreted, high-level and general-purpose programming language. Python's design philosophy emphasizes code readability with its notable use of significant indentation.")
text.pack()
win.mainloop()

Output

Running the above code will display a window containing some text. The text is wrapped by words which helps the user to read the document or text file easily.

Updated on: 22-Apr-2021

6K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements