
How to Add a Scrollbar to a Listbox
Post by Amina Delali, August 20th, 2023
Description
Google found 17 different Lions names on the web. For example: Simba, the lion that must learn about responsibility and bravery after the death of his father. Or Mufasa, Simba's father and the former king in The Lion King. I added them to a text file: lions.txt , and I wanted to display them on a ListBox Tkinter widget. I didn't change the default height of the Listbox, but I changed the default font. Anyway, the height of the listbox wasn't enough to display all the names at once.
The overall idea is simple. I click on a Button tkinter widget. The list of Lion's names will automatically be added to the ListBox. To see all the content of the list, I will have to scroll through the names of the list. I can scroll using the keyboard, but it is preferrable to use a Scrollbar tkinter widget. The Scrollbar is a Python widget that can be linked to other widgets, such as Listbox, Text, and Canvas, to allow users to view all parts of the linked widget's content, that is typically larger than the available space.
In the code, I first open the lions.txt file, and recuperate its content into a list named "lionsNameList"".

I will create a Frame widget to contain the ListBox with the Scrollbar linked to it. So, I will:
- Create the Frame "aFrame"
- Create the Scrollbar "myScrollBar", and the Listbox "myListBox", by specifying "aFrame" as their master parameter.
- Pack the Scrollbar on the right side, and the ListBox on the left one. I will also specify the fill = "y" as the parameter of the pack method of the Scrollbar
- Link between the Scrollbar and the Listbox by setting these 2 parameters:
- command=myListBox.yview for the Scrollbar
- yscrollcommand=myScrollBar.set for the Listbox

Now, all I have to do, is to define the method that will add the recuperated names that are in the list "lionsNameList", to the Listbox "myListBox". The method will be called "addLions", and it will be set as the command property of the Button "myButton", that I will create after defining the function.

You can find the code below, but do not forget to download and save the : lions.txt file in the same folder from where you will be running your code.
The Code
from tkinter import Tk,Listbox,Button,Scrollbar,Frame
lionsFile =open("lions.txt","r")
lionsNameList = lionsFile.readlines()
pinkLavender = "#d8b2d1"
goldFoil = "#bd9b16"
crayola = "#c5e384"
blackRussian = "#24252B"
main_window =Tk()
main_window.configure(bg=pinkLavender)
main_window.geometry("400x400+300+100")
main_window.title("Scrollbar TK")
aFrame = Frame (main_window)
aFrame.pack(pady=50)
myListBox = Listbox(aFrame,relief="flat",
selectbackground=crayola,
selectforeground=blackRussian,
font=("Arial",14),)
myListBox.pack(side="left")
myScrollBar = Scrollbar(aFrame,orient="vertical",command=myListBox.yview,)
myScrollBar.pack(side ="right", fill = "y" )
myListBox.configure(yscrollcommand=myScrollBar.set)
def addLions():
for name in lionsNameList:
myListBox.insert('end',name)
myButton = Button(main_window,text="Add My List of Lions Names",
bg=goldFoil, activebackground=crayola,
font=("Arial",14),
command=addLions,
relief="flat")
myButton.pack(side="bottom", pady=20)
main_window.mainloop()
The Output
Something to say?
If you want to add something about this post, please feel free to do it by commenting below 🙂.