tkinter給表格控件(treeview)添加滾動條

# 定義列名集合
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”未定義

 

 

邊學習邊長大,每天向前咕蛹咕蛹

 

 

 

 

 

發佈了16 篇原創文章 · 獲贊 7 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章