Tkinter Treeview Bind scrollbar
The previous section introduced the usage of Scrollbar and also combined Scrollbar with Listbox. We can refer to these two sections for ideas to apply Scrollbar to Treeview controls.
Example#1
Applies scrollbars to the Treeview control.
from tkinter import *
from tkinter.ttk import *
root = Tk()
root.title("apidemos.com")
stateCity = {"Illinois": "Chicago", "California": "Los Angeles",
"Texas": "Houston", "Washington": "Seattle",
"Jiangsu": "Nanjing", "Shandong": "Qingdao",
"Guangdong": "Guangzhou", "Fujain": "Xiamen",
"Mississippi": "Oxford", "Kentucky": "Lexington",
"Florida": "Miami", "Indiana": "West Lafeyette"}
tree = Treeview(root,columns=("cities"))
yscrollbar = Scrollbar(root)
yscrollbar.pack(side=RIGHT,fill=Y)
tree.pack()
# tree.configure(yscrollcommand=yscrollbar.set)
yscrollbar.config(command=tree.yview)
tree.configure(yscrollcommand=yscrollbar.set) # After testing, the above two lines of code have no effect after swapping the order
# Create column headings
tree.heading("#0",text="State") # Icon bar
tree.heading("cities",text="City")
# Format field
tree.column("cities",anchor=CENTER)
# Create content
for state in stateCity.keys():
tree.insert("",index=END,text=state,values=stateCity[state])
root.mainloop()
Output: