Tkinter Treeview绑定滚动条
前面介绍过滚动条Scrollbar的用法,同时也将Scrollbar与Listbox进行了结合。我们可以参考这两节的思路将Scrollbar应用在Treeview控件中。
示例1
将滚动条应用在Treeview控件中。
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) # 经过试验,以上两行代码交换顺序后无影响
# 建立栏标题
tree.heading("#0",text="State") # 图标栏
tree.heading("cities",text="City")
# 格式栏位
tree.column("cities",anchor=CENTER)
# 建立内容
for state in stateCity.keys():
tree.insert("",index=END,text=state,values=stateCity[state])
root.mainloop()
输出: