Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Attach scrollbar to listbox as opposed to window in Tkinter
A Listbox widget contains a list of items such as a list of numbers or characters. Let us suppose that you want to create a long list of items using the Listbox widget. Then, there should be a proper way to view all the items in the list. Adding Scrollbar to the list box widget will be helpful in this case.
To add a new scrollbar, you have to use Listbox(parent, bg, fg, width, height, bd, **options) constructor. Once the Listbox is created, you can add a scrollbar to it by creating an object of Scrollbar(**options).
Example
#Import the required libraries
from tkinter import *
from tkinter import ttk
#Create an instance of Tkinter Frame
win = Tk()
#Set the geometry of Tkinter Frame
win.geometry("700x350")
#Create a vertical scrollbar
scrollbar= ttk.Scrollbar(win, orient= 'vertical')
scrollbar.pack(side= RIGHT, fill= BOTH)
#Add a Listbox Widget
listbox = Listbox(win, width= 350, bg= 'bisque')
listbox.pack(side= LEFT, fill= BOTH)
for values in range(100):
listbox.insert(END, values)
listbox.config(yscrollcommand= scrollbar.set)
#Configure the scrollbar
scrollbar.config(command= listbox.yview)
win.mainloop()
Output
Running the above code will display a window that contains a Listbox widget with several items in it. The Vertical scrollbar is attached to the Listbox widget.

Advertisements