How to attach a Scrollbar to a Text widget in Tkinter?



Tkinter Text widget is used to accept multiline user Input. It is similar to Entry Widget but the only difference is that Text widget supports multiple line texts. In order to create a Text widget, we have to instantiate a text object.

Adding multiple texts will require to add the ScrollBar. In order to add a scrollbar in the text widget, we can call the ScrolledText(root) function. This function generally creates a text field with a scrollbar.

The ScrolledText(root) function resides in Tkinter ScrolledText Module. We can import it using the following command,

from tkinter.scrolledtext import ScrolledText

Example

In this example, we will create a Text widget and then add a scrollbar to it.

#Import the library from tkinter import * from tkinter.scrolledtext import ScrolledText #Create an object of tkinter window or frame win = Tk() #Define the geometry of window win.geometry("650x250") #Create an instance of Text Widget ScrolledText(win).pack() win.mainloop()

Output

Running the above code will display a window with a text widget that supports multiline user Input and a native scrollbar.

Updated on: 2021-03-27T06:07:30+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements