Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Python

Cách thay đổi độ rộng và trọng số cột ttk.Treeview trong Python 3.3


Widget Treeview hiển thị dữ liệu dạng bảng. Để cấu hình độ rộng cột, dùng thuộc tính width và stretch (NO để cố định, YES để co giãn).

Ví dụ minh họa

from tkinter import *
from tkinter import ttk

win = Tk()
win.geometry("700x350")

style = ttk.Style()
style.theme_use('clam')

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="Ngôn ngữ lập trình")

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

Kết quả đầu ra

Bảng hiển thị ngôn ngữ lập trình với cột ID cố định width=100.

Cách thay đổi độ rộng và trọng số cột ttk.Treeview trong Python 3.3