Search code examples
pythontreeviewttk

How can I configure a Treeview to have multiline items?


I have a treeview item, and I want it to have two lines per item, using a newline character where decided. But it just puts the bottom items over the upper ones, regardless of how much space the text fills. Any fixes? I've tried messing with the internal padding of the Treeview, but that didn't seem to do anything.

import tkinter as tk
from tkinter import ttk

main_window=tk.Tk()

tree= ttk.Treeview(main_window, columns=['column 1', 'column 2'], show='tree headings')
for i in range(3):
    tree.heading('#%s'%i, text=i)

tree.insert('', 'end', text='item 1', values=['text1-1\ntext1-2', 'text1-3\ntext1-4'])
tree.insert('', 'end', text='item 2', values=['text2-1\ntext2-2', 'text2-3\ntext2-4'])

tree.pack()

main_window.mainloop()

Solution

  • I have found the solution to this. You need to set a style for the treeview and use the rowheight item, changing the height of a each row. But as there are some complications doing this, I found a relevant thread which discusses implementation.