ttk.Treeview - 如何获取多行列

问题描述 投票:0回答:1

我正在尝试在 tkinter 树视图中实现多行列。在下面的示例中,我希望“描述”列中的文本跨多行换行。如果用户拖动列边框来调整其大小,文本应相应换行。这可能吗?谢谢!

上述示例的代码如下所示 -

from tkinter import ttk

if __name__ == "__main__":
    root = tk.Tk()
    root.title("TreeView Example")

    tree = ttk.Treeview(root, columns=('Category', 'Product ID', 'Description', 'Quantity'))
    tree.heading('#0', text='')
    tree.heading('Category', anchor=tk.W, text='Category')
    tree.heading('Product ID', anchor=tk.W, text='Product ID')
    tree.heading('Description', anchor=tk.W, text='Description')

    tree.heading('Quantity', text='Quantity')
    tree.column('Quantity', anchor=tk.CENTER)

    tree.insert('', 'end', "Level 1", text="Level 1")
    tree.insert("Level 1", 'end', "Level 2", text="Level 2")
    tree.insert("Level 2", 'end', "Level 3", text="Level 3")

    tree.insert("Level 3", 'end', "line-1", text="")
    tree.set("line-1", 'Category', "CatA")
    tree.set("line-1", 'Product ID', "ProdA")
    tree.set("line-1", 'Description', "The quick brown fox jumped over the lazy dog. The quick brown fox " +
                  "jumped over the lazy dog. The quick brown fox jumped over the lazy dog. The quick brown fox " +
                  "jumped over the lazy dog. The quick brown fox jumped over the lazy dog. The quick brown fox " +
                  "jumped over the lazy dog. The end")
    tree.set("line-1", 'Quantity', "1")

    tree.pack(expand=True, fill='both')

    root.mainloop()
python tkinter treeview ttk
1个回答
0
投票
  1. 应用包裹长度样式:

风格 = ttk.Style() style.configure("Treeview",wraplength=300) # 根据需要调整wraplength

  1. 插入 人物:

long_description =“这是一个很长的描述

© www.soinside.com 2019 - 2024. All rights reserved.