Knowpapa.com - a developer's blog

Making Scrollable Text Area with Tkinter | Python Tutorials Example

Tkinter text-area widget natively does not have class for Scrollable Text Area. However, we can easily make one by using the text area widget and the scroll widget together.

Here’s the code:


from Tkinter import *
class scrollTxtArea:
	def __init__(self,root):
		frame=Frame(root)
		frame.pack()
		self.textPad(frame)
		return

	def textPad(self,frame):
		#add a frame and put a text area into it
		textPad=Frame(frame)
		self.text=Text(textPad,height=50,width=90)
		
		# add a vertical scroll bar to the text area
		scroll=Scrollbar(textPad)
		self.text.configure(yscrollcommand=scroll.set)
		
		#pack everything
		self.text.pack(side=LEFT)
		scroll.pack(side=RIGHT,fill=Y)
		textPad.pack(side=TOP)
		return
def main():
	root = Tk()
	foo=scrollTxtArea(root)
	root.title('TextPad With a Vertical Scroll Bar')
	root.mainloop()
main()


An Alternate method: Using the ScrolledText Module

There is another module called ScrolledText which can be used to achieve the same functionality.

import Tkinter
from ScrolledText import *

root = Tkinter.Tk(className=" Another way to create a Scrollable text area")
textPad = ScrolledText(root, width=50, height=40)
textPad.pack()
root.mainloop()

Though the second methods is short and simple, I prefer to use the Tkinter method as it reduces dependency on other modules.