How to code the tkinter "scrolledtext" module?

TkinterPythonGUI-Programming

A widget in a Tkinter application can be configured easily by adding extensions and properties to it. The Text widget in tkinter is used to accept multiline user input. We can make the text inside the Text widget Scrollable by adding a scrollbar to it.

The ScrolledText Widget is also available in Tkinter Library. It is the combination of Text widget and Scrollbar widget which provides features like scrolling the text in an application. In order to use ScrolledText widget in an application, you must import it first. The scrolledtext widget works similar to the standardText widget. It includes all the properties and attributes that can be used extensively in the widget.

Example

# Import the tkinter library
from tkinter import *
from tkinter.scrolledtext import ScrolledText
from lorem_text import lorem

# Create an instance of tkinter frame
win= Tk()

# Set the size of the Tkinter window
win.geometry("700x350")

# Set the title of the window
win.title("Scrolled Text")

# Add ScrolledText widget
size= 10
text= ScrolledText(win, width=40, height= 50)
text.insert(END, lorem.paragraphs(10))
text.pack(fill= BOTH, side= LEFT, expand= True)

win.mainloop()

Output

Executing the above code will display a multiline Text widget. The text inside the Text widget are scrollable. We can see even better output by adding wrapping feature in it.

raja
Published on 07-Jun-2021 11:35:13
Advertisements