How to get a horizontal scrollbar in Tkinter?


The Scrollbar widget in tkinter is one of the useful widgets that is used to pack the container elements and their contents with a scrollbar. With Scrollbars, we can view large sets of data very efficiently.

Generally, Tkinter allows to add vertical and horizontal scrollbars. To add a horizontal scrollbar in an application, we've to use the orientation as Horizontal in the scrollbar constructor.

Example

Let us create a text editor that contains a horizontal scrollbar in it.

# Import the required library
from tkinter import *
from tkinter import ttk
from tkinter import messagebox

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

# Set the geometry
win.geometry("700x350")

# Add a Scrollbar(horizontal)
h=Scrollbar(win, orient='horizontal')
h.pack(side=BOTTOM, fill='x')

# Add a text widget
text=Text(win, font=("Calibri, 16"), wrap=NONE, xscrollcommand=h.set)
text.pack()

# Add some text in the text widget
for i in range(5):
   text.insert(END, "Welcome to Tutorialspoint...")

# Attach the scrollbar with the text widget
h.config(command=text.xview)

win.mainloop()

Output

If we run the above code, it will display a text editor that will have some text in it. The text widget is packed with a horizontal scrollbar which gets visible whenever the text overflows.

Updated on: 05-Aug-2021

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements