« Tkinter
Scrollbar helps user to scroll and view the entire content.
Scrollbar(parent window, options .. )
Tkinter Scrollbar and integrating it with text & Spinbox using different layouts with all options
VIDEO
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()
Options
In above code we used 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 .
orient
By default the option orient is 'vertical' , we can set it to horizontal to display the Scrollbar in horizontal direction.
sb = tk.Scrollbar(my_w,orient='horizontal')
Using Scrollbar with Listbox
We will use grid layout to place the widgets
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
Using Scrollbar with Text
We will use grid to place the widgets and one text box is used for inputs
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()
Adding scrollbar to Treeview
Since Treeview is used to show tabular data, we may have to show more rows by allowing the user to scroll up or down by using scrollbar.
How Vertical scrollbar is added to Treeview while displaying MySQL records
→
« OptionMenu Projects in Tkinter
← Subscribe to our YouTube Channel here