The Scrollbar widget is used to add scrolling capability to various widgets like Listbox, Text, and Canvas.
import tkinter as tk
root = tk.Tk()
root.title("Scrollbar Example")
root.geometry("300x200")
# Create a frame to hold the listbox and scrollbar
frame = tk.Frame(root)
frame.pack(fill=tk.BOTH, expand=True, padx=10, pady=10)
# Create a scrollbar
scrollbar = tk.Scrollbar(frame)
scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
# Create a listbox
listbox = tk.Listbox(frame, yscrollcommand=scrollbar.set)
# Add items to the listbox
for i in range(1, 51):
listbox.insert(tk.END, f"Item {i}")
listbox.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
# Configure the scrollbar to work with the listbox
scrollbar.config(command=listbox.yview)
root.mainloop()