Tkinter Treeview绑定滚动条

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()

输出:

Tkinter Treeview绑定滚动条

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程