Scrollbar(parent window, options .. )
import tkinter as tk
from tkinter import *
my_w=tk.Tk()
my_w.geometry("350x200")
sb = tk.Scrollbar(my_w,cursor='hand1')
sb.pack(side=RIGHT,fill=Y)
l1 = tk.Listbox(my_w,height=8,width=90,
bg='yellow', yscrollcommand = sb.set )
l1.pack(side=LEFT,padx=15)
for i in range(1, 40): # multiple lines added to Listbox
l1.insert(END, "Line No : " + str(i))
sb.config( command = l1.yview )
my_w.mainloop()
cursor='hand'
to change the cursor shape when it is over the scrollbar. You can get a list of curshor shapes in our button widget.
sb = tk.Scrollbar(my_w,orient='horizontal')
import tkinter as tk
my_w = tk.Tk()
my_w.geometry("300x200") # Size of the window
my_w.title("plus2net.com")
l1=tk.Label(my_w,text='Listbox with Scroll bar')
l1.grid(row=1,column=1,columnspan=2,pady=10)
sb = tk.Scrollbar(my_w)
sb.grid(row=2, column=2, sticky='e')
my_list=['PHP','Python','MySQL','HTML','Jquery','Java','CSS','Perl']
l1 = tk.Listbox(my_w,height=2, yscrollcommand = sb.set)
l1.grid(row=2,column=1,padx=20,pady=20)
for element in my_list: # adding elements to Listbox
l1.insert(tk.END,element)
sb.config( command = l1.yview )
my_w.mainloop() # Keep the window open
import tkinter as tk
my_w = tk.Tk()
my_w.geometry("400x200")
sb = tk.Scrollbar(my_w)
sb.grid(row=1, column=2, sticky='w')
t1 = tk.Text(my_w, height=3, width=35)
t1.grid(row=1,column=1,padx=20,pady=20)
my_str1='1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n11\n12\n13\n14'
t1.insert(tk.END, my_str1)
sb.config( command = t1.yview )
my_w.mainloop()
for options in scroll_x.config():
print(options + ": " + str(scroll_x[options]))
import tkinter as tk
from tkinter import ttk
my_w = tk.Tk()
my_w.geometry("400x300")
# Style for scrollbar
style = ttk.Style()
style.theme_use('clam') # Ensure theme supports customization
style.configure("Custom.Vertical.TScrollbar", troughcolor="blue",
background="lightblue", width=40)
style.configure("Custom.Horizontal.TScrollbar", troughcolor="blue",
background="lightblue", width=40)
# Create Text widget
text_widget = tk.Text(my_w, wrap="none")
text_widget.grid(row=0, column=0, sticky="nsew")
# Configure grid layout
my_w.grid_rowconfigure(0, weight=1)
my_w.grid_columnconfigure(0, weight=1)
# Create styled scrollbars
scroll_y = ttk.Scrollbar(my_w, orient="vertical", command=text_widget.yview,
style="Custom.Vertical.TScrollbar")
scroll_y.grid(row=0, column=1, sticky="ns")
scroll_x = ttk.Scrollbar(my_w, orient="horizontal", command=text_widget.xview,
style="Custom.Horizontal.TScrollbar")
scroll_x.grid(row=1, column=0, sticky="ew")
# Link scrollbars to text widget
text_widget.config(yscrollcommand=scroll_y.set, xscrollcommand=scroll_x.set)
my_w.mainloop()
Author
🎥 Join me live on YouTubePassionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.