tkinter给表格控件(treeview)添加滚动条

tkinter给表格控件(treeview)添加滚动条

Scroll Down
# 定义列名集合
self.columns = ["Sno", "Name", "Score1", "Score2"]
"""
    定义Treeview
    self.Frame2为父容器
    columns为列名集合
    show="headings"表示显示表头
"""
self.tree = Treeview(self.Frame2, columns=self.columns, show="headings")
# 定义各列列宽及对齐方式
self.tree.column("Sno", width=100, anchor="sw")
self.tree.column("Name", width=100, anchor="center")
self.tree.column("Score1", width=100, anchor="center")
self.tree.column("Score2", width=100, anchor="center")
# 定义表头
self.tree.heading("Sno", text="PK")
self.tree.heading("Name", text="name")
self.tree.heading("Score1", text="Excel1")
self.tree.heading("Score2", text="Excel2")
# 放置控件,rel*表示使用相对定位,相对于父容器的定位
self.tree.place(relx=0.004, rely=0.028, relwidth=0.964, relheight=0.95)

"""
    定义滚动条控件
    orient为滚动条的方向,vertical--纵向,horizontal--横向
    command=self.tree.yview 将滚动条绑定到treeview控件的Y轴
"""
self.VScroll1 = Scrollbar(self.Frame2, orient='vertical', command=self.tree.yview)
self.VScroll1.place(relx=0.971, rely=0.028, relwidth=0.024, relheight=0.958)
# 给treeview添加配置
self.tree.configure(yscrollcommand=self.VScroll1.set)

其中最重要的是:

self.VScroll1 = Scrollbar(self.Frame2, orient='vertical', command=self.tree.yview)

self.tree.configure(yscrollcommand=self.VScroll1.set)

需要注意的是  self.tree.configure(yscrollcommand=self.VScroll1.set) 不能在定义滚动条之前,否则会报错:“self.VScroll1”未定义

 

 

边学习边长大,每天向前咕蛹咕蛹