How to attach a vertical scrollbar to a Treeview using Tkinter?


If you want to display a list of items that contains some columns in it, then you can use the Treeview widget in Tkinter. The Treeview widget allows the user to add a large number of lists along with the properties that can be customized instantly.

If you want to attach a vertical scrollbar to the list of items in a Treeview widget, then you can define a constructor of Scrollbar and configure it by adding the command to it. Let’s take an example and see how it works.

Example

# Import the required libraries
from tkinter import *
from tkinter import ttk

# Create an instance of tkinter frame
win= Tk()

# Set the size of the tkinter window
win.geometry("700x350")

# Create an instance of Style widget
style= ttk.Style()
style.theme_use('clam')

# Add a Treeview widget and set the selection mode
tree= ttk.Treeview(win, column=("c1", "c2"), show='headings', height= 8, selectmode="browse")
tree.column("#1", anchor=CENTER, stretch= NO)
tree.heading("#1", text="Fname")
tree.column("#2", anchor=CENTER, stretch=NO)
tree.heading("#2", text="Lname")

# Insert the data in Treeview widget
tree.insert('', 'end', text= "1",values=('Alex', 'M'))
tree.insert('', 'end', text="2",values=('Belinda','Cross'))
tree.insert('', 'end', text="3",values=('Ravi','Malviya'))
tree.insert('', 'end', text="4",values=('Suresh','Rao'))
tree.insert('', 'end', text="5",values=('Amit','Fernandiz'))
tree.insert('', 'end', text= "6",values=('Raghu','Sharma'))
tree.insert('', 'end',text= "7",values=('David','Nash'))
tree.insert('', 'end',text= "8",values=('Ethan','Plum'))
tree.insert('', 'end', text= "9", values=('Janiece','-'))

# Adding a vertical scrollbar to Treeview widget
treeScroll = ttk.Scrollbar(win)
treeScroll.configure(command=tree.yview)
tree.configure(yscrollcommand=treeScroll.set)
treeScroll.pack(side= RIGHT, fill= BOTH)
tree.pack()

win.mainloop()

Output

Running the above code will display a window that contains a list of items in a treeview widget along with a vertical scrollbar attached to it.

Updated on: 16-Dec-2021

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements