Vertical and Horizontal Scrollbars on Tkinter Widget

Scrollbars are useful to provide dynamic behavior in an application. In a Tkinter application, we can create Vertical as well as Horizontal Scrollbars. Scrollbars are created by initializing the object of Scrollbar() widget.

To create a horizontal scrollbar, we have to provide the orientation, i.e., "horizontal" or "vertical". Scrollbars can be accessible once we configure the particular widget with the scrollbars.


#Import the required libraries
from tkinter import *

#Create an instance of Tkinter Frame
win = Tk()

#Set the geometry of Tkinter Frame

#Create some dummy Text
text_v = "Python is dynamically-typed and garbage-collected. It supports multiple programming paradigms, including structured (particularly, procedural), object-oriented and functional programming."
text_h = ("\nNASA \n Google \nNokia \nFacebook \n Netflix \n Expedia \n Reddit \n Quora \n MIT\n Udemy \n Shutterstock \nSpotify\nAmazon\nMozilla\nDropbox")

#Add a Vertical Scrollbar
scroll_v = Scrollbar(win)
scroll_v.pack(side= RIGHT,fill="y")

#Add a Horizontal Scrollbar
scroll_h = Scrollbar(win, orient= HORIZONTAL)
scroll_h.pack(side= BOTTOM, fill= "x")
#Add a Text widget
text = Text(win, height= 500, width= 350, yscrollcommand= scroll_v.set,
xscrollcommand = scroll_h.set, wrap= NONE, font= ('Helvetica 15'))
text.pack(fill = BOTH, expand=0)
text.insert(END, text_v)
text.insert(END, text_h)

#Attact the scrollbar with the text widget
scroll_h.config(command = text.xview)
scroll_v.config(command = text.yview)



Running the above code will display a window containing context about Python Programming language. The context can be dynamically viewed using horizontal and vertical scrollbars.

Updated on: 26-May-2021

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started