Adding a scrollbar to a group of widgets in Tkinter


Let’s suppose you want to add a scrollbar to a group of widgets in an application, then you can use the Scrollbars property in tkinter. Adding Scrollbars to a group of widgets can be achieved by Scrollbar(....options).

Example

In this example, we will define a group of Listbox widgets and then add a vertical scrollbar to make the list scrollable.

#Import the required library
from tkinter import *

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

#Define the geometry
win.geometry("750x400")

#Create a listbox
listbox= Listbox(win)
listbox.pack(side =LEFT, fill = BOTH)

#Create a Scrollbar
scrollbar = Scrollbar(win)
scrollbar.pack(side = RIGHT, fill = BOTH)

#Insert Values in listbox
for i in range(150):
   listbox.insert(END, i)

listbox.config(yscrollcommand = scrollbar.set)
scrollbar.config(command = listbox.yview)
win.mainloop()

Output

Running the above code will display a window that contains a list of numbers in the range 1-150. The list of numbers is bound with a vertical scrollbar which makes the list vertically scrollable.

Updated on: 15-Apr-2021

521 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements