Open In App

Python-Tkinter Treeview scrollbar

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

Python has several options for constructing GUI and python tkinter is one of them. It is the standard GUI library for Python, which helps in making GUI applications easily. It provides an efficient object-oriented interface to the tk GUI toolkit. It also has multiple controls called widgets like text boxes, scrollbars, buttons, etc. Moreover, Tkinter has some geometry management methods namely, pack(), grid(), and, place() which are helpful in organizing widgets.
Note: For more information, refer to Python GUI – tkinter
 

Treeview scrollbar

When a scrollbar uses treeview widgets, then that type of scrollbar is called as treeview scrollbar. Where, a treeview widget is helpful in displaying more than one feature of every item listed in the tree to the right side of the tree in the form of columns. However, it can be implemented using tkinter in python with the help of some widgets and geometry management methods as supported by tkinter. 
Below example illustrates the usage of Treeview Scrollbar using Python-tkinter: 
Example 1: 
 

Python




# Python program to illustrate the usage of
# treeview scrollbars using tkinter
 
 
from tkinter import ttk
import tkinter as tk
 
# Creating tkinter window
window = tk.Tk()
window.resizable(width = 1, height = 1)
 
# Using treeview widget
treev = ttk.Treeview(window, selectmode ='browse')
 
# Calling pack method w.r.to treeview
treev.pack(side ='right')
 
# Constructing vertical scrollbar
# with treeview
verscrlbar = ttk.Scrollbar(window,
                           orient ="vertical",
                           command = treev.yview)
 
# Calling pack method w.r.to vertical
# scrollbar
verscrlbar.pack(side ='right', fill ='x')
 
# Configuring treeview
treev.configure(xscrollcommand = verscrlbar.set)
 
# Defining number of columns
treev["columns"] = ("1", "2", "3")
 
# Defining heading
treev['show'] = 'headings'
 
# Assigning the width and anchor to  the
# respective columns
treev.column("1", width = 90, anchor ='c')
treev.column("2", width = 90, anchor ='se')
treev.column("3", width = 90, anchor ='se')
 
# Assigning the heading names to the
# respective columns
treev.heading("1", text ="Name")
treev.heading("2", text ="Sex")
treev.heading("3", text ="Age")
 
# Inserting the items and their features to the
# columns built
treev.insert("", 'end', text ="L1",
             values =("Nidhi", "F", "25"))
treev.insert("", 'end', text ="L2",
             values =("Nisha", "F", "23"))
treev.insert("", 'end', text ="L3",
             values =("Preeti", "F", "27"))
treev.insert("", 'end', text ="L4",
             values =("Rahul", "M", "20"))
treev.insert("", 'end', text ="L5",
             values =("Sonu", "F", "18"))
treev.insert("", 'end', text ="L6",
             values =("Rohit", "M", "19"))
treev.insert("", 'end', text ="L7",
             values =("Geeta", "F", "25"))
treev.insert("", 'end', text ="L8",
             values =("Ankit", "M", "22"))
treev.insert("", 'end', text ="L10",
             values =("Mukul", "F", "25"))
treev.insert("", 'end', text ="L11",
             values =("Mohit", "M", "16"))
treev.insert("", 'end', text ="L12",
             values =("Vivek", "M", "22"))
treev.insert("", 'end', text ="L13",
             values =("Suman", "F", "30"))
 
# Calling mainloop
window.mainloop()


Output:
 

In the above program, we have used pack() method of the geometry management methods. And, we have constructed only vertical scrollbar as per the requirement of the code but you can construct both the bars as per your requirements. Moreover, anchors are used here in order to define the positions of the text. However, you can also use other geometry management methods in order to construct the treeview scrollbar.
 



Last Updated : 15 Oct, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads