วิดเจ็ต Treeview ใช้เพื่อแสดงข้อมูลในโครงสร้างแบบลำดับชั้น โดยทั่วไป ข้อมูลจะแสดงผ่านตารางที่มีชุดของแถวและคอลัมน์ เราสามารถเพิ่มข้อมูลในรูปแบบของตารางโดยใช้วิดเจ็ต Treeview
ในการกำหนดค่าตำแหน่งของรายการในคอลัมน์ เราสามารถใช้คุณสมบัติสมอ มันกำหนดตำแหน่งของคอลัมน์วิดเจ็ต Treeview ด้วยค่าที่กำหนด แต่ละแถวในตารางจะสัมพันธ์กับคอลัมน์ ในการจัดแนวข้อความของแถวทางด้านขวา เราสามารถใช้ สมอ คุณสมบัติและกำหนดค่า "E", (anchor=E) .
ตัวอย่าง
# Import the required libraries
from tkinter import *
from tkinter import ttk
# Create an instance of tkinter frame
win=Tk()
# Set the size of the tkinter window
win.geometry("700x350")
# Create an instance of Style widget
style=ttk.Style()
style.theme_use('clam')
# Add a Treeview widget
tree=ttk.Treeview(win, column=("c1", "c2"), show='headings', height=8)
tree.column("# 1", anchor=E, stretch=NO, width=100)
tree.heading("# 1", text="Index")
tree.column("# 2", anchor=E, stretch=NO)
tree.heading("# 2", text="Programming Language")
# Insert the data in Treeview widget
tree.insert('', 'end',text="1",values=('1','C++'))
tree.insert('', 'end',text="2",values=('2', 'Java'))
tree.insert('', 'end',text="3",values=('3', 'Python'))
tree.insert('', 'end',text="4",values=('4', 'Golang'))
tree.insert('', 'end',text="5",values=('5', 'JavaScript'))
tree.insert('', 'end',text="6",values=('6', 'C# '))
tree.insert('', 'end',text="7",values=('6', 'Rust'))
tree.insert('', 'end',text="8",values=('6', 'SQL'))
tree.pack()
win.mainloop() ผลลัพธ์
หากเราเรียกใช้โค้ดข้างต้น จะแสดงตารางที่มีคอลัมน์ที่มีข้อความชิดขวา
