Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

วิธีเปลี่ยนความกว้างและน้ำหนักคอลัมน์ ttk.Treeview ใน Python 3.3


เพื่อแสดงข้อมูลจำนวนมากในแอปพลิเคชัน Tkinter ให้ใช้ Widget Treeview โดยทั่วไปเราแสดงข้อมูลผ่านตารางที่มีแถวและคอลัมน์ สามารถเพิ่มข้อมูลเป็นตารางด้วย Treeview widget

เพื่อกำหนดความกว้างคอลัมน์ของ Treeview ให้ใช้คุณสมบัติ width และ stretch ซึ่งตั้งค่าความกว้างคอลัมน์ตามค่าที่กำหนด

ตัวอย่าง

ในตัวอย่างนี้ สร้างตารางที่มีรายการภาษาโปรแกรมมิ่ง ความกว้างคอลัมน์ 'ID' และ 'Programming Language' ตั้งตามเนื้อหา สามารถกำหนดค่าเพื่อตั้งความกว้างคอลัมน์ได้

# 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=CENTER, stretch=NO, width=100)
tree.heading("# 1", text="ID")
tree.column("# 2", anchor=CENTER, 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()

ผลลัพธ์

รันโค้ดเพื่อแสดงตารางที่มีรายการภาษาโปรแกรมมิ่งและดัชนี

วิธีเปลี่ยนความกว้างและน้ำหนักคอลัมน์ ttk.Treeview ใน Python 3.3