How to word-wrap text in Tkinter Text?

PythonTkinterServer Side ProgrammingProgramming

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.


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
#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.")


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.

Published on 22-Apr-2021 07:36:04