How do I use Tkinter in Python to create line-wrapped text that fills the width of the window?

TkinterPythonGUI-Programming

Tkinter provides Text widget to input data in a text field. It can accept multiline user input. Tkinter includes many inbuilt properties and features that can be used to improve the look and feel of the context. The text written in Text widget can be wrapped with wrap property. The wrap allows users to simplify the text editor by wrapping the context in words, characters or None. It fixes the indentation of the text inside the Text Editor.

Example

In this example, we will wrap the sentences by words which means each word gets selected automatically without following the same line.

# Import the required libraries
from tkinter import *
from lorem_text import lorem

# Create an instance of tkinter frame
win = Tk()
win.geometry("700x350")

# Add a text widget and fill with lorel Ipsum generator paragraphs
size= 5
text = Text(win, wrap="word")
text.insert(END, lorem.paragraphs(size))
text.pack()

win.mainloop()

Output

Running the above code will display a window with some text in a text widget. The content written inside the text widget is wrapped by words.

raja
Published on 07-Jun-2021 11:21:05
Advertisements